Using Command Line, Extract a Video Clip from YouTube

Suleiman
Suleiman
Jul 8, 2020 · 4 min read

A shell script to download and extract a video clip from YouTube.

Image for post
Image for post
You still want to hack facebook? good luck — Photo by Suleiman

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).

  1. Download a YouTube video
  2. 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

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”.

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.

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";
fi
echo "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.

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

By Dev Genius

The best stories sent monthly to your email. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Suleiman

Written by

Suleiman

Software engineer, runner, hiker, fitness enthusiast. https://namieluss.com

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Suleiman

Written by

Suleiman

Software engineer, runner, hiker, fitness enthusiast. https://namieluss.com

Dev Genius

Coding, Tutorials, News, UX, UI and much more related to development

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store