Working with YouTube and Extracting Audio


In my previous couple of articles, I have been exploring the capabilities of ImageMagick, exhibiting that simply since you’re engaged on the command line doesn’t suggest you are simply processing textual content. As I defined, ImageMagick makes it simple to work with pictures, add watermarks, and analyze content material much more precisely than customary Linux.
file orders, and far more.

Equally, I wish to see the audio and video on this article. OK, perhaps “hear” to audio and “watch” to video, however once more, I am nonetheless targeted on the command line, so in each examples, participant/viewer apps are wanted.

YouTube to MP3 Audio

As somebody who watches numerous lectures on-line, I am additionally impressed with on-line companies that may extract solely the audio portion of a YouTube or Vimeo video and put it aside as an MP3. For instance, it’s far safer to hearken to a lecture whereas driving than to strive to not watch a video on the go.

Since there are such a lot of dwell 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.

Word: Pay attention to copyright points with any obtain and conversion of content material. Simply because it is on Vimeo, YouTube, or one other on-line service doesn’t suggest you’ve permission to extract the audio or obtain it and put it aside to your laptop.

Let’s begin with probably the most fundamental performance: downloading a video from YouTube so you’ll be able to watch it in your Linux system. There are many browser plugins and even web sites devoted to this job, however who desires to threat malware or be tormented by porn web site advertisements? Yeh.

Fortunately, there’s a nice public area program on GitHub known as youtube-dl that covers all of your wants. In its most simple type, it enables you to obtain video content material from YouTube and plenty of different on-line video repositories, however as you may be taught, it might probably do fairly a bit extra.

You possibly can seize a replica to your system right here.

Let’s begin by downloading a replica of considered one of my very own YouTube movies. It is a evaluation of the nice 1More quad-driver headphones, and the URL is https://www.youtube.com/watch?v=BFL1E77hTHQ.

Apart: I’ve a YouTube channel the place I evaluation client electronics and devices. You should subscribe! Watch all my movies at http://youtube.com/askdavetaylor.

YouTube has a number of methods to assemble URLs, nonetheless, together with utilizing its URL-shortener youtu.be, however fortunately, youtube-dl can deal with the variations.

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


youtube-dl 'https://www.youtube.com/watch?v=BFL1E77hTHQ'

All the output of the command is a bit, um, bushy, although:


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

You possibly can undergo the output messages, however it’s the message from fellow open-source program ffmpeg that issues probably the most:
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 widespread Matroska multimedia container format, and it really works with numerous video gamers (together with VideoLAN, aka VLC, my favourite cross-platform video participant).

a quick ls The consequence states and the default filename is derived from the title of the video, one thing that is probably not significantly 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 like to specify the output title and have the output file in MP4 (MPEG4) format as an alternative? That is doable:


$ youtube-dl -o 1more-review.mp4 -f mp4 
    'https://www.youtube.com/watch?v=BFL1E77hTHQ'
[youtube] BFL1E77hTHQ: Downloading webpage
[youtube] BFL1E77hTHQ: Downloading video data webpage
[youtube] BFL1E77hTHQ: Extracting video info
[youtube] BFL1E77hTHQ: Downloading MPD manifest
[download] Vacation spot: 1more-review.mp4
[download] 100% of 57.63MiB in 00:27

As a bonus, you additionally get much less ominous informational messages from this system, 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 simply 58M versus the MKV-merged model’s 124M.

So how do you see it? More than likely, a double-click and it is going to be up and operating, as proven in Determine 1.

Determine 1. Downloaded YouTube video enjoying in Ubuntu Participant

It is easy sufficient, however the unique aim was to have the ability to extract solely the audio part of a YouTube video, so let’s take a look at that working.

Downloading Simply the Audio Monitor

Since I’ve began delving into command-line choices for the youtube-dl program, it is no huge deal to search out out that there is one other command-line choice that allows you to save solely the audio portion of a video:


$ youtube-dl -x --audio-format mp3 
    'https://www.youtube.com/watch?v=BFL1E77hTHQ'
[youtube] BFL1E77hTHQ: Downloading webpage
[youtube] BFL1E77hTHQ: Downloading video data webpage
[youtube] BFL1E77hTHQ: Extracting video info
[youtube] BFL1E77hTHQ: Downloading MPD manifest
[download] Vacation spot: 1More Quad Driver In-Ear Headphones
Reviewed-BFL1E77hTHQ.webm
[download] 100% of 4.81MiB in 00:07
[ffmpeg] Vacation spot: 1More Quad Driver In-Ear Headphones
Reviewed-BFL1E77hTHQ.mp3
Deleting unique file 1More Quad Driver In-Ear Headphones
Reviewed-BFL1E77hTHQ.webm (go -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

It is easy sufficient, and the output is delightfully small: 4MB complete. The issue is, identical bizarre naming challenge, so as well as -o
output-filename
Will certainly win. However, actually, so long as you are keen to discover all of its command-line choices, youtube-dl makes these duties loads simpler.

writing a wrapper script

As an alternative of worrying about obscure command-line flag notation, let’s simply write a script that does the heavy lifting for you. I will name it ytdl for “youtube obtain”, and by default, it is going to solely settle for a URL and output an MP4 format video file that has the identical title because the YouTube shortcut (for instance, the above video would turn out to be BFL1E77hTHQ .mp4).

Add a second parameter, and that turns into the output file title. give particulars
-a flag, and it saves audio output in mp3 format solely.

Let’s begin with the utilizing block in case the consumer 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
fi

It is fairly simple. The script can also be going to make use of some predefined mixtures of flags to make it simpler to write down:


youtubedl="/usr/native/bin/youtube-dl"
audioflags="-x --audio-format mp3"
videoflags="-f mp4"
flags=$videoflags       # default set of command flags
audioonly=0             # default is audio + video

If the consumer specifies -a flag,
audioonly shall be set to true (ie, 1), and the default flag will change from video to audio:


if [ "$1" = "-a" ] ; then
  audioonly=1
  flags=$audioflags
  shift
fi

you’ll do not forget that shift The command strikes all parameters “down” one to the left, so $2 It occurs
$1 And so forth. It is actually a easy strategy to course of and skip parameters in a script.

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


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

This isn’t probably the most strong code, because it assumes that the required URL is in a format like the instance used right here,
youtube-yadda-yadaa?worth=shortcode, This removes the shortcode and easily provides an applicable filename suffix. There are higher methods to do that, however that is okay, it is going to work for now. Simply understand that in case your YouTube URL or one other web site’s URL is of a really totally different kind, your output format could also be a bit odd.

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


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

this a lot solely! Now you’ll be able to obtain a video like this:


$ ytdl 'https://www.youtube.com/watch?v=5yXDzg_QDGw' wiper.mp4

And an audio half with it:


$ ytdl -a 'https://www.youtube.com/watch?v=5yXDzg_QDGw'

effectively, eh?

I handed my spot for this column, however it’s such a enjoyable and easy script on high of an superior, highly effective program, that it is value it, is not it? And now you know the way to make YouTube give you the results you want, and never vice versa!



Supply hyperlink