How to download YouTube videos and clip them with cli tools


Lets have some fun downloading YouTube videos with youtube-dl and clipping them with ffmpeg using the command line.

Install the tools

Download a YouTube video using youtube-dl

First lets list the available formats for the video we want to download:

youtube-dl -F https://www.youtube.com/watch?v=VIDEO_ID

You should see output that resembles the following:

format code  extension  resolution note
249          webm       audio only tiny   50k , webm_dash container, opus  (48000Hz), 19.76MiB
250          webm       audio only tiny   70k , webm_dash container, opus  (48000Hz), 27.74MiB
251          webm       audio only tiny  126k , webm_dash container, opus  (48000Hz), 49.89MiB
140          m4a        audio only tiny  129k , m4a_dash container, mp4a.40.2 (44100Hz), 51.00MiB
160          mp4        256x144    144p   18k , mp4_dash container, avc1.4d400c, 30fps, video only, 7.40MiB
278          webm       256x144    144p   39k , webm_dash container, vp9, 30fps, video only, 15.38MiB
133          mp4        426x240    240p   35k , mp4_dash container, avc1.4d4015, 30fps, video only, 13.97MiB
242          webm       426x240    240p   59k , webm_dash container, vp9, 30fps, video only, 23.62MiB
134          mp4        640x360    360p   62k , mp4_dash container, avc1.4d401e, 30fps, video only, 24.47MiB
243          webm       640x360    360p  126k , webm_dash container, vp9, 30fps, video only, 49.89MiB
135          mp4        854x480    480p   88k , mp4_dash container, avc1.4d401f, 30fps, video only, 34.88MiB
244          webm       854x480    480p  178k , webm_dash container, vp9, 30fps, video only, 70.12MiB
136          mp4        1280x720   720p  217k , mp4_dash container, avc1.4d401f, 30fps, video only, 85.87MiB
247          webm       1280x720   720p  297k , webm_dash container, vp9, 30fps, video only, 117.29MiB
298          mp4        1280x720   720p60  314k , mp4_dash container, avc1.4d4020, 60fps, video only, 124.07MiB
302          webm       1280x720   720p60  394k , webm_dash container, vp9, 60fps, video only, 155.25MiB
299          mp4        1920x1080  1080p60  536k , mp4_dash container, avc1.64002a, 60fps, video only, 211.19MiB
303          webm       1920x1080  1080p60  663k , webm_dash container, vp9, 60fps, video only, 261.48MiB
18           mp4        640x360    360p  297k , avc1.42001E, 30fps, mp4a.40.2 (44100Hz), 117.28MiB
22           mp4        1280x720   720p  346k , avc1.64001F, 30fps, mp4a.40.2 (44100Hz) (best)

As you can see, YouTube has many formats for a video. In order to download a specific format we can pass in the -f flag with the format code from the list we just printed. So if we want the mp4 format at 640x360 resolution, we can use the format code 18 from the list to get that. Lets run the following:

youtube-dl -f 18 https://www.youtube.com/watch?v=VIDEO_ID

Ok great. Now we have a video downloaded. Lets clip it using ffmpeg. Lets say we want to clip the video from 10 seconds into the video to 20 seconds. We can run:

ffmpeg -i downloaded-video.mp4 -ss 00:00:10 -to 00:00:20 clipped-video-version.mp4

This will give us a 10 second clip that starts at 10 seconds into the video and ends at 20 seconds.

Now go destroy people on social media with video evidence of them being hypocrites on YouTube. Have fun! :)