Skip to content

Instantly share code, notes, and snippets.

@lidio601
Last active August 29, 2015 14:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lidio601/15e4b0ab5d80755b4f5b to your computer and use it in GitHub Desktop.
Save lidio601/15e4b0ab5d80755b4f5b to your computer and use it in GitHub Desktop.
convert MOD+MOI video to AVI. In this case I need to convert my video camera output files (*.MOD + *.MOI) to AVI files to save disk space and have a more useful video format.
#!/usr/bin/python
# Script to import video from Panasonic SDR-H20 Camera
# This script is the same as SDCopy.exe utility in Windows
# Script copies mod files, renames them to "YYYY-MM-DD HH-MM.mpg" pattern, where "YYYY-MM-DD HH-MM" is creation time
# Script also can optionally set aspect ratio flag
# Made by m0sia (m0sia@plotinka.ru)
# Modified by lidio601
import os
import sys
import commands
import string
import struct
import shutil
import datetime
if len(sys.argv)<3:
print "Usage "+sys.argv[0]+" \"camera path\" \"destination path\" [setAspectFlag]"
print "camera path contains files from your camera"
print "destion path - path, where import mpg files"
print "destion path = - to create a subdirectory in the source path"
print "setAspectFlag will set wide screen flag in mpg files, maybe useful"
sys.exit()
# search for *.mod video files
commandString = "find "+sys.argv[1]+" -path *.mod"
commandString = "ls -1 \""+sys.argv[1]+"\" | grep -i .mod$"
files = commands.getoutput(commandString)
print commandString
files = string.split(files, "\n")
# output directory
outputdir = str(sys.argv[2]).rstrip("/")+"/"
if outputdir == "-/":
outputdir = os.path.join( sys.argv[1] , os.path.basename(sys.argv[1]) )
os.mkdir(outputdir)
print "Output directory:", outputdir
i=1
tocheck = []
for file1 in files:
datafile=file1.replace(".mod",".moi").replace(".MOD",".MOI")
nam=file1.replace(".mod","").replace(".MOD","")
print 'Input file: ', file1, datafile
f = open(os.path.join(sys.argv[1],datafile), mode='rb')
array=f.read()
year,month,day,hour,minutes=struct.unpack(">HBBBB",array[6:12])
date=datetime.date(year,month,day)
time=datetime.time(hour,minutes)
f.close()
newfilename=os.path.join(outputdir,str(date).replace("-","")+"_"+time.strftime("%H%M")+"_"+nam+".mpg")
print "Coping file "+newfilename+" "+str(i)+"/"+str(len(files))
i=i+1
f = open(os.path.join(sys.argv[1],file1), mode='rb')
newfile = open(newfilename, mode='wb')
while 1:
data=f.read(65536)
if data:
if len(sys.argv)==4:
if sys.argv[3]=="setAspectFlag":
data=data.replace("\x2C\x02\x40\x23\x17\x4C","\x2C\x02\x40\x33\x17\x4C")
newfile.write(data)
else:
break
newfile.close()
f.close()
#exit()
#os.utime(newfilename,(os.path.getmtime(os.path.join(sys.argv[1],datafile)),os.path.getmtime(os.path.join(sys.argv[1],datafile))))
newfilename2 = newfilename.replace(".mpg",".avi")
print "Transconding %s to %s" % (newfilename,newfilename2)
cmd = """ffmpeg -y -i "%s" -filter:v yadif -q:v 0 -q:a 0 -b:v 8400k -vcodec mpeg4 -acodec ac3 -ab 320k -sn -s 640x450 -aspect "16:9" -loglevel quiet "%s" >/dev/null 2>&1 """ % (newfilename,newfilename2)
print
print cmd
print
#print "-"*40
os.system(cmd)
os.utime(newfilename2,(os.path.getmtime(os.path.join(sys.argv[1],datafile)),os.path.getmtime(os.path.join(sys.argv[1],datafile))))
#print "-"*40, "\n\n"
print "From size of %4.2fMB to %4.2fMB" % (os.path.getsize(newfilename)/(1024*1024),os.path.getsize(newfilename2)/(1024*1024))
os.remove(newfilename)
tocheck.append(newfilename2)
#break
# fixing file mtime for each output video file
for file in tocheck:
os.utime(file,(os.path.getctime(file),os.path.getctime(file)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment