Friday, April 6, 2018

Python - download youtube videos

To download youtube videos, we need to install the module: youtube-dl

$ sudo pip install youtube-dl

Check File Types: To check out the filetypes available for downloading, we use the following command:

$ youtube-dl -F https://www.youtube.com/watch?v=2eumjKINo6w
WARNING: Assuming --restrict-filenames since file system encoding cannot encode all characters. Set the LC_ALL environment variable to fix this.
[youtube] 2eumjKINo6w: Downloading webpage
[youtube] 2eumjKINo6w: Downloading video info webpage
[youtube] 2eumjKINo6w: Extracting video information
WARNING: unable to extract uploader nickname
[info] Available formats for 2eumjKINo6w:
format code  extension  resolution note
249          webm       audio only DASH audio   51k , opus @ 50k, 3.56MiB
250          webm       audio only DASH audio   55k , opus @ 70k, 4.06MiB
171          webm       audio only DASH audio   89k , vorbis@128k, 6.32MiB
251          webm       audio only DASH audio  127k , opus @160k, 9.37MiB
140          m4a        audio only DASH audio  128k , m4a_dash container, mp4a.40.2@128k, 9.66MiB
278          webm       256x144    144p   96k , webm container, vp9, 15fps, video only, 6.22MiB
160          mp4        256x144    144p  117k , avc1.42c00c, 15fps, video only, 8.63MiB
242          webm       426x240    240p  216k , vp9, 30fps, video only, 8.97MiB
133          mp4        426x240    240p  248k , avc1.4d4015, 30fps, video only, 18.57MiB
243          webm       640x360    360p  417k , vp9, 30fps, video only, 18.09MiB
134          mp4        640x360    360p  534k , avc1.4d401e, 30fps, video only, 23.52MiB
244          webm       854x480    480p  758k , vp9, 30fps, video only, 32.44MiB
135          mp4        854x480    480p 1082k , avc1.4d401f, 30fps, video only, 49.89MiB
247          webm       1280x720   720p 1455k , vp9, 30fps, video only, 74.34MiB
136          mp4        1280x720   720p 2065k , avc1.4d401f, 30fps, video only, 99.03MiB
248          webm       1920x1080  1080p 2791k , vp9, 30fps, video only, 170.39MiB
137          mp4        1920x1080  1080p 4141k , avc1.640028, 30fps, video only, 208.59MiB
17           3gp        176x144    small , mp4v.20.3, mp4a.40.2@ 24k
36           3gp        320x180    small , mp4v.20.3, mp4a.40.2
43           webm       640x360    medium , vp8.0, vorbis@128k
18           mp4        640x360    medium , avc1.42001E, mp4a.40.2@ 96k
22           mp4        1280x720   hd720 , avc1.64001F, mp4a.40.2@192k (best)
 $


Download video of a particular filetype:

$ youtube-dl -f 18 https://www.youtube.com/watch?v=2eumjKINo6w
WARNING: Assuming --restrict-filenames since file system encoding cannot encode all characters. Set the LC_ALL environment variable to fix this.
[youtube] 2eumjKINo6w: Downloading webpage
[youtube] 2eumjKINo6w: Downloading video info webpage
[youtube] 2eumjKINo6w: Extracting video information
WARNING: unable to extract uploader nickname
[download] Destination: The_Story_of_Integration_2_of_4_-_Riemann_s_Integral-2eumjKINo6w.mp4
[download] 100% of 30.23MiB in 00:01
 $ 

Notice, the -f flag used.

Downloading the entire playlist can be done by using the below command:

$ youtube-dl -f 18 -o '%(playlist_index)s. %(title)s.%(ext)s'  https://www.youtube.com/playlist?list=PLrsBhRPS9dWHGfBTLJcmeopwjbTCn6cUe
WARNING: Assuming --restrict-filenames since file system encoding cannot encode all characters. Set the LC_ALL environment variable to fix this.
[youtube:playlist] PLrsBhRPS9dWHGfBTLJcmeopwjbTCn6cUe: Downloading webpage
[download] Downloading playlist: Khan Academy Linear Algebra | Vectors and spaces
[youtube:playlist] playlist Khan Academy Linear Algebra | Vectors and spaces: Downloading 41 videos
[download] Downloading video 1 of 41
[youtube] br7tS1t2SFE: Downloading webpage
[youtube] br7tS1t2SFE: Downloading video info webpage
[youtube] br7tS1t2SFE: Extracting video information
WARNING: unable to extract uploader nickname
[download] Destination: 01. Vector_intro_for_linear_algebra_Vectors_and_spaces_Linear_Algebra_Khan_Academy.mp4
[download] 100% of 4.44MiB in 00:00
[download] Downloading video 2 of 41
[youtube] lCsjJbZHhHU: Downloading webpage
[youtube] lCsjJbZHhHU: Downloading video info webpage
[youtube] lCsjJbZHhHU: Extracting video information
WARNING: unable to extract uploader nickname
[download] Destination: 02. Real_coordinate_spaces_Vectors_and_spaces_Linear_Algebra_Khan_Academy.mp4


Notice here, the -o flag used.

No comments:

Post a Comment