1

Is it possible to write Python code that gets a video file (mpg, avi, wmv, mov, etc) and a time interval (e.g. from 5_min-10_secs-11_ms to 10-min_15-secs-40_ms) and create a small video from this interval?

I know this is possible with ffmpeg, mencoder and other programs, but I'd like to do this in Python code.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
xralf
  • 3,312
  • 45
  • 129
  • 200
  • 1
    I'd say the path least annoying will be to just shell out to `ffmpeg`; the CLI seems to be the only interface to the ffmpeg libraries that's guaranteed to both remain up-to-date, and expose all the high-level functionality. You could also try using the Python bindings for [GStreamer](http://gstreamer.freedesktop.org/) which also seems to work at this high a level, but it looks like a pretty complex library. – millimoose Jan 10 '12 at 13:58
  • 1
    What do you exactly mean by "do this in python code". Do you mean: a) pure python, b) python bindings to an external library, or c) a system call to e.g. ffmpeg. – Paul Hiemstra Jan 10 '12 at 14:02
  • @PaulHiemstra I mean to create output from inputs defined in the question. – xralf Jan 10 '12 at 14:06
  • I understand that, I asked more specifically what you meant by "do this in python code". – Paul Hiemstra Jan 10 '12 at 14:14
  • I've asked this myself a few times. There is still no python native way to do it - mencoder or ffmpeg from the command line is the best approach. – Björn Lindqvist Jan 10 '12 at 14:58
  • @PaulHiemstra If you know more possibilities to achieve the goal it's even better and we can compare the solutions. – xralf Jan 10 '12 at 16:03
  • @bjorn I think one of the reasons why there is no python native solution is because using a system call works so well. In additionc I can imagine that a pure python implementation would be quite slow... – Paul Hiemstra Jan 10 '12 at 16:08
  • @xralf, do you like any of the answers? It seems you have not upvoted any answer. – Paul Hiemstra Jan 10 '12 at 16:09

1 Answers1

1

Googling for "python library to edit video file" lead me right back to SO:

In addition to getting this kind of functionality from a library, I often use a system call (os.system or subprocess) to call mencoder or ffmpeg. You could write a function to do this, in pythonesque pseudo code:

def getVideoChunk(filepath, timerange, outputfile):
    retcode = systemcall("ffmpeg %s %s", filepath, timerange, outputfile)
    return retcode

Ofcourse you need to choose a way of running tge systemcall and you need to learn some ffmpeg syntax.

EDIT (based on comments) Assuming the syntax of mmpeg is like:

ffmpeg -i input.mpg -sameq -ss 00:02:00 -t 00:02:00 output.mpg

The call would look like:

getVideoChunk("input.mpg", "00:02:00", "output.mpg")

and the system call bit would look like (note I use os.system):

os.system("ffmpeg -i %s -sameq -ss %s -t %s %s" % (filepath, timerange, timerange, outputfile))

note that this code is also pseuodo python code and I have not tested it...the code is purely instructional.

Community
  • 1
  • 1
Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149