[mythtv-users] Automated mpeg2 recordings chopping with avidemux

Paul de Bruin P.W.deBruin at ewi.tudelft.nl
Sat Nov 29 16:08:32 EST 2003


Hi all,

Today I spent some time figuring out how to cut sections from mpeg2
recordings (pvr250/350) without reencoding. After a bit of
experimentation I found out it can be done using avidemux.  I have
attached a snippet of python code that generates the required commands.

The steps:

My recordings are: MPEG2 352x576 at 25 2000/2500 bitrate, with 224 mp2 audio

In mythtv create/edit the cutlist for the recording. Make sure there is
a cut at the beginning of the recording (if the recording starts
immediately, cut the first second or so).

Edit the attached python script to provide mysql location/user/password.
Call with the full path to the recording to generate the lines to be
executed to generate a .m2v/.mp2 pair for each section of the cutlist.

Example:

# cutmpeg2.py /media/tv/9_20031129151300_20031129154500.nuv 

avidemux --index-mpeg /media/tv/9_20031129151300_20031129154500.nuv 
         /media/tv/9_20031129151300_20031129154500.nuv.idx C0 --audio-codec MP2 --quit
avidemux --load /media/tv/9_20031129151300_20031129154500.nuv --begin 9097 --end 40597 
         --audio-codec MP2 --save-raw-video 9_20031129151300_20031129154500_0.m2v 
	 --save-raw-audio 9_20031129151300_20031129154500_0.mp2 --quit

[the output above is two lines, but is formatted for this mail]

The first line generates an index (.idx) for the mpeg2 recording
(required). The second line cuts the raw video and audio streams and
saves these to two files (.m2v and .mp2) for each section of the
cutlist.  These streams can then be remuxed and all sections can be
concatenated. 

Note:
1) This is all just a proof of concept.
2) avidemux needs X to operate and will popup all sorts of annoying
   progress windows during processing.

Hope this helps,
Regards,
Paul
-------------- next part --------------
#!/usr/bin/env python

# simple python script to automate removing sections from mpeg2 recordings by mythtv with avidemux

import re
import sys
import os.path

# get info from filename
basename = os.path.basename(sys.argv[1]).split(".")[0]
[chanid, starttime, endtime] = basename.split("_");

#print 'Basename : ', basename
#print 'ChanId   : ', chanid
#print 'Start    : ', starttime
#print 'End      : ', endtime

user = "mythtv"
pwd  = "mythtv"
host = "mendax"

mysqlcmd = "mysql -u %s --password=%s -h %s " % (user, pwd, host) +\
           '-e "use mythconverg; select cutlist from recorded where ' +\
           'chanid=%s and starttime=%s and endtime=%s;"' % (chanid, starttime, endtime)
	  

# get info from database
cutlist = os.popen(mysqlcmd, 'r')
lines   = cutlist.readlines()
tmpcut  = "".join(lines)
cutlist.close()

cuts = []
# get cutlist points
numbermatch = re.compile('[0-9]+')
matches = numbermatch.finditer(tmpcut)
for match in matches:
	cuts.append(match.group(0))	

# stream number stuff for avidemux can be found here:
# http://fixounet.free.fr/avidemux/doc/en/input.xml.html

# build output 
print "avidemux --index-mpeg %s %s %s --audio-codec MP2 --quit" % (sys.argv[1], sys.argv[1]+'.idx', 'C0')
for i in range(len(cuts)/2-1):
	print "avidemux --load %s --begin %s --end %s --audio-codec MP2 --save-raw-video %s --save-raw-audio %s --quit" %\
	      (sys.argv[1], cuts[2*i+1], cuts[2*i+2], basename+"_"+`i`+'.m2v', basename+"_"+`i`+'.mp2')


More information about the mythtv-users mailing list