Python Youtube Audio Downloader using Website - python

So I am writing a simple script to extract links from a Youtube video, and then download the audio from each of the links. Extracting the links I got down using urllib, however, youtube_dl is being more problematic.
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download(["https://www.youtube.com/watch?v=YQHsXMglC9A"])
I have the correct "ydl_opts" and everything, but it keeps rasing:
DownloadError: ERROR: ffprobe or avprobe not found. Please install one.
Even after Googling this, and installing ffprobe, I keep getting this error... and the weird thing is I can import ffprobe at the beginning of the program but youtube_dl doesn't detect it(?).
So I am trying another idea:
Use a third party website like "youtube-mp3.org", get a template link like:
"""http://www.youtube-mp3.org/get?video_id={1}&ts_create=1451072960&r=MjQuMjEuMTc5LjU2&h2=12c386ccffa12ee7b16dd25078df2558&s=156489""".format(video_link_id)
... then formatting it with Youtubes unique video ID, and download it with urllib.urlopen(), but that download link generates unique arguments that can't be used with other videos. So is there a way to download from a website like that, and/or supply argument like changing the download URL algorithm? Or something to that effect?
Overall objective:
Get Youtube video ID
Download and convert to mp3 or similar format
Play audio within Python
Bonus:
Do it as efficiently and quickly as possible (i.e Not downloading entire video, or extra conversion steps)

I can recommend you a package called pytube.
I have used it and am very pleased with the results. It downloads the video, and after that you can use a library to extract only the audio.
Hope it helps.

Related

How to get the direct link to a youtube video in the mp3 format in Python?

I am currently trying to find out how to get the direct link to the youtube video in mp3 format without downloading it on a computer, so I just need to get the link leading to the internet mp3 file. I tried to do it by the youtube_dl library in Python.
My code:
import youtube_dl
link = 'https://www.youtube.com/watch?v=8fATAQtY9ag'
ydl_opts = {
'format': 'bestaudio'
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(link, download=False)
print(info['formats'][0]['url'])
I am getting the result:
[youtube] 8fATAQtY9ag: Downloading webpage
https://rr4---sn-gvnuxaxjvh-n8ves.googlevideo.com/videoplayback?expire=1667397364&ei=lCJiY6fjI7P97QSmxK_QCA&ip=95.72.245.107&id=o-AFd3ju7rPL4AbxE5TTqyDlwEVYa3O038Ljvp4iTFz594&itag=249&source=youtube&requiressl=yes&mh=6d&mm=31%2C29&mn=sn-gvnuxaxjvh-n8ves%2Csn-gvnuxaxjvh-n8vk&ms=au%2Crdu&mv=m&mvi=4&pl=22&gcr=ru&initcwndbps=1213750&vprv=1&mime=audio%2Fwebm&ns=BRs0-MEqtt1vrCzmr61YjwoI&gir=yes&clen=1668462&dur=253.441&lmt=1614142570111218&mt=1667375377&fvip=8&keepalive=yes&fexp=24001373%2C24007246&c=WEB&txp=1311222&n=c2zXxbznF8OLQmMbvo&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cgcr%2Cvprv%2Cmime%2Cns%2Cgir%2Cclen%2Cdur%2Clmt&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&lsig=AG3C_xAwRgIhAPWkeRlVf2AmqbDFXrRJSLT0IMcLmlU4pQoMty5b7zzDAiEAw50GzyfT6NucOra_4kJ2BYfoPtWEa5lOCtQEcuF-Ekw%3D&sig=AOq0QJ8wRAIgFP0t2TNC_rvUpjX3Q6DqExtm0pQ5gSlYlpq_4iCv2r8CIFwzl_uW5aQ1knRCDfoVD9eVF4By4qaCBCEsZZKyPBba
This long link contains an audio file with the weba extension, but not the mp3 one. So, may be there is a way to solve this problem using this or any other library?
YouTube has never had audio-only video formats with MP3 audio, and haven't had mixed (audio+video) formats with MP3 audio for years.
Your options are:
Not use mp3. An obvious one, but pretty much anything supports mp4a these days, and lots support opus. (If you plan to use it in a browser, here are the stats for browser support for AAC and opus, and MP3 for comparison)
If you do specifically need mp3, download and convert another audio-only format (currently, the choice is between mp4a or opus). This is available in youtube-dl/yt-dlp if you have ffmpeg installed, enabled with the following options (remember to also set format together with them):
{
'extractaudio': True,
'audioformat': 'mp3',
}
Also, I think a useful information here: YouTube puts the IP address of the video requester inside the URL video, and on some videos makes it a part of the signature that authenticates you - when that's the case, the video has to be downloaded from the same IP address.

How do I sent an HTTP request which returns the URL?

Im trying to figure out how to search for and return a URL for a search term, I'm confused on how to request using python as my program is using discord.py.
What it does so far is that it can only play videos from a direct youtube link by downloading the video with youtube-dl and then converting it to audio with FFmpeg and playing it.
What I am trying to do is support the above mentioned (links) and detect whether the command is run with a link or not, if it's not a link, I would like it to search the term in Youtube using an HTTP request or similar and then return a URL to then be able to download and play it.
Any support on how I would get this to work? So far I have only found solutions in Javascript and I have absolutely no experience with java so I cant implement it.

M4S file extensin in Streaming video. How to download and use in Python

I was watching a movie on hotstar and decided to download that movie. I will use Python to do that. But for that I need the video URL. I inspected the page and came to know that it has segmented streaming file.
The requested URL that I have highlighted in the picture is:
https://hses1.hotstar.com/videos/movies/bengali/sweater/1260011260/1569583501995/ad1f5bdbac36f489da1adb7f077be226/video/avc1/3/seg-1306.m4s
It has a .m4s extension. How to read that file? Or how to work with it using Python?
USE IDM to download movie, or you can scrape bit there must be m3u8 link as well which might be not in use as of now

Accessing youtube video stream map (python)

I'm trying to write a python script that will download a youtube video, using this line of code for getting the download url:
download_url = "http://www.youtube.com/get_video?video_id={0}&t={1}&fmt=22&asv=2".format(video_id, token_value)
(video_id and token_values being info I've got from parsing youtube video url)
but this keeps downloading empty file.
Since this method is old, is there now some other form of getting download url for youtube videos?
I solved my problem (to some extent) in the meantime. I just had to access youtube video stream map and grab one of the URLs stored there and download the file. However, this works only on videos that don't have their signature encrypted. I'm still working on a solution for videos with encrypted signatures.

Python urllib basic script for youtube

I am trying to download a video from Youtube, and have tried the following:
url = "http://www.youtube.com/watch?v=f4l3pBovB_c"
urllib.urlretrieve(url,"test.mp4")
It creates the file test.mp4, but got the following error:
Windows media player cannot play the file. The Player might not
support the file type or might not support the codec that was used to
compress the file.
I would like to try to understand how to accomplish this myself, I have seen modules like youtube-dl.py, but again would like to get the code working in it's simplest form so I can understand the script as I build it.
Youtube videos are not stored on youtube.com domain, so the video which you are trying to download does not exist that URL. Downloading a youtube video is a complicated process but you can use youtube-dl for it.

Categories