Python Forum
How to get python to download YouTube videos in the background?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get python to download YouTube videos in the background?
#1
I am looking to create a small project. The idea is that you input a youtube video link and it downloads it onto your computer. I was thinking about using Selenium - it opens the browser, goes to MP3 video converter and pastes the link, downloads it. But is there a way of doing it without any visible actions (opening the browser). In other words how can I hide that the browser has opened. It would just open the py program, you input the link and after a while the video has been downloaded.
Reply
#2
There are several packages that download youtube, see: https://pypi.org/search/?q=youtube
Reply
#3
youtube-dl is big Python project with 48,454 stars on GitHub.
Can be used in many ways command-line,GUI or web-app.
I made solution's that to use myself with Flask,to get MP3 from a youtube link where youtube-dl dos the background work.
[Image: sPeRIX.jpg]
Reply
#4
im not sure if this is still accurate in the process, but if so you can replicate it in python.
https://www.computing.dcu.ie/~humphrys/N...utube.html
Recommended Tutorials:
Reply
#5
(Mar-24-2019, 11:43 AM)metulburr Wrote: im not sure if this is still accurate in the process, but if so you can replicate it in python.
https://www.computing.dcu.ie/~humphrys/N...utube.html
Hmm why try an old method that for sure will not work now,youtube-dl is always updated as a lot people work on it.
Also support a lot of other video sites youtube-dl supported sites.

Basic usage is simple youtube-dl url_link
Example get video:
λ youtube-dl https://www.youtube.com/watch?v=d8OI9FllKfg
[youtube] d8OI9FllKfg: Downloading webpage
[youtube] d8OI9FllKfg: Downloading video info webpage
[youtube] d8OI9FllKfg: Downloading js player vflGPko2h
[download] Destination: Komodo - (I Just) Died In Your Arms-d8OI9FllKfg.f248.webm
[download] 100% of 30.32MiB in 00:16
[download] Destination: Komodo - (I Just) Died In Your Arms-d8OI9FllKfg.f251.webm
[download] 100% of 4.14MiB in 00:02
[ffmpeg] Merging formats into "Komodo - (I Just) Died In Your Arms-d8OI9FllKfg.webm"
Deleting original file Komodo - (I Just) Died In Your Arms-d8OI9FllKfg.f248.webm (pass -k to keep)
Deleting original file Komodo - (I Just) Died In Your Arms-d8OI9FllKfg.f251.webm (pass -k to keep
Example get MP3 in highest quality:
λ youtube-dl --extract-audio --audio-format mp3 --audio-quality 0 https://www.youtube.com/watch?v=d8OI9FllKfg
[youtube] d8OI9FllKfg: Downloading webpage
[youtube] d8OI9FllKfg: Downloading video info webpage
[youtube] d8OI9FllKfg: Downloading js player vflGPko2h
[download] Destination: Komodo - (I Just) Died In Your Arms-d8OI9FllKfg.webm
[download] 100% of 4.14MiB in 00:02
[ffmpeg] Destination: Komodo - (I Just) Died In Your Arms-d8OI9FllKfg.mp3
Reply
#6
I use youtube-dl also. The problem is they do not support all sites. youtube-dl doesnt even support sites like.... speedvid.net, vidlox.tv, vidoza.net, clipwatching.com, vidtodo.com, etc. And some of the sits they do say they support, often have trouble working. The other problem with youtube is if they think you are pirating they wont answer your issue and just not give you helpful info. They wont help if they know the site is pirating, but they have how many sites listed as supported? The only alternative is to build your own. I have thought about this, but not had the desire to devote the time to do it.

Plus if the concept is to create your own method of downloading, using youtube-dl is not a fix. It does not teach you how to download from different sites via programming.

Quote:But is there a way of doing it without any visible actions (opening the browser).
Yes there is a way to hide the browser. You can use it in headless mode by giving it the argument --headless
Quote:
options.add_argument("--headless")
https://python-forum.io/Thread-Web-scrap...7#pid60787
Recommended Tutorials:
Reply
#7
Beside question. Is it possible to create a Python code like the one below written in PHP? Several quality options are selectable, as shown here http://imgbox.com/YciMpWyC
Output:
Youtube address: https://www.youtube.com/watch?v=zPMr0lEMqpo
Thanks, and cheers
Reply
#8
(Mar-24-2019, 02:56 PM)samsonite Wrote: Beside question. Is it possible to create a Python code like the one below written in PHP? Several quality options are selectable, as shown here http://imgbox.com/YciMpWyC
youtube-dl has all this eg listformats youtube-dl -F
G:\1_youtube
λ youtube-dl -F https://www.youtube.com/watch?v=d8OI9FllKfg
[youtube] d8OI9FllKfg: Downloading webpage
[youtube] d8OI9FllKfg: Downloading video info webpage
[info] Available formats for d8OI9FllKfg:
format code  extension  resolution note
249          webm       audio only DASH audio   69k , opus @ 50k, 1.58MiB
250          webm       audio only DASH audio   90k , opus @ 70k, 2.09MiB
140          m4a        audio only DASH audio  127k , m4a_dash container, mp4a.40.2@128k, 3.89MiB
171          webm       audio only DASH audio  142k , vorbis@128k, 3.82MiB
251          webm       audio only DASH audio  161k , opus @160k, 4.14MiB
394          mp4        256x144    144p   95k , av01.0.05M.08, 25fps, video only, 2.41MiB
278          webm       256x144    144p   95k , webm container, vp9, 25fps, video only, 2.79MiB
160          mp4        256x144    144p  110k , avc1.4d400c, 25fps, video only, 2.06MiB
395          mp4        426x240    240p  208k , av01.0.05M.08, 25fps, video only, 4.71MiB
242          webm       426x240    240p  220k , vp9, 25fps, video only, 5.01MiB
133          mp4        426x240    240p  295k , avc1.4d4015, 25fps, video only, 5.03MiB
396          mp4        640x360    360p  364k , av01.0.05M.08, 25fps, video only, 8.06MiB
243          webm       640x360    360p  419k , vp9, 25fps, video only, 10.14MiB
134          mp4        640x360    360p  580k , avc1.4d401e, 25fps, video only, 10.25MiB
397          mp4        854x480    480p  623k , av01.0.05M.08, 25fps, video only, 13.27MiB
244          webm       854x480    480p  767k , vp9, 25fps, video only, 16.88MiB
135          mp4        854x480    480p  832k , avc1.4d401e, 25fps, video only, 14.78MiB
398          mp4        1280x720   720p 1183k , av01.0.05M.08, 25fps, video only, 24.68MiB
136          mp4        1280x720   720p 1212k , avc1.4d401f, 25fps, video only, 20.78MiB
247          webm       1280x720   720p 1472k , vp9, 25fps, video only, 23.73MiB
137          mp4        1920x1080  1080p 1641k , avc1.640028, 25fps, video only, 30.68MiB
248          webm       1920x1080  1080p 2108k , vp9, 25fps, video only, 30.32MiB
18           mp4        640x360    medium  547k , avc1.42001E, mp4a.40.2@ 96k (44100Hz), 16.74MiB (best)
Download eg 136 mp4 1280x720 720p 1212k.
G:\1_youtube
λ youtube-dl -f 136 https://www.youtube.com/watch?v=d8OI9FllKfg
[youtube] d8OI9FllKfg: Downloading webpage
[youtube] d8OI9FllKfg: Downloading video info webpage
[download] Destination: Komodo - (I Just) Died In Your Arms-d8OI9FllKfg.mp4
[download] 100% of 20.78MiB in 00:08
From a python script.
import youtube_dl

options = {
    'format': 'bestaudio/best',  
    'extractaudio': True,        
    'audioformat': "mp3",        
    'outtmpl': '%(id)s',         
    'noplaylist': True,          
    'listformats': True,   # print a list of the formats to stdout and exit
}

with youtube_dl.YoutubeDL(options) as ydl:
    ydl.download(['https://www.youtube.com/watch?v=d8OI9FllKfg'])
Output:
[youtube] d8OI9FllKfg: Downloading webpage [youtube] d8OI9FllKfg: Downloading video info webpage [youtube] Downloading just video d8OI9FllKfg because of --no-playlist [info] Available formats for d8OI9FllKfg: format code extension resolution note 249 webm audio only DASH audio 69k , opus @ 50k, 1.58MiB 250 webm audio only DASH audio 90k , opus @ 70k, 2.09MiB 140 m4a audio only DASH audio 127k , m4a_dash container, mp4a.40.2@128k, 3.89MiB 171 webm audio only DASH audio 142k , vorbis@128k, 3.82MiB 251 webm audio only DASH audio 161k , opus @160k, 4.14MiB 394 mp4 256x144 144p 95k , av01.0.05M.08, 25fps, video only, 2.41MiB 278 webm 256x144 144p 95k , webm container, vp9, 25fps, video only, 2.79MiB 160 mp4 256x144 144p 110k , avc1.4d400c, 25fps, video only, 2.06MiB 395 mp4 426x240 240p 208k , av01.0.05M.08, 25fps, video only, 4.71MiB 242 webm 426x240 240p 220k , vp9, 25fps, video only, 5.01MiB 133 mp4 426x240 240p 295k , avc1.4d4015, 25fps, video only, 5.03MiB 396 mp4 640x360 360p 364k , av01.0.05M.08, 25fps, video only, 8.06MiB 243 webm 640x360 360p 419k , vp9, 25fps, video only, 10.14MiB 134 mp4 640x360 360p 580k , avc1.4d401e, 25fps, video only, 10.25MiB 397 mp4 854x480 480p 623k , av01.0.05M.08, 25fps, video only, 13.27MiB 244 webm 854x480 480p 767k , vp9, 25fps, video only, 16.88MiB 135 mp4 854x480 480p 832k , avc1.4d401e, 25fps, video only, 14.78MiB 398 mp4 1280x720 720p 1183k , av01.0.05M.08, 25fps, video only, 24.68MiB 136 mp4 1280x720 720p 1212k , avc1.4d401f, 25fps, video only, 20.78MiB 247 webm 1280x720 720p 1472k , vp9, 25fps, video only, 23.73MiB 137 mp4 1920x1080 1080p 1641k , avc1.640028, 25fps, video only, 30.68MiB 248 webm 1920x1080 1080p 2108k , vp9, 25fps, video only, 30.32MiB 18 mp4 640x360 medium 547k , avc1.42001E, mp4a.40.2@ 96k (44100Hz), 16.74MiB (best)
Reply
#9
I have one on github (using PyQt5)

I use it in Linux.

[Image: 37687008-2e107b86-2c9a-11e8-86a7-bc255857c989.png]
Reply
#10
Thanks a lot, snippsat, for your kind answer.
Further info needs. Which command I've to add to download one single file .mp4 coming from this choice?
Output:
22 mp4 1280x720 hd720 347k , avc1.64001F, mp4a.40.2@192k (44100Hz) (best)
# -------- YT.py -----------
import youtube_dl
 
options = {
    'format': 'bestaudio/best',  
    'extractaudio': True,        
    'audioformat': "mp3",        
    'outtmpl': '%(id)s',         
    'noplaylist': True,          
    'listformats': True,   # print a list of the formats to stdout and exit
}
 
with youtube_dl.YoutubeDL(options) as ydl:
    ydl.download(['https://www.youtube.com/watch?v=zPMr0lEMqpo'])

# ----- OUTPUT -------
#C:\Training>python YT.py
#[youtube] zPMr0lEMqpo: Downloading webpage
#[youtube] zPMr0lEMqpo: Downloading video info webpage
#[youtube] Downloading just video zPMr0lEMqpo because of --no-playlist
#[info] Available formats for zPMr0lEMqpo:
#format code  extension  resolution note
#249          webm       audio only DASH audio   49k , opus @ 50k, 1.07MiB
#250          webm       audio only DASH audio   54k , opus @ 70k, 1.21MiB
#171          webm       audio only DASH audio   88k , vorbis@128k, 2.03MiB
#251          webm       audio only DASH audio  100k , opus @160k, 2.21MiB
#140          m4a        audio only DASH audio  127k , m4a_dash container, mp4a.40.2@128k, 3.08MiB
#160          mp4        256x144    144p   38k , avc1.4d400c, 30fps, video only, 421.68KiB
#278          webm       256x144    144p   84k , webm container, vp9, 30fps, video only, 987.35KiB
#133          mp4        426x240    240p   98k , avc1.4d4015, 30fps, video only, 776.98KiB
#242          webm       426x240    240p  141k , vp9, 30fps, video only, 1.39MiB
#243          webm       640x360    360p  264k , vp9, 30fps, video only, 2.60MiB
#134          mp4        640x360    360p  264k , avc1.4d401e, 30fps, video only, 1.70MiB
#135          mp4        854x480    480p  474k , avc1.4d401f, 30fps, video only, 3.02MiB
#244          webm       854x480    480p  481k , vp9, 30fps, video only, 4.13MiB
#136          mp4        1280x720   720p  858k , avc1.4d401f, 30fps, video only, 5.35MiB
#247          webm       1280x720   720p  915k , vp9, 30fps, video only, 8.75MiB
#43           webm       640x360    medium , vp8.0, vorbis@128k, 6.16MiB
#18           mp4        640x360    medium  306k , avc1.42001E, mp4a.40.2@ 96k (44100Hz), 7.44MiB
#22           mp4        1280x720   hd720  347k , avc1.64001F, mp4a.40.2@192k (44100Hz) (best)
# ------------------------------------------------------------------------------------------------
Edit. Just solved.
Output:
C:\Training>youtube-dl -f 22 https://www.youtube.com/watch?v=zPMr0lEMqpo [youtube] zPMr0lEMqpo: Downloading webpage [youtube] zPMr0lEMqpo: Downloading video info webpage [download] Destination: How to install pip on Windows (The easy way)-zPMr0lEMqpo.mp4 [download] 100% of 8.41MiB in 00:02
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python multiprocessing to download sql table mg24 5 1,407 Oct-31-2022, 03:53 PM
Last Post: Larz60+
  download with internet download manager coral_raha 0 2,880 Jul-18-2021, 03:11 PM
Last Post: coral_raha
  How to make a bot in pycharm for youtube for automatically adding python comments kodek2222 1 2,027 Jan-21-2021, 12:47 PM
Last Post: Aspire2Inspire
  download pubmed PDFs using pubmed2pdf in python Wooki 8 5,369 Oct-19-2020, 03:06 PM
Last Post: jefsummers
  How can I download Python files from GitHub? bitcoin10mil 2 2,767 Aug-26-2020, 09:03 PM
Last Post: Axel_Erfurt
  win32 API: Launch Application to RDP session from background process python script rangeshgupta 0 2,091 May-28-2020, 09:41 PM
Last Post: rangeshgupta
Big Grin python download manager with progressbar (not gui) ghostblade 1 1,892 Apr-23-2020, 11:05 AM
Last Post: snippsat
  Python Download GillietheSquid 2 1,997 Mar-27-2020, 09:15 PM
Last Post: GillietheSquid
  How to check if video has been deleted or removed in youtube using python Prince_Bhatia 14 11,581 Feb-21-2020, 04:33 AM
Last Post: jehoshua
  How to background another process in Python? Kalet 2 2,463 Oct-21-2019, 05:17 AM
Last Post: newbieAuggie2019

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020