问题
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.
回答1:
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)
result.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)
来源:https://stackoverflow.com/questions/56017234/how-to-download-a-clip-of-an-youtube-video-with-pytube