Using FFmpeg and youtube-dl.

posted in: 2160p, ffmpeg, youtube, youtube-dl | 0

FFmpeg — A complete, cross-platform solution to record, convert and stream audio and video.
youtube-dl is a small command-line program to download videos from YouTube.com and a few more sites.
WebM Encoding Tutorial
High Quality Audio
HEVC encoding guide
FFmpeg AAC
x265 API Documentation
FFmpeg MP3 Encoding Guide

Let’s start with something simple – Grabbing information from a youtube video.
Here is a link: https://www.youtube.com/watch?v=k_okcNVZqqI
** Be aware that some commands roll off to the next line**

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

[youtube] k_okcNVZqqI: Downloading webpage
[youtube] k_okcNVZqqI: Extracting video information
[youtube] k_okcNVZqqI: Downloading DASH manifest
[info] Available formats for k_okcNVZqqI:
format code extension resolution note
140 m4a audio only DASH audio 129k , m4a_dash container, aac @128k (44100Hz), 2.83MiB
171 webm audio only DASH audio 138k , audio@128k (44100Hz), 2.78MiB
141 m4a audio only DASH audio 255k , m4a_dash container, aac @256k (44100Hz), 5.61MiB
160 mp4 256x144 DASH video 111k , 12fps, video only, 2.40MiB
278 webm 256x144 DASH video 123k , webm container, VP9, 1fps, video only, 1.97MiB
242 webm 426x240 DASH video 212k , 1fps, video only, 3.87MiB
133 mp4 426x240 DASH video 247k , 24fps, video only, 5.38MiB
243 webm 640x360 DASH video 424k , 1fps, video only, 7.61MiB
134 mp4 640x360 DASH video 606k , 24fps, video only, 9.52MiB
244 webm 854x480 DASH video 808k , 1fps, video only, 15.05MiB
135 mp4 854x480 DASH video 1144k , 24fps, video only, 18.75MiB
247 webm 1280x720 DASH video 1817k , 1fps, video only, 31.55MiB
136 mp4 1280x720 DASH video 2246k , 24fps, video only, 35.31MiB
248 webm 1920x1080 DASH video 2699k , 1fps, video only, 48.12MiB
137 mp4 1920x1080 DASH video 4173k , 24fps, video only, 66.06MiB
264 mp4 2560x1440 DASH video 10076k , 24fps, video only, 151.95MiB
271 webm 2560x1440 DASH video 17159k , 1fps, video only, 180.31MiB
266 mp4 3840x2160 DASH video 22295k , h264, 24fps, video only, 385.72MiB
138 mp4 3840x2160 DASH video 24157k , 24fps, video only, 283.43MiB
272 webm 3840x2160 DASH video 34452k , 1fps, video only, 331.97MiB
13 3gp unknown
17 3gp 176x144
36 3gp 320x240
5 flv 400x240
43 webm 640x360
18 mp4 640x360
22 mp4 1280x720 (best)

From the paste above we can see several versions of that youtube video page. Format code, extension of the files available and resolution. You can get a video, maybe you’re just after the sound and that is possible as well.

This is how you get the title.
youtube-dl --get-title https://www.youtube.com/watch?v=k_okcNVZqqI
INK DROPS 4K (ULTRA HD)

Grabbing audio only.

Suppose you are after a song in that video. Look at the first 3 lines of that information above. ^^^ There is .m4a(128k), .webm(128k), .m4a(256k)-more quality.
Grab option 3, format code 141(audio only DASH audio 255k , m4a_dash container, aac @256k (44100Hz), 5.61MiB).
youtube-dl -f 141 https://www.youtube.com/watch?v=k_okcNVZqqI

[youtube] k_okcNVZqqI: Downloading webpage
[youtube] k_okcNVZqqI: Extracting video information
[youtube] k_okcNVZqqI: Downloading DASH manifest
[download] Destination: INK DROPS 4K (ULTRA HD)-k_okcNVZqqI.m4a
[download] 100% of 5.61MiB in 00:00
[ffmpeg] Correcting container in "INK DROPS 4K (ULTRA HD)-k_okcNVZqqI.m4a"

You should now have a INK DROPS 4K (ULTRA HD)-k_okcNVZqqI.m4a in the same folder as youtube-dl.
It is probably playable by most OS or players.
Here is another way of converting the m4a to another format just incase.
ffmpeg -i "INK DROPS 4K (ULTRA HD)-k_okcNVZqqI.m4a" -c:a libmp3lame -b:a 256k INK_DROPS.mp3
 
Another format, for science.
ffmpeg -i "INK DROPS 4K (ULTRA HD)-k_okcNVZqqI.m4a" -c:a libvorbis -b:a 256k INK_DROPS.ogg

Grabbing video only.

Now let us do the same thing for video. The last line on that list above shows a 720p version that is playable with sound.
youtube-dl -f 22 https://www.youtube.com/watch?v=k_okcNVZqqI

[youtube] k_okcNVZqqI: Downloading webpage
[youtube] k_okcNVZqqI: Extracting video information
[youtube] k_okcNVZqqI: Downloading DASH manifest
[download] Destination: INK DROPS 4K (ULTRA HD)-k_okcNVZqqI.mp4
[download] 100% of 56.25MiB in 00:17

Again, like the audio before, the video should be playable on most OS and players. It should also have audio playing along.
 
——————————————————————————————————————————–
Maybe now you want to grab the file with a high resolution and quality. Format code 248 and 137 look good but they are Dash Video only, no sound.
Let’s grab 137 first since it is playable via most OS/players.
** Watch out for filename conflicts, youtube-dl will think you got it already **

youtube-dl -f 137 https://www.youtube.com/watch?v=k_okcNVZqqI

[youtube] k_okcNVZqqI: Downloading webpage
[youtube] k_okcNVZqqI: Extracting video information
[youtube] k_okcNVZqqI: Downloading DASH manifest
[download] Destination: INK DROPS 4K (ULTRA HD)-k_okcNVZqqI.mp4
[download] 100% of 66.06MiB in 00:12

What about the webm?
WebM is Google’s VP9 Codec for youtube, it’s tough to handle with most stock OS setup and players(VLC-2.2.1).
Grab it regardless, for science.
youtube-dl -f 248 https://www.youtube.com/watch?v=k_okcNVZqqI

[youtube] k_okcNVZqqI: Downloading webpage
[youtube] k_okcNVZqqI: Extracting video information
[youtube] k_okcNVZqqI: Downloading DASH manifest
[download] Destination: INK DROPS 4K (ULTRA HD)-k_okcNVZqqI.webm
[download] 100% of 48.12MiB in 00:07

——————————————————————————————————————————–

Now, you have two good video files with no sound. FFmpeg comes through now.
Here’s a quick command to join video & audio together.
ffmpeg -i "INK DROPS 4K (ULTRA HD)-k_okcNVZqqI.mp4" -i "INK DROPS 4K (ULTRA HD)-k_okcNVZqqI.m4a" -c copy INK_DROPS.mp4


The video has sound now, it’s new name is INK_DROPS.mp4. Most players will be able to playback.

What? You want to try using the webm file?

ffmpeg -i "INK DROPS 4K (ULTRA HD)-k_okcNVZqqI.webm" -c:v libvpx -i "INK DROPS 4K (ULTRA HD)-k_okcNVZqqI.m4a" INK_DROPS_WEBM.mp4

Notice how the audio quality of INK_DROPS_WEBM.mp4 dropped. Sounds tinny. Let’s try again with a different audio codec(-c:a)
ffmpeg -i "INK DROPS 4K (ULTRA HD)-k_okcNVZqqI.webm" -i "INK DROPS 4K (ULTRA HD)-k_okcNVZqqI.m4a" -c:a libvorbis INK_DROPS_WEBM_LIBVORBIS.mp4

ahhhh, much better sounding this time. What about using AAC instead of libvorbis?
ffmpeg -i "INK DROPS 4K (ULTRA HD)-k_okcNVZqqI.webm" -i "INK DROPS 4K (ULTRA HD)-k_okcNVZqqI.m4a" -c:a aac -strict experimental INK_DROPS_WEBM_TO_MP4_AAC.mp4

Sounds good too.
——————————————————————————————————————————–

What about UHD(3840×2160) content?

You can generate a nice UHD file from youtube sources but TV manufactures have taken this ball to their courts. What do I mean?
Well the .mp4 UHD(2160p) file you download from youtube will not play on many sets from Samsung, Vizio, LG, Sharp, etc.
A new codec was created, H.265/HEVC, it is somewhat problematic and expensive. Source 1Source 2
You can have a HEVC file with .mp4/.mkv container(extension) which will work on the displays above.

You can still generate the .mp4 file, play it back via HDMI-out port to an UHD display but not via a USB Flash Drive or External Hard Disk.
Let’s get format code 138, 138 mp4 3840x2160 DASH video 24157k , 24fps, video only, 283.43MiB
youtube-dl -f 138 https://www.youtube.com/watch?v=k_okcNVZqqI

[youtube] k_okcNVZqqI: Downloading webpage
[youtube] k_okcNVZqqI: Extracting video information
[youtube] k_okcNVZqqI: Downloading DASH manifest
[download] Destination: INK DROPS 4K (ULTRA HD)-k_okcNVZqqI.mp4

Join UHD video file with audio. We are using -flags global_header to avoid violations.
Try without it, the file will generate but you will get these warnings. I think FFmpeg compensates, corrects it for output.
[mp4 @ 00000000056e9820] Codec for stream 0 does not use global headers but container format requires global headers
[mp4 @ 00000000056e9820] Codec for stream 1 does not use global headers but container format requires global headers

ffmpeg -i "INK DROPS 4K (ULTRA HD)-k_okcNVZqqI.mp4" -i "INK DROPS 4K (ULTRA HD)-k_okcNVZqqI.m4a" -c copy -flags global_header INK_DROPS_UHD.mp4

You should now have a playable .mp4 file. You can also output via hdmi-out port to an UHD tv display.
——————————————————————————————————————————–

UHD 2160p HEVC/H265 with AAC

So how do we get a file to be compliant enough to pass as playable via USB Flash or Ext. HDD?
Make sure it is HEVC, FFmpeg’s libx265 will help with that part.
As for audio, FFmpeg’s(9/25/15) AAC is experimental so you have to add -strict -2 or -strict experimental

Here is an example — *** You should be aware that the commands extend into the second line. ***
ffmpeg -i "INK DROPS 4K (ULTRA HD)-k_okcNVZqqI.mp4" -i "INK DROPS 4K (ULTRA HD)-k_okcNVZqqI.m4a" -c:a aac -strict experimental -c:v libx265 -preset ultrafast INK_DROPS_HEVC_H265_AUDIO.mkv

I have to admit HEVC compression efficiency is insane. With -preset ultrafast the file is 120MB. The UHD .mp4 was 296MB!!!
Let’s try -preset veryslow. This should be interesting, it will take a while.
ffmpeg -i "INK DROPS 4K (ULTRA HD)-k_okcNVZqqI.mp4" -i "INK DROPS 4K (ULTRA HD)-k_okcNVZqqI.m4a" -c:a aac -strict experimental -c:v libx265 -preset veryslow INK_DROPS_HEVC_H265_AUDIO_VERYSLOW.mkv

Stream mapping:
Stream #0:0 -> #0:0 (h264 (native) -> hevc (libx265))
Stream #1:0 -> #0:1 (aac (native) -> aac (native))
Press [q] to stop, [?] for help
frame= 4422 fps=0.4 q=-0.0 Lsize= 86444kB time=00:03:04.48 bitrate=3838.6kbits/s

With -preset veryslow, the file is about 88.5MB. Impressive!

Let us try another round with -x265-params -crf=23 enabled.
ffmpeg -i "INK DROPS 4K (ULTRA HD)-k_okcNVZqqI.mp4" -i "INK DROPS 4K (ULTRA HD)-k_okcNVZqqI.m4a" -c:a aac -strict experimental -c:v libx265 -preset medium -x265-params crf=23 INK_DROPS_HEVC_MEDIUM_CRF23.mkv

Even with the mode above, the file is still under in size compared to .mp4(stock parameters) one. Seating at 177MB.
One more round with -b:a 256k audio, -preset slow.
ffmpeg -i "INK DROPS 4K (ULTRA HD)-k_okcNVZqqI.mp4" -i "INK DROPS 4K (ULTRA HD)-k_okcNVZqqI.m4a" -c:a aac -strict experimental -b:a 256k -c:v libx265 -preset slow -x265-params crf=23 INK_DROPS_HEVC_SLOW_CRF23_256k.mkv

Surprising enough, the file is in the same range ~178MB with 256k aac. Not bad!

Nice! What about another round with the .mp4 container?
ffmpeg -i "INK DROPS 4K (ULTRA HD)-k_okcNVZqqI.mp4" -i "INK DROPS 4K (ULTRA HD)-k_okcNVZqqI.m4a" -c:v libx265 -preset slow -x265-params crf=23 -c:a aac -strict experimental -b:a 256k INK_DROPS_HEVC_SLOW_CRF23_256k.mp4




Leave a Reply

Your email address will not be published. Required fields are marked *