Skip to content

Instantly share code, notes, and snippets.

@macu
Created March 13, 2014 02:29
Show Gist options
  • Save macu/9520902 to your computer and use it in GitHub Desktop.
Save macu/9520902 to your computer and use it in GitHub Desktop.
Simplifies using avconv to slice out part of a video
#!/usr/bin/env python
import sys
from operator import sub
from subprocess import call
def parseTime(s):
parts = s.split(':')
if len(parts) == 2:
parts.insert(0, 0)
if len(parts) != 3:
raise Exception("Bad format")
return list(map(int, parts))
def expressTime(t):
return "%02d:%02d:%02d" % tuple(t)
def duration(t0, t1):
d = map(sub, t0, t1)
if d[2] < 0:
d[2] += 60
d[1] -= 1
if d[1] < 0:
d[1] -= 60
d[0] -= 1
return d
def sliceVideo(ifile, ofile, starttime, endtime):
ss = expressTime(starttime)
t = expressTime(duration(endtime, starttime))
args = ["avconv", "-i", ifile, "-ss", ss, "-t", t, "-c", "copy", ofile]
print args
call(args)
def main(argv):
print "Slice out a section of video using avconv"
print "(Convert avi to mp4 to avoid choppy audio)"
ifile = ""
ofile = ""
if len(argv) > 0:
ifile = argv[0]
print "Input file : ", ifile
else:
ifile = raw_input("Input file : ")
if len(argv) > 1:
ofile = argv[1]
print "Output file : ", ofile
else:
ofile = raw_input("Output file : ")
print "Enter start time and end time in HH:MM:SS format"
stime = parseTime(raw_input("Start time : "))
etime = parseTime(raw_input("End time : "))
sliceVideo(ifile, ofile, stime, etime)
if __name__ == "__main__":
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment