Tuesday 19 May 2020

youtube-dl (how to) - best guide to get you started

####################################################################
###        youtube-dl - the best guide to get you started        ###
###                                                              ###

>> Installing
[Note]: Before you start; 
If you have upgraded to Catalina (on never have installed xcode and gcc before), then you will need to:
install Xcode (from Apple's App Store), then run the command: xcode-select --install 
then install gccand then install youtube-dl

[TIP]: You must run brew update first. Then you can either use the brew upgrade to update all packages, or, run brew upgrade youtube-dl to update only youtube-dl

//To install youtube-dl [1] under MacOS use:
brew install youtube-dl

//Once it is installed, update the application:
sudo youtube-dl -U

>> Download Video
//Download a youtube [URL] as video file:
..see all available options for video/audio quality: 
youtube-dl -F [URL]

..download automatically the video with the best quality available: 
youtube-dl -f best [URL] 

[FYI]: The 'best' option selects the best file video quality resolution instead of video quality bit-rate.
[Tip]: You can choose and combine video and audio from the list. For example, in the list below, even though the 'best' option is 1280x720 (marked as: 22), you can choose the 1920x1080 mp4 container (marked as: 137) with 132k audio at 44100Hz (marked as: 140). To do this, use the -f option as: -f 137+140

format code  extension  resolution note
139          m4a        audio only DASH audio   51k , m4a_dash container, mp4a.40.5@ 48k (22050Hz)
140          m4a        audio only DASH audio  132k , m4a_dash container, mp4a.40.2@128k (44100Hz)
251          webm       audio only DASH audio  135k , webm_dash container, opus @160k (48000Hz)
278          webm       256x144    DASH video   95k , webm_dash container, vp9, 24fps, video only
160          mp4        256x144    DASH video  108k , mp4_dash container, avc1.4d400b, 24fps, video only
242          webm       426x240    DASH video  220k , webm_dash container, vp9, 24fps, video only
133          mp4        426x240    DASH video  242k , mp4_dash container, avc1.4d400c, 24fps, video only
243          webm       640x360    DASH video  405k , webm_dash container, vp9, 24fps, video only
134          mp4        640x360    DASH video  521k , mp4_dash container, avc1.4d401e, 24fps, video only
244          webm       854x480    DASH video  752k , webm_dash container, vp9, 24fps, video only
135          mp4        854x480    DASH video 1155k , mp4_dash container, avc1.4d4014, 24fps, video only
247          webm       1280x720   DASH video 1505k , webm_dash container, vp9, 24fps, video only
136          mp4        1280x720   DASH video 2310k , mp4_dash container, avc1.4d4016, 24fps, video only
248          webm       1920x1080  DASH video 2646k , webm_dash container, vp9, 24fps, video only
137          mp4        1920x1080  DASH video 2967k , mp4_dash container, avc1.640028, 24fps, video only
18           mp4        640x360    360p  340k , avc1.42001E, 24fps, mp4a.40.2@ 96k (44100Hz), 75.12MiB
22           mp4        1280x720   720p 1295k , avc1.64001F, 24fps, mp4a.40.2@192k (44100Hz) (best)
This action will download both the .mp4 and .m4a files and produce the final .mp4 with the video and audio that you chose. 

..download automatically the video with the best quality available, including a proper filename
youtube-dl -f best -o '%(title)s.%(ext)s' --restrict-filenames [URL]

>> Download Subtitles 
//The following are the subtitle options you have with youtube-dl. 
--write-sub             Write subtitle file
--write-auto-sub        Write automatic subtitle file (YouTube only)
--all-subs              Download all the available subtitles of the video
--list-subs             List all available subtitles for the video
--sub-format FORMAT     Subtitle format, accepts formats preference, for example: "srt" or "ass/srt/best"
--sub-lang LANGS        Languages of the subtitles to download (optional) separated by commas, use IETF language tags like 'en,pt'

//With --write-auto-sub you will download (YouTube only) the default generated subtitles in a .vtt file. 

youtube-dl -f best -o '%(title)s.%(ext)s' --restrict-filenames --write-auto-sub [URL]

[Tip]: To convert to .srt instead, you need to use: --write-auto-sub --convert-subs=srt

//You can also specify the language of the subtitles you want to download: 
youtube-dl -f best -o '%(title)s.%(ext)s' --restrict-filenames --write-srt --sub-lang en [URL]

//If you only need to download the subtitles (not the video), use:
youtube-dl --all -subs --skip-download [URL]

//If the video does not have subtitles the following error will appear: 
WARNING: video doesn't have subtitles 

[FYI]: Keeping the subtitles file in the same directory as the video file for VLC to load them automatically.
[Tip]: Google2SRT on sourceforge is a free and open source alternative to download subtitles for Windows, Mac and Linux. See: http://sourceforge.net/projects/google2srt/

[Bonus!]: Burn / Merge / Hardcode / Combine / Embed the subtitles using Youtube-dl and ffmpeg. 
In case you need to play the video on devices that do not support to load the subtitles from a separate file, you can use ffmpeg to embed them in the video. There are 2 different options to do this which are explained bellow as Option A and Option B
Note that, first you need to download the .srt version of the subtitles. For convenience, the following command with download both the video and the subtitles converted to .srt (instead of .vtt): 
youtube-dl -f best -o '%(title)s.%(ext)s' --restrict-filenames --write-srt --sub-lang en --convert-subs=srt [URL]

(Option A) "Packing" the subtitles in the video file: 
This is a fast way with "packs" (embeds) the subtitles within the video file. In other words you end up with only one video file that you have the option to enable/disable the subtitles. (similar to .mkv files). For example, when using this option, in VLC under Subtitles > Subtitle Track you will have the option to enable showing the subtitles, or disable them. That way you may embed more than one choices for subtitles. The following command can merge the two files (video and subs) and output a new version of the video (VideoOut_withSubs.mp4) with the subtitles "packed" within the video file.
ffmpeg -i Video.mp4 -i Video.en.srt -crf 3 -c:v copy -c:a copy -c:s mov_text  VideoOut_withSubs.mp4  

[FYI]: With -crf 3 you control the quality of the output video (lower value, is higher quality).

(Option B) Re-Encoding the video file with the subtitles hardcoded to the video frames:
In this option the subtitles are "burned" onto the frames of the video. This is for playing the video on devices that do not support choosing to enable/disable subtitles, nor, removing the subtitles from the video. (without cropping completely the area of the video where the subtitles are displayed).
ffmpeg -i video.mp4 -vf subtitles=video.en.srt -crf 3 -c:a copy video_subs.mp4

>> Download Audio

[Note]: The default audio format is the ogg (opus) format. You might get a message:
ERROR: ffprobe/avprobe and ffmpeg/avconv not found. Please install one.

In that case, download and install either ffmpeg or avconv to convert the file to mp3 format:
brew install ffmpeg

[Bonus!]: If ffmpeg fails to install dependencies, it might give you a message like the following. Simply run the command as is to install dependencies: 
brew link xz yasm lame x264 xvid

//Download a youtube file in audio format:
youtube-dl -x -f bestaudio --audio-quality 1 --audio-format mp3 [URL]

youtube-dl -x -f bestaudio -o '%(title)s.%(ext)s' --audio-quality 1 --audio-format mp3 [URL]

[Note]: Problematic Filenames;
Using --restrict-filenames can help with problematic filenames that contain emoji characters. This will remove all unicode characters (e.g. Arabic, Japanese, etc.) and replace any spaces with underscore. However, the sed 's/_ */ /g' command allows to replace every underscore with a space. 

>> Useful tips to know about when working with youtube-dl

//To open Finder from terminal in the current working directory:
open .

//See information about an audio file e.g. *.mp3:
afinfo [AUDIO_FILE]

//Play an audio file (e.g. *.mp3) from the terminal:
afplay [AUDIO_FILE] 

//If you download an .webm video file and need to convert it offline to mp3, you can use:
ffmpeg -i "FILE.WEBM" -vn -ab 320k -ar 44100 -y "FILE.mp3"

//How to remove the 'Youtube ID' from all the mp3 filenames in one command:
for i in *.mp3; do mv "$i" "$(echo $i | sed 's/................$//').mp3"; done

//For problematic filenames than contain emoji characters, try a basic cleanup:
..in 3 steps: 
ls *.txt | sed -e 's/[^A-Za-z0-9._-]/_/g' | sed 's/_/ /g' | sed 's/  */ /g'

..in 2 steps: 
ls *.txt | sed -e 's/[^A-Za-z0-9._-]/ /g' | sed 's/  */ /g'

####################################################################
### Links with information about youtube-dl ########################
[1] https://github.com/ytdl-org/youtube-dl
[2] https://www.ostechnix.com/youtube-dl-tutorial-with-examples-for-beginners/
[3] http://manpages.ubuntu.com/manpages/xenial/man1/youtube-dl.1.html
###                                                       ###
#############################################################

Alternative steps:

1. Install Homebrew, if not already installed. This can be done by running the command `/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"` in the terminal.

2. Use Homebrew to install youtube-dl by running the command `brew install youtube-dl` in the terminal.

3. To download a video from YouTube, use the command `youtube-dl [YouTube URL]` in the terminal.

4. To download an entire playlist, use the command `youtube-dl --yes-playlist [YouTube Playlist URL]` in the terminal.

5. To download videos in the highest quality available, use the command `youtube-dl -f best [YouTube URL]` in the terminal.

6. To download videos in a specific format, use the command `youtube-dl -f [format] [YouTube URL]` in the terminal.

7. To download videos in multiple formats, use the command `youtube-dl -f "[format1],[format2],[format3]" [YouTube URL]` in the terminal.

No comments:

Post a Comment