Working with YouTube and Extracting Audio

In my previous few articles, I have been exploring the capabilities of ImageMagick,
displaying that simply since you’re engaged on a command line
does not imply you are caught processing solely textual content. As I defined,
ImageMagick makes it straightforward to work
with photographs, including
watermarks and analyzing
content material way more precisely than with the usual Linux
file command,
and far, far more.

Persevering with in an analogous vein, I need to have a look at audio and video on this
Properly, possibly “pay attention” to audio and “look” at video, however
once more, I am nonetheless specializing in the command line, so in each cases,
participant/viewer apps are required.

YouTube to MP3 Audio

As somebody who watches loads of lectures on-line, I am additionally intrigued by
the net companies that may extract simply the audio portion of a YouTube or
Vimeo video and put it aside as an MP3. Listening to a lecture whereas driving is
far safer than attempting to not watch a video on the transfer, for instance.

Since there are such a lot of reside live performance performances on-line, many individuals additionally
like to make use of a video-to-MP3 service so as to add these songs to their music libraries.

Notice: be leery of copyright points with any obtain and conversion of content material.
Simply because it is on Vimeo, YouTube or different on-line service,
does not imply you may have permission to extract the audio and even obtain
it and put it aside in your pc.

Let’s begin with essentially the most primary performance: downloading a video from
YouTube so you possibly can watch it in your Linux system. There are loads of
browser plugins and even web sites dedicated to this process, however who needs to
threat malware or be suffering from porn web site adverts? Yech.

Luckily, there is a terrific public area program referred to as youtube-dl
on GitHub that covers all of your wants. At its most elementary, it helps you to
obtain video content material from YouTube and a wide range of different on-line video
repositories, however as you may be taught, it will probably do fairly a bit extra.

You may seize a replica on your system
right here.

Let’s begin by downloading a replica of certainly one of my very own YouTube movies.
It is a evaluation of the luxurious 1More quad-driver headphones, and its URL

As an apart: I’ve a YouTube channel the place I evaluation shopper electronics
and devices. It’s best to subscribe! Discover all my movies at

YouTube has a bunch of the way it will probably assemble a URL, nonetheless, together with utilizing
its URL-shortener, however fortuitously, youtube-dl can deal with the

Downloading a replica of the video to the present working listing is now as
easy as:

youtube-dl ''

The complete output of the command is a bit, um, furry, nonetheless:

$  youtube-dl ''
[youtube] BFL1E77hTHQ: Downloading webpage
[youtube] BFL1E77hTHQ: Downloading video data webpage
[youtube] BFL1E77hTHQ: Extracting video data
[youtube] BFL1E77hTHQ: Downloading MPD manifest
WARNING: Requested codecs are incompatible for merge and
will probably be merged into mkv.
[download] Vacation spot: 1More Quad Driver In-Ear Headphones
[download] 100% of 118.74MiB in 02:49
[download] Vacation spot: 1More Quad Driver In-Ear Headphones
[download] 100% of 4.81MiB in 00:03
[ffmpeg] Merging codecs into "1More Quad Driver In-Ear
Headphones Reviewed-BFL1E77hTHQ.mkv"
Deleting authentic file 1More Quad Driver In-Ear Headphones
Reviewed-BFL1E77hTHQ.f137.mp4 (cross -k to maintain)
Deleting authentic file 1More Quad Driver In-Ear Headphones
Reviewed-BFL1E77hTHQ.f251.webm (cross -k to maintain)

You may wade by means of the output messages, however it’s the message from
companion open-source program ffmpeg that is most essential:
merging codecs into ... mkv.

In different phrases, the obtain format of the video is MKV by default. MKV is
a part of the more and more in style Matroska Multimedia Container format, and
it really works with loads of video gamers (together with VideoLan, aka VLC, my favourite
cross-platform video participant).

A fast ls reveals the consequence and that the default filename is taken from
the title of the video, one thing which may not be notably fascinating:

$ ls -lh *mkv
-rw-r--r--  1 taylor  workers   124M Jan 31 16:56 1More Quad
Driver In-Ear Headphones Reviewed-BFL1E77hTHQ.mkv

Do you favor to specify the output identify and have the output file in MP4 (MPEG4)
format as an alternative? That is doable:

$ youtube-dl -o 1more-review.mp4 -f mp4 
[youtube] BFL1E77hTHQ: Downloading webpage
[youtube] BFL1E77hTHQ: Downloading video data webpage
[youtube] BFL1E77hTHQ: Extracting video data
[youtube] BFL1E77hTHQ: Downloading MPD manifest
[download] Vacation spot: 1more-review.mp4
[download] 100% of 57.63MiB in 00:27

As a bonus, you get much less ominous informational messages from this system
too, so it is cleaner. And the output, certain sufficient, is in MP4 format:

$ ls -lh *mp4
[email protected] 1 taylor  workers  58M Jan 31 16:57 1more-review.mp4

As a second bonus, it is also extra environment friendly in its video encoding, so
the MP4 model of the downloaded video is just 58M versus the 124M
of the MKV-merged model.

So how do you watch it? Probably, do a double-click and it will be up and
operating, as proven in Determine 1.

Determine 1. Downloaded YouTube Video Enjoying in Ubuntu Participant

That is straightforward sufficient, however the authentic aim was to have the ability to extract simply the audio
element of a YouTube video, so let us take a look at that process.

Downloading Simply the Audio Observe

Since I’ve already began to delve into the command-line choices for
the youtube-dl program, it isn’t a leap to seek out out that there is
yet one more command-line choice that allows you to save simply the audio portion
of a video:

$ youtube-dl -x --audio-format mp3 
[youtube] BFL1E77hTHQ: Downloading webpage
[youtube] BFL1E77hTHQ: Downloading video data webpage
[youtube] BFL1E77hTHQ: Extracting video data
[youtube] BFL1E77hTHQ: Downloading MPD manifest
[download] Vacation spot: 1More Quad Driver In-Ear Headphones
[download] 100% of 4.81MiB in 00:07
[ffmpeg] Vacation spot: 1More Quad Driver In-Ear Headphones
Deleting authentic file 1More Quad Driver In-Ear Headphones
Reviewed-BFL1E77hTHQ.webm (cross -k to maintain)
$ ls -lh *mp3
-rw-r--r--  1 taylor  workers   4.0M Jan 31 18:22 1More Quad
Driver In-Ear Headphones Reviewed-BFL1E77hTHQ.mp3

That is straightforward sufficient, and the output is delightfully small: 4MB whole. The issue is,
there’s the identical awkward naming situation, so the addition of -o
positively will probably be a win. However, actually, youtube-dl makes
these duties trivially straightforward, so long as you are prepared to determine all
of its command-line choices.

Writing a Wrapper Script

As a substitute of worrying concerning the obscure command-line flag notation, let’s
simply write a script that does the heavy lifting for you. I’ll
name it ytdl for “youtube obtain”, and by default, it will settle for
only a URL and output an MP4 format video file that has the identical identify as
the YouTube shortcut (for instance, the above video would grow to be BFL1E77hTHQ.mp4).

Add a second parameter, and that turns into the output filename. Specify the
-a flag, and it saves audio output solely, in MP3 format as an alternative.

Let’s begin with a utilization block if the person forgets to specify something
or simply wants a easy reminder:

if [ $# -eq 0 ] ; then
  echo "Utilization: $(basename $0) {-a} YouTubeURL {outputfile}"
  echo "   the place -a extracts the audio portion in MP3 format"
  exit 1

That is straightforward sufficient. The script can also be going to make use of some predefined mixtures
of flags to make it simpler to write down:

audioflags="-x --audio-format mp3"
videoflags="-f mp4"
flags=$videoflags       # default set of command flags
audioonly=0             # default is audio + video

If the person specifies the -a flag,
audioonly will probably be set to true (that’s, 1),
and the default flags will swap from video to audio:

if [ "$1" = "-a" ] ; then

You will recall that the shift command strikes all of the parameters
“down” one to the left, so $2 turns into
$1 and so forth. It is a simple option to
course of and discard parameters in a script, in fact.

The most important block of code creates a default output filename from the
YouTube URL:

if [ $# -eq 1 ] ; then
  # no output filename specified
  outfile=$(echo "$1" | minimize -d= -f2)
  if [ $audioonly -eq 1 ] ; then

This is not essentially the most sturdy code, as a result of it assumes that the URL
specified is in a format just like the examples used herein,
youtube-yadda-yadaa?worth=shortcode. It extracts the shortcode and easily
appends an acceptable filename suffix. There are higher methods to do that,
however that is okay, this’ll work for now. Simply understand that your
output format may be a bit bizarre if in case you have a really totally different sort of
YouTube URL or a URL from one other web site.

And, lastly, the precise invocation of the
youtube-dl command:

$youtubedl $flags -o "$outfile" "$1"

That is it! Now you possibly can obtain a video as merely as:

$ ytdl '' wiper.mp4

And an audio portion with:

$ ytdl -a ''

Good, eh?

I’ve approach overrun my house for this column, however that is such a enjoyable and
easy script atop a terrific, highly effective program, that it is value it,
proper? And now you understand how to make YouTube give you the results you want, relatively than
vice versa!

Supply hyperlink