Skip to content

Instantly share code, notes, and snippets.

@j-min
Created July 1, 2019 05:19
Show Gist options
  • Save j-min/077fbb548037cd229a2fa4207739b73c to your computer and use it in GitHub Desktop.
Save j-min/077fbb548037cd229a2fa4207739b73c to your computer and use it in GitHub Desktop.
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