Sometimes we want to share with YouTube some part of a video on YouTube - the attention span in modern reality is reduced to the limit, and if you drop the link to the video (even with the start timecode) with the comment
“watch from 21:51 to 24:55” - great the probability that the video will not be viewed.
In addition - pieces of video may be required for mounting their clips - and it is rather inconvenient to download the entire clip for a few seconds and search for / cut out the necessary part in the editing program.
How to upload a part of a YouTube video using ffmpeg - under the cut
Obtain a direct link
Part of the implementation of my Telegram
bot in Python:
We need the
pytube library.
Create a YouTube class object to which we pass our link, pass the number of the desired stream and get a direct link to the video / audio
')
from pytube import YouTube link = " " itag = _ url = YouTube(link).streams.get_by_itag(itag).url
Note that 1080p and 480p streams do not have audio tracks.
Next, we submit a link to the input (-i) ffmpeg together with the start (-ss) and end (-to) timecodes in the format “hh: mm: ss.xx”. We set the audio codec, bitrate and "-avoid_negative_ts make_zero" in order to avoid hanging pictures 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 cut it off - we immediately download the necessary 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
And what to do with streams without audio? FFMPEG will help us out here too - it can take several streams as inputs and merge them.
We get a direct link to the video stream (for example, 137 - 1080p) and to the stream from audio / video - for example, 18 - 360p
url = YouTube(link).streams.get_by_itag(itag).url aurl = YouTube(link).streams.get_by_itag(18).url
Then the magic begins - we feed both streams to the input 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 it - now the loading and merging of streams occurs 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 the conversion of video / audio from one format to another and allows you to optimize the load on the incoming channel, disk, processor time and RAM.
With the help of ffmpeg in the
bot , the acceleration / deceleration of the sound with thin compensation is implemented, compression into the opus format. Now here's the video / audio download from the right time using the timecodes - just attach the timecodes to the link and the bot promptly sends the right piece of audio / video in the right format and quality:
http://www.youtube.com/watch?v=Qgm36HHDEk0(30:29.5-30:38.5)