Download YouTube movies and Auto-Number Them

From dftwiki3
Jump to: navigation, search

--D. Thiebaut (talk) 10:15, 23 March 2014 (EDT)



YouTube-dl


This script requires youtube-dl, which can be downloaded from GitHub.

Source


#! /usr/bin/env python
# getYouTubeVids.py
# (C) D. Thiebaut 2014
# given a list of youtube videos stored in the array vids, and a 
# filename variable that represents the name of the videos, including
# a numbering scheme (%02d means 2 decimals with leading 0, i.e. 01,
# 02, etc., the program will fetch the videos using the command
# youtube-dl (to be downloaded from some other site) and will store
# all the movie files under the given filename format.
# Assumes Python V 2.7.  Switch the print statements around for
# Python 3+

import subprocess

#--- the name to be given to all the downloaded videos ---
filename = "Qt5Tutorials_Thiebaut_%02d"

#--- the video URLs.  Could also just use the last part of the URL, e.g. "EAdAvMc1MCI" ---
vids = [ "http://www.youtube.com/watch?v=EAdAvMc1MCI",
            "http://www.youtube.com/watch?v=XmzqZmr-S-Q" ]

#--- download each video and store it under the selected filename, ---
#--- and keep the original  extension. ---
for i,vid in enumerate(vids):
    thisFileName = filename % (i+1) + ".%(ext)s"
    #print( "youtube-dl -o \"" + thisFileName + "\" " + vid )
    print "youtube-dl -o \"" + thisFileName + "\" " + vid 
    #call( ["youtube-dl", "-o",  thisFileName, vid ] )
    subprocess.Popen( ["youtube-dl", "-o",  thisFileName, vid ], 
                      stdout = subprocess.PIPE, 
                      stderr= subprocess.PIPE ).communicate()
    #print( "done" )
    print "done"


Execution


In a terminal/console window, first make the script executable:

chmod a+x getYouTubeVids.py


Then download the videos

./getYouTubeVids.py