Search code examples
pythonpytube

How to download a clip of an YouTube video with pytube?


I've been trying to download certain portions of YouTube video. The long way is to download the video then extract the certain portion of it. But when it comes to a big dataset with long videos, the method is costly.

The code works. But downloads the entire video instead of the certain portion.

from pytube import YouTube

YouTube('https://www.youtube.com/embed/yf8Ub90OWFM?start=15&end=25').streams.first().download()

Expected result: 10 second video in the time interval of 15-25 seconds.


Solution

  • According to issue Support to download partial videos of PyTube it is not currently possible.

    Therefore you might use one of Python videos post processing library, eg moviepy:

    from moviepy.editor import *
    
    video = VideoFileClip("myHolidays.mp4").subclip(50,60)
    video.write_videofile("myHolidays_edited.webm",fps=25)
    

    Or get the command-line ffmpeg tool:

    ffmpeg -ss (start time) -i (direct video link) -t (duration needed) -c:v copy -c:a copy (destination file)