
FFMPEG. Download part of the video from YouTube
- Tutorial

Sometimes we want to share part of some video on YouTube with friends - the concentration time in modern reality is reduced to the limit, and if you drop the link to the video (even with a timecode) with the comment “watch from 21:51 to 24:55” - it’s great the likelihood that the video will not be watched.
In addition - pieces of video may be required for editing your videos - and it’s rather inconvenient to download the entire video for a few seconds and search / cut the necessary part in the editing program.
How to download part of a YouTube video using ffmpeg - under cat
Obtain a direct link
Part of the implementation of my Telegram bot in Python:
We need a pytube library .
We create a YouTube class object to which we transfer our link, transfer the number of the desired stream and get a direct link to video / audio
from pytube import YouTube
link = "ссылка на видео"
itag = номер_потока
url = YouTube(link).streams.get_by_itag(itag).url
Please note that 1080p and 480p streams do not have audio tracks.

Next - submit a link to the input (-i) ffmpeg along with the time codes of the beginning (-ss) and end (-to) in the format "hh: mm: ss.xx". We set the audio codec, bitrate and "-avoid_negative_ts make_zero" in order to avoid freezing the picture at the beginning of the video due to the loss of key frames.
ffmpeg will download the video from the right moment - we don’t need to download the video to the computer and crop it - we immediately pump out the desired piece.
process_call_str = 'ffmpeg -ss {1} -to {2} -i "{0}"'\
'-acodec aac -b:a 192k -avoid_negative_ts make_zero "{3}"'
.format(str(url), str(ss), str(t), download_file_path)
status = subprocess.check_call(process_call_str, shell=True)
Streams without audio
So what to do with streams without audio? FFMPEG will help us out here too - it can receive several streams at the input and combine them.
We get a direct link to the video stream (for example 137 - 1080p) and to the stream with audio / video - for example 18 - 360p
url = YouTube(link).streams.get_by_itag(itag).url
aurl = YouTube(link).streams.get_by_itag(18).url
Next, the magic begins - we feed both streams and with the help of “-map” we take the video track from the first stream and the audio track from the second stream and combine - now the loading and combining of the streams takes place from the right place from two sources.
process_call_str = 'ffmpeg -ss {2} -to {3} -i "{0}" -ss {2} -to {3} -i "{1}"' \
' -acodec aac -b:a 192k -avoid_negative_ts ' \
'make_zero -map 0:v:0 -map 1:a:0 "{4}"' \
.format(str(url), str(aurl), str(ss), str(t), download_file_path)
status = subprocess.check_call(process_call_str, shell=True)
Conclusion
In general, ffmpeg is quite a powerful thing, the possibilities of which are somewhat wider than converting video / audio from one format to another and allows you to optimize the load on the incoming channel, disk, processor time and RAM.
Using ffmpeg, the bot implements acceleration / deceleration of sound with loudness, compression to the opus format. Now here is the video / audio download from the right time by timecodes - just attach timecodes to the link and the bot will promptly send the desired piece of audio / video in the desired format and quality:
http://www.youtube.com/watch?v=Qgm36HHDEk0(30 : 29.5-30: 38.5)