Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Youtube Video Download & Trim
# https://github.com/mps-youtube/pafy
import pafy
if __name__ == '__main__':
video_url = 'https://www.youtube.com/watch?v=D_Ij3fAps4s'
video = pafy.new(video_url)
print(video.title, video.duration)
best = video.getbest()
best.download(quiet=False)
import ffmpeg # https://github.com/kkroening/ffmpeg-python/
def trim(input_path, output_path, start=30, end=60):
"""
https://github.com/kkroening/ffmpeg-python/issues/184
"""
input_stream = ffmpeg.input(input_path)
vid = (
input_stream.video
.trim(start=start, end=end)
.setpts('PTS-STARTPTS')
)
aud = (
input_stream.audio
.filter_('atrim', start=start, end=end)
.filter_('asetpts', 'PTS-STARTPTS')
)
joined = ffmpeg.concat(vid, aud, v=1, a=1).node
output = ffmpeg.output(joined[0], joined[1], output_path)
output.run()
if __name__ == '__main__':
input_path = './HOZIN | KING OF POP | Judge showcase.mp4'
output_path = './hozin_short.mp4'
trim(input_path, output_path, start=30, end=60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.