Python time difference and ffmpeg

I frequently want to cut scenes out of video clips. I use ffmpeg to do this. However it’s tedious to put my options in every time. The command I used over and over is…
“ffmpeg -i fileIn -ss startTime -t timeLength [-codec copy] fileOut”

This was tedious to type every time. So I wrote a Python program so I could just enter the args…
fileIn [c] startTime timeLength

This saved a lot of typing. Notice there is no fileOut, because I simply create an output file with the same name as the input file, only I add a sequence suffix to the input file. So fileOut is usually filein-01.mp4, the next run it’s filein-02.mp4 and so on. The optional “c” added “-codec copy”. Which creates the output file very fast. However it doesn’t always start exact. Sometimes there are several seconds after your specified startTime, missing before the fileOut starts playing. Not adding “c” for “-codec copy” can take much longer to run but provides a more exact start time.

Now this has saved me a lot of typing. However the timeLength can be tedious to calculate. So if I watch the vid on vlc and the piece I want starts at 3min 46sec and ends at 35min 13sec I need to mentally calculate that’s 31 min 27sec…or 31:27 for timeLength. Not easy for my old brain. So that’s where this project comes in. It’s a very easy problem for Python.

import datetime
# Since I only care about the time difference…use the same date!
start = datetime.datetime(2000, 1, 1, 0, 3, 46) # (yy, mm, dd, hh, mm, ss)
finish = datetime.datetime(2000, 1, 1, 0, 35, 13)
difference = finish – start

Now I considered, there’s a good possibility that ffmpeg has a ending time flag to use for this, rather than the ‘-t’ flag. But where’s the fun in that?

Join the Conversation

1 Comment

Leave a comment

Your email address will not be published. Required fields are marked *