Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trim (remove frames from) a video using Python

I would like to trim - cut off frames at the beginning and end - a video that can be in a variety of different formats, and then save the trimmed video.

Are there any libraries or suggestions on how to do this?

Thanks!

like image 643
Albeit Avatar asked Oct 24 '22 01:10

Albeit


1 Answers

I've been using ffmpeg and the subprocess module of python to extract video thumbnails, but it seems ffmpeg can do pretty much anything.

Once you install ffmpeg, you can trim off the first second of video like so

> ffmpeg -i sample.mov  -ss 1 trim.mov

So using the subprocess module of python

import subprocess
seconds = "1" # has to be a string
subprocess.call(['ffmpeg', '-i', inputfilename, '-ss', seconds, outputfilename])

will take off the first second. For specific frames, there are flags like -vframes and -dframes, but I haven't actually used them. Documentation of ffmpeg here.

There's also pyffmpeg, a python wrapper for ffmpeg. But I haven't used it.

like image 113
Zach Avatar answered Oct 27 '22 09:10

Zach