youtube-dl, downloading streams (arch/linux/android) #021

I am a big fan of jDownloader, since I prefer to have videos on my drive, I can start them from when I want, change speed, change size, pause without any of the online lag.

I found youtube-dl when searching , it is a very powerful application, very useful to handle HTTP Live Streaming (HLS) protocol.

The following commands are specific for twitch and youtube,  motor trend on demand(.)com method is  at the bottom of the post

youtube-dl --list-formats https://www.twatch.tv/videos/999999999


 to get the media in a specific format, you can use -f FORMAT

 youtube-dl -f 720p https://www.twatch.tv/videos/99999999

 to get the url the video, use  option -g

 youtube-dl -f 720p -g https://www.twatch.tv/videos/999999999


This will provide a link that you can launch in mpv/vlc and watch the VOD,  vlc can record it.

Downloading parts of a twitch VOD

Very specific to twitch are the long streams, you can select a start time and and end time for avoid downloading such a large file, for that use youtube-dl to get the VOD url, then ffmpeg to set the time interval:

 youtube-dl -f 720p -g https://www.twatch.tv/videos/999999999


 then use the url in ffmpeg, add -ss start time -to end time and the destination file name :
ffmpeg -ss 01:00:00 -to 02:00:00 -i https://some.stream.url -acodec copy -vcodec copy new_twitch_stream.mp4


 notice that ffmpeg only downloads the selected time range, the options are:
    -ss HH:MM:SS : start time to take
    -to HH:MM:SS : end time
    -t HH:MM:SS : time length to take

   -acodec copy ; keep original audio codec
   -vcodec copy ; keep original video codec

more usefull youtube-dl commands :

  youtube-dl -f 720p -v https://www.twatch.tv/videos/999999999 ; verbose mode
  youtube-dl -a list_of_urls.txt ; download all links in the file
  youtube-dl -x   https://www.twatch.tv/videos/999999999 ; dowload audio only
  youtube-dl --write-description --write-info-json --write-annotations --write-sub --write-thumbnail https://www.youtobe.com/watch?v=aaaaaaaa ; works for youtube only
 youtube-dl -f bestvideo+bestaudio https://www.youtobe.com/watch?v=aaaaaaaa ; also worstvideo or worstaudio
 youtube-dl  --rm-cache-dir ; to remove cache from a failed download
youtube-dl -o output_file_name.mp4 ; to create a specific file

I use youtube-dl -f best -a youtube_list.txt on android phone; its just a very fast way to save videos to watch offline later, this post contains instructions about termux - android terminal emulator.

Method for other sites, including motor trend on demand(.)com
If youtube-dl --list-formats https://www.motortrendwithoutdemand.com/(...) is used:


It will return an error, however you can get around this by using the browser extension "The Stream Detector"

 Launch the site, wait for the video to start, then right click over the icon and choose


Open a text editor, paste all the links, and try each one in youtube-dl, if successfull you will get this :


so, now to download a specific resolution, he 4278 code is, according to the picture above 1920x1080 resolution:

youtube-dl -f 4278 -v  https://cdnsecak(...)


If an error occurs while downloading a stream, sometimes adding -ciw to youtube-dl options can work:

 youtube-dl -ciw (usual options) (url)

-c, --continue ;Force resume of partially downloaded files. By default, youtube-dl will resume downloads if possible.
-i, --ignore-errors ;Continue on download errors, for example to skip unavailable videos in a playlist
-w, --no-overwrites ;Do not overwrite files

ffmpeg can fix issues with a mp4/mkv/(...) files, so if a stream download file has issues, use:

ffmpeg -err_detect ignore_err -i "the_downloaded_stream.mp4" -c copy the_fixed_stream_file.mp4
 
Youtube-dl in android

youtube-dl is a great tool and there is a way to use it in android using Termux to access some linux applications.

Install termux, if google play say the device is not compatible, you can use other stores to get the .apk file.

Make sure termux has access to storage, go the phone Settings -> Applications -> Termux -> Permissions ,  alternative method is to type in termux bash:

termux-setup-storage

The required permission request dialogue should pop up

Open termux, and type the following commands:

 pkg update ;to check for updates
 pkg upgrade ;to install the updates
 pkg install python ;this will provide the application pip, will be installed from the python repository
 pip install youtube-dl

 works as the linux/windows version

 To copy files there is an application that displays progress: rsync

pkg install rsync

 then use :

rsync -ah --progress downloaded_file.mp4 /destination_folder/

 to grant access to a new installed sd card, type:

termux-setup-storage

 In home folder, storage directory should appear, containing external-1 folder that gives access to sd-card, repeate the command if you changed sd-cards.

When 'best' format is not the best at all

Sometimes when using the '-f best' option a very low quality video is downloaded , this happens because:

youtube-dl --list-formats https://www.youtube.com/watch?v=aaabbbccxxx


youtube or another web page doesn't have a 720p/1080p video with the audio merged, so to get the 'best' video its required to download them in 2 steps, video and audio:

youtube-dl -f 140 -v https://www.youtube.com/watch?v=adscasdcasdca
youtube-dl -f 137 -v https://www.youtube.com/watch?v=asdcasdvsdfvsdf

After downloading the mp4(video) and the m4a(audio) files use ffmpeg to merge them:


 ffmpeg -i video_file.mp4 -i audio_file.m4a -c copy video_and_audio.mkv

Downloading only some videos from a youtube playlist

use the --playlist-item parameter:

youtube-dl -f best --playlist-items 39,38,37 https://www.youtube.com/playlist?list=PLblablablablabla


Comments