Using Command Line, Extract a Video Clip from YouTube
A shell script to download and extract a video clip from YouTube.

I watch videos on YouTube almost daily for various reasons, ranging from learning to entertainment. Sometimes I want to store or share a specific part of the video. Often I find myself opting for online YouTube downloaders, after that I search for another online resource to trim/cut out the portion(s) I need.
This process can be annoying, notwithstanding those third-party (YouTube downloader, video trimmer) sites are filled with ads (at-times very awkward…) and suspicious pop ups. If care is not taking, you might end up downloading your video clip alongside a malware or spyware or other malicious contents out there.
“There are more hackers breeding every day, and more brilliant minds are turning into hackers. Security has advanced, but so have hackers.”
Michael Demon Calce
Like many software developers out there, I love the Terminal (command line), it didn’t take long before I decided to write a script to do the job for me. It turned out be a piece of a cake, thanks to open source community.
There are plethora of developers contributing to open source platforms, kudos to our heroes (not all wear capes). If you are a developer, you can give back by either contributing to open source communities or supporting those who do (buy them a coffee… it goes a long way).
Tasks — Purpose of Script
- Download a YouTube video
- Cut out a clip from the video
To achieve the above tasks, I will be using two command-line programs which are youtube-dl and ffmpeg. Download and install them into your device… it is quite straightforward.
- Youtube-dl is a command-line program to download videos from youtube.com and a few more sites.
- FFmpeg is a complete, cross-platform solution to record, convert and stream audio and video.
Let’s download a video clip, I will use Steve Jobs famous 2005 Stanford Commencement Address… we all loved it.
“Don’t let the noise of others’ opinions drown out your own inner voice. And most important, have the courage to follow your heart and intuition. They somehow already know what you truly want to become. Everything else is secondary.”
Steve Jobs
Download a Video with Youtube-dl
To download a video, get the video id and run the below command. You can also specify the output format of the video.
$ youtube-dl --format mp4 UF8uR6Z6KLc
Once the above command is executed successfully, you should get the .mp4 file downloaded in the directory in which the command was executed. The file should be named something like “Steve Jobs’ 2005 Stanford Commencement Address-UF8uR6Z6KLc.mp4”.
Next, get a clip from the downloaded video file
The pinnacle of the speech for me was the part he spoke about when he dropped out of college, the way he coped with it, his interest calligraphy and how windows copied Mac… haha. I will cut out that section using ffmpeg, it’s between the 3rd and 6th minute.
$ ffmpeg -i steve-video.mp4 -ss 00:03:32 -to 00:05:32 steve-clip.mp4
Now that we have downloaded the YouTube video and cut out the section we need from it, let’s combine the above commands into a shell script.
Shell Script
Let’s name the file youtube-clip.sh. The script is as follow;
#!/bin/bash# youtube video id
vid="$1";echo "[youtube-dl] download video from youtube in .mp4 format...";
youtube-dl --format mp4 $vid -o '%(title)s.%(ext)s';if [ "$2" == --trim ]
then
# from which point to which point you want to trim
# start e.g. 00:03:32
# end e.g. 00:05:32 start="$3";
end="$4"; # get the name of the video file
file_name=`youtube-dl --get-filename -o '%(title)s' $vid`;
# file extension
ext="mp4"; # path to working directory
path=`pwd`;
# video clip name and path to store location
clip="$path/${file_name}_clip.${ext}"; echo "[ffmpeg] cutting out the section file you need";
ffmpeg -i "$path/$file_name.$ext" -ss $start -to $end "$clip";
fiecho "done...";
Awesome, we are ready. I added a conditional statement to the script. This will allow us to download, then decide if to trim the video file or not. Make sure you give youtube-clip.sh appropriate permission.
# make a file executable in Terminal on Mac
$ chmod 755 youtube-clip.sh
Now let’s execute youtube-clip.sh script on Terminal.
For downloading video without trimming, pass the video Id only.
$ ./youtube-clip.sh UF8uR6Z6KLc
For downloading and extracting clip from the video, add — trim then pass start and end time.
$ ./youtube-clip.sh UF8uR6Z6KLc --trim 00:03:32 00:05:32
Once you’ve executed the above command, you should see a file with similar name including _clip.mp4 in the same directory.
Note: You can adjust or re-write youtube-clip.sh script to work with audio (i.e. mp3 files). Likewise checkout other sites youtube-dl supports such as twitter, I will let you explore those options… good luck hacking.
“Your time is limited, so don’t waste it living someone else’s life.”
Steve Jobs
The above quote from Steve Jobs resonates with me, and when I think of him, I think of time and how limited it is… use your time wisely.
Reference
You can find the full script “ youtube-clip.sh” in my GitHub Gist.