X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=rec10%2Ftrunk%2Fsrc%2Ftv2mp4.py;h=7584eca9a792311ae4f100591e105ac69c90bb8e;hb=99b552ed08e1ea6c3b4fca44784761565e6856ba;hp=6b7e3cc8e88591f4333d9ee480efcfd5ce8d1dfb;hpb=4aefefe50a07d92c9dcd208e566d0c7af4750178;p=rec10%2Frec10-git.git diff --git a/rec10/trunk/src/tv2mp4.py b/rec10/trunk/src/tv2mp4.py index 6b7e3cc..7584eca 100644 --- a/rec10/trunk/src/tv2mp4.py +++ b/rec10/trunk/src/tv2mp4.py @@ -1,162 +1,257 @@ -#!/usr/bin/python -# coding: UTF-8 -# Rec10 TS Recording Tools -# Copyright (C) 2009 Yukikaze -import commands -import tv2avi -import os -import re -import os.path -import time -import recdblist -import configreader - -def ts2mp4(pin, pout, opt): - dir=os.path.split(pout)[0] - title=os.path.split(pout)[1] - title=os.path.splitext(title)[0] - tpavi=os.path.join(dir, title+".avi") - tv2avi.ts2avi(pin, tpavi, opt) - time.sleep(10) - if not os.path.exists(tpavi): - if os.path.exists(pin): - tpavi=pin - avi2mp4(tpavi,pout,opt) -def avi2mp4(pin,pout,opt): - isvfr=0 - if re.search(u"v", opt): - isvfr=1 - exe = configreader.getpath(u"MP4Box") - dir=os.path.split(pin)[0] - title=os.path.split(pin)[1] - title=os.path.splitext(title)[0] - audiopath=os.path.join(dir,title+u"_audio.raw") - videopath=os.path.join(dir,title+u"_video.h264") - exes=[] - os.environ['LANG']="ja_JP.UTF-8" - txt="" - if isvfr==1: - wineexe=configreader.getpath("wine") - cfr2tc=configreader.getpath("cfr2tc") - dtsedit=configreader.getpath("DtsEdit") - pin120=pin.replace(".avi",".120.avi") - pints=pin.replace(".avi",".ts") - pmp4=pin.replace(".avi",".tmp.mp4") - pintimecode=pin.replace(".avi",".timecode.txt") - pinaac=pin.replace(".avi",".aac") - evfr=[] - evfr.append("ffmpeg -i '"+pints+"' -vn -f aac -acodec copy '"+pinaac+"'") - evfr.append(wineexe+u" "+cfr2tc+u" '"+"Z:\\"+pin+"' '"+"Z:\\"+pin120+u"' 'Z:\\"+pintimecode+u"' 2") - evfr.append(exe+u" -aviraw video \'"+pin120+u"\'") - videotpath=os.path.join(dir,title+u".120_video.h264") - evfr.append(exe+u" -fps 29.970030 -add \'"+videotpath+"\' -add \'"+pinaac+"\' -new \'"+pmp4+"\'") - #evfr.append(exe+u" -add \'"+pinaac+"\' \'"+pmp4+"\'") - evfr.append(wineexe+u" "+dtsedit+u" -tc \'Z:\\"+pintimecode+u"\' \'Z:\\"+pmp4+"\' -o \'Z:\\"+pout+"\'") - exes=evfr - for e in exes: - recdblist.printutf8(e) - txt=txt+e+"\n" - try: - txt=txt+unicode(commands.getoutput(e.encode('utf-8')),'utf-8') - except: - "" - if (not os.path.exists(pin.replace(".avi",".120.avi"))and (not os.path.exists(pout))): - txt=txt+"CFR2TC error. maybe avi file is over 1GB.(ODML error would have happend.)" - ptmp=pin.replace(".avi",".noodml.avi") - e="mencoder -noodml -forceidx -oac copy -ovc copy -o \'"+ptmp+"\'"+" \'"+pin+"\'" - ex="nice -n 17 "+e - e2=wineexe+u" "+cfr2tc+u" '"+"Z:\\"+ptmp+"' '"+"Z:\\"+pin120+u"' '"+"Z:\\"+pintimecode+u"' 2" - ex2="nice -n 17 "+e2 - e3=exe+u" -aviraw video \'"+pin120+u"\'" - ex3="nice -n 17 "+e3 - e4=exe+u" -fps 29.97 -add \'"+videotpath+"\' -add \'"+pinaac+"\' -new \'"+pmp4+"\'" - ex4="nice -n 17 "+e4 - e5=wineexe+u" "+dtsedit+u" -tc \'Z:\\"+pintimecode+u"\' \'Z:\\"+pmp4+"\' -o \'Z:\\"+pout+"\'" - ex5="nice -n 17 "+e5 - recdblist.printutf8(e) - txt=txt+e+"\n" - try: - cmdt=unicode(commands.getoutput(ex.encode('utf-8')),'utf-8') - cmdt=cmdt+unicode(commands.getoutput(ex2.encode('utf-8')),'utf-8') - cmdt=cmdt+unicode(commands.getoutput(ex3.encode('utf-8')),'utf-8') - cmdt=cmdt+unicode(commands.getoutput(ex4.encode('utf-8')),'utf-8') - cmdt=cmdt+unicode(commands.getoutput(ex5.encode('utf-8')),'utf-8') - os.remove(ptmp) - except: - "" - txt=txt+cmdt+"\n" - else: - exes.append(exe+u" -aviraw audio \'"+pin+u"\'") - exes.append(exe+u" -aviraw video \'"+pin+u"\'") - exes.append(exe+u" -new -fps 29.970030 -add \'"+videopath+u"\'#video -add \'"+audiopath+u"\'#audio \'"+pout+u"\'") - for e in exes: - recdblist.printutf8(e) - txt=txt+e+"\n" - try: - txt=txt+unicode(commands.getoutput(e.encode('utf-8')),'utf-8') - except: - "" - logo=pin.replace(u".avi",u".log") - txt = "\n####MP4Box-log####\n"+txt - f=open(logo,'a') - f.write(txt.encode('utf-8')) - f.close() - time.sleep(10) - delpath=[] - delpath.append(pin.replace(u".avi",u".120.avi")) - delpath.append(pin.replace(u".avi",u".sa.avi")) - delpath.append(pin.replace(u".avi",u".ts")) - delpath.append(pin.replace(u".avi",u".aac")) - delpath.append(pin.replace(u".avi",u".m2v")) - delpath.append(pin.replace(u".avi",u".120_video.h264")) - delpath.append(pin.replace(u".avi",u".ts.b25")) - if os.path.exists(pout): - if os.path.exists(audiopath): - os.remove(audiopath) - if os.path.exists(videopath): - os.remove(videopath) - """ - for dp in delpath: - try: - os.remove(dp) - except: - "" - """ -def mkv2mp4(pin,pout): - exeb = configreader.getpath(u"mkvextract") - exe = configreader.getpath(u"MP4Box") - dtsedit=configreader.getpath("DtsEdit") - wineexe=configreader.getpath("wine") - dir=os.path.split(pin)[0] - title=os.path.split(pin)[1] - title=os.path.splitext(title)[0] - audiopath=os.path.join(dir,title+u"_audio.raw") - videopath=os.path.join(dir,title+u"_video.h264") - timecodepath=os.path.join(dir,title+u"_1_timecode.txt") - tmpmp4=os.path.join(dir,title+u".tmp.mp4") - exe0=exeb+u" tracks \'"+pin+u"\' 1:\'"+videopath+u"\' 2:\'"+audiopath+u"\'" - exe1=exeb+u" timecodes_v2 \'"+pin+"\' 1:\'"+timecodepath+"\'" - exe2=exe+u" -new -fps 29.970030 -add \'"+videopath+u"\'#video -add \'"+audiopath+u"\'#audio \'"+tmpmp4+u"\'" - exe3=wineexe+u" "+dtsedit+u" -tc \'Z:\\"+timecodepath+u"\' \'Z:\\"+tmpmp4+u"\' -o \'Z:\\"+pout+u"\'" - logo=pin.replace(".avi",".log") - os.environ['LANG']="ja_JP.UTF-8" - txt= exe0+"\n"+exe1+"\n"+exe2+"\n"+exe3+"\n" - try: - txt = txt+unicode(commands.getoutput(exe0.encode('utf-8')),'utf-8') - txt = txt+unicode(commands.getoutput(exe1.encode('utf-8')),'utf-8') - txt = txt+unicode(commands.getoutput(exe2.encode('utf-8')),'utf-8') - txt = txt+unicode(commands.getoutput(exe3.encode('utf-8')),'utf-8') - except: - "" - os.remove(audiopath) - os.remove(videopath) - txt = "\n####MKV2MP4-log####\n"+txt - f=open(logo,'a') - f.write(txt.encode('utf-8')) - f.close() - time.sleep(10) - if os.path.exists(pout): - if os.path.exists(audiopath): - os.remove(audiopath) - if os.path.exists(videopath): - os.remove(videopath) +#!/usr/bin/python +# coding: UTF-8 +# Rec10 TS Recording Tools +# Copyright (C) 2009-2010 Yukikaze +import commands +import shutil +import tv2avi +import os +import re +import os.path +import string +import base64 +import time +import recdblist +import configreader +import status + +def ts2mp4(pin, pout, opt): + dir=os.path.split(pout)[0] + title=os.path.split(pout)[1] + title=os.path.splitext(title)[0] + tpraw=os.path.join(dir, title+".264") + tpmp4=os.path.join(dir, title+".mp4") + if os.path.isfile(pin) and os.path.getsize(pin)>10*1000: + tv2avi.ts2raw(pin, tpraw, opt) + time.sleep(10) + if os.path.isfile(tpraw) and os.path.getsize(tpraw)>10*1000: + raw2mp4(tpraw, tpmp4, opt) + time.sleep(10) + if os.path.exists(tpraw): + os.remove(tpraw) +def raw2mp4(pin,pout,opt): + dir=os.path.split(pout)[0] + title=os.path.split(pout)[1] + title=os.path.splitext(title)[0] + duration="-fps 29.970030 " + ffmpeg=configreader.getpath("ffmpeg") + isdualaac=0 + ispentaaudio=0 + if re.search("a",opt): + duration="-fps 23.976023 " + elif re.search("v",opt): + duration="-fps 23.976023 " + if re.search("d",opt): + isdualaac=1 + duration="-fps 29.970030 "##ffmpegが24fpsに対応していないための措置 + elif re.search("5",opt): + ispentaaudio=1 + duration="-fps 29.970030 "##ffmpegが24fpsに対応していないための措置 + exe = configreader.getpath("mp4box") + txt="" + os.environ['LANG']="ja_JP.UTF-8" + if isdualaac==1: + wineexe=configreader.getpath("wine") + pincap=pin.replace(".264",".srt") + try: + cap2ass=configreader.getpath("caption2ass") + except: + cap2ass="" + pints=pin.replace(".264",".ts") + paac1=pin.replace(".264","_1.aac") + paac2=pin.replace(".264","_2.aac") + recdblist.printutf8(paac1) + if not os.path.exists(paac1): + paac1=pin.replace(".264","_1.mp3") + if not os.path.exists(paac2): + paac2=pin.replace(".264","_2.mp3") + e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pints+"\" \"Z:\\"+pincap+"\"" + e1=exe +u" "+duration+" -add \""+pin+"\" -new \""+pout+"\"" + e1a1=exe +u" -add \""+paac1+"\" \""+pout+"\"" + e1a2=exe +u" -add \""+paac2+"\" \""+pout+"\"" + e1s=exe +u" -add \""+pincap+"\" \""+pout+"\"" + if os.path.isfile(cap2ass): + txt=txt+execcomd(e0) + execmp4box(pin, pout, e1) + addmp4(paac1, pout, e1a1) + addmp4(paac2, pout, e1a2) + if os.path.exists(pincap) and (os.path.getsize(pincap)>1000): + addmp4(pincap, pout, e1s) + elif ispentaaudio==1: + wineexe=configreader.getpath("wine") + pincap=pin.replace(".264",".srt") + try: + cap2ass=configreader.getpath("caption2ass") + except: + cap2ass="" + pints=pin.replace(".264",".ts") + paac1=pin.replace(".264","_1.aac") + paac2=pin.replace(".264","_2.aac") + recdblist.printutf8(paac1) + if not os.path.exists(paac1): + paac1=pin.replace(".264","_1.mp3") + if not os.path.exists(paac2): + paac2=pin.replace(".264","_2.mp3") + e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pints+"\" \"Z:\\"+pincap+"\"" + e1=exe +u" "+duration+" -add \""+pin+"\" -new \""+pout+"\"" + e1a1=exe +u" -add \""+paac1+"\" \""+pout+"\"" + e1a2=exe +u" -add \""+paac2+"\" \""+pout+"\"" + e1s=exe +u" -add \""+pincap+"\" \""+pout+"\"" + if os.path.isfile(cap2ass): + txt=txt+execcomd(e0) + execmp4box(pin, pout, e1) + addmp4(paac1, pout, e1a1) + addmp4(paac2, pout, e1a2) + if os.path.exists(pincap) and (os.path.getsize(pincap)>1000): + addmp4(pincap, pout, e1s) + else: + wineexe=configreader.getpath("wine") + pincap=pin.replace(".264",".srt") + try: + cap2ass=configreader.getpath("caption2ass") + except: + cap2ass="" + pints=pin.replace(".264",".ts") + pinaac=pin.replace(".264",".aac") + e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pints+"\" \"Z:\\"+pincap+"\"" + e1=ffmpeg+" -i \""+pints+"\" -vn -f aac -acodec copy \""+pinaac+"\"" + e2=exe +u" "+duration+" -add \""+pin+"\" -new \""+pout+"\"" + e2a=exe +u" -add \""+pinaac+"\" \""+pout+"\"" + e2s=exe +u" -add \""+pincap+"\" \""+pout+"\"" + if os.path.isfile(cap2ass): + txt=txt+execcomd(e0) + txt=txt+execcomd(e1) + execmp4box(pin, pout, e2) + addmp4(pinaac, pout, e2a) + if os.path.exists(pincap) and (os.path.getsize(pincap)>1000): + addmp4(pincap, pout, e2s) + recdblist.addlog(pout, txt, "mp4box-log") + if status.getSettings_auto_del_tmp()==1: + if os.path.exists(pout): + if re.search(opts,"MW8") or re.search(opts,"MW9"): + if os.path.getsize(pout)>50*1000*1000: + deltitle(dir,title) + elif re.search(opts,"8") or re.search(opts,"9"): + "" + else: + if os.path.getsize(pout)>100*1000*1000: + deltitle(dir,title) +def mkv2mp4(pin,pout): + exeb = configreader.getpath(u"mkvextract") + exe = configreader.getpath(u"mp4Box") + #dtsedit=configreader.getpath("DtsEdit") + wineexe=configreader.getpath("wine") + dir=os.path.split(pin)[0] + title=os.path.split(pin)[1] + title=os.path.splitext(title)[0] + etitle=base64.b16encode(title.encode('utf-8')) + audiopath=os.path.join(dir,etitle+u"_audio.aac") + videopath=os.path.join(dir,etitle+u"_video.264") + timecodepath=os.path.join(dir,etitle+u"_1_timecode.txt") + tmpmp4=os.path.join(dir,etitle+u".tmp.mp4") + exe0=exeb+u" tracks \'"+pin+u"\' 1:\'"+videopath+u"\' 2:\'"+audiopath+u"\'" + exe1=exeb+u" timecodes_v2 \'"+pin+"\' 1:\'"+timecodepath+"\'" + exe2=exe+u" -fps 29.970030 -add \'"+videopath+u"\' -add \'"+audiopath+u"\' -new \'"+tmpmp4+u"\'" + exe3=wineexe+u" "+dtsedit+u" -tc \'Z:\\"+timecodepath+u"\' \'Z:\\"+tmpmp4+u"\' -o \'Z:\\"+pout+u"\'" + os.environ['LANG']="ja_JP.UTF-8" + txt="" + try: + txt=txt+execcomd(exe0)+"\n" + txt=txt+execcomd(exe1)+"\n" + txt=txt+execcomd(exe2)+"\n" + txt=txt+execcomd(exe3)+"\n" + except: + "" + recdblist.addlog(pin, txt, "MKV2MP4-log") + txt = "\n####MKV2MP4-log####\n"+txt + time.sleep(10) + if status.getSettings_auto_del_tmp()==1: + if os.path.exists(pout): + if os.path.getsize(pout)>100*1000*1000: + deltitle(dir,title) +def execmp4box(pin,pout,cmd): + title=os.path.splitext(os.path.split(pin)[1])[0] + ptin=os.path.join(os.path.dirname(pin),base64.b16encode(title.encode('utf-8'))+".264") + recdblist.printutf8(ptin) + shutil.move(pin,ptin) + time.sleep(10) + ptout=os.path.join(os.path.dirname(pout),base64.b16encode(title.encode('utf-8'))+".mp4") + cmdn=string.replace(cmd,pin,ptin) + cmdn=string.replace(cmdn,pout,ptout) + recdblist.printutf8(cmdn) + txt="" + try: + txt=execcomd(cmdn) + except Exception, inst: + txt= "error occures in execmp4box\n" + txt=txt+ str(type(inst))+"\n" + txt=txt+str(inst) + recdblist.addlog(pin, txt, "MP4Box-log") + time.sleep(5) + shutil.move(ptin,pin) + shutil.move(ptout,pout) + time.sleep(5) +def addmp4(padd,pout,cmd):#without video + title=os.path.splitext(os.path.split(padd)[1])[0] + ext=os.path.splitext(os.path.split(padd)[1])[1] + ptadd=os.path.join(os.path.dirname(padd),base64.b16encode(title.encode('utf-8'))+ext) + ptoutb=os.path.join(os.path.dirname(pout),base64.b16encode(title.encode('utf-8'))+"_b.mp4") + ptout=os.path.join(os.path.dirname(pout),base64.b16encode(title.encode('utf-8'))+".mp4") + shutil.move(padd,ptadd) + if os.path.isfile(pout): + shutil.move(pout,ptoutb) + time.sleep(5) + cmdn=string.replace(cmd,padd,ptadd) + cmdn=string.replace(cmdn,u"-out \""+pout,u"-add \""+ptoutb+"\" -new \""+ptout) + cmdn=string.replace(cmdn,u"\""+pout,u"-add \""+ptoutb+"\" -new \""+ptout) + cmdn=string.replace(cmdn,pout,ptout) + recdblist.printutf8(cmdn) + txt="" + try: + txt=execcomd(cmdn) + except Exception, inst: + txt= "error occures in addmp4\n" + txt=txt+ str(type(inst))+"\n" + txt=txt+str(inst) + recdblist.addlog(pout, txt, "MP4Box-log-add") + time.sleep(5) + os.remove(ptoutb) + shutil.move(ptadd,padd) + shutil.move(ptout,pout) + time.sleep(5) +def execcomd(cmd): + txt="" + try: + txt=u"Cmd : "+cmd+"\n" + txt2=commands.getoutput(cmd.encode('utf-8')) + txt=txt+unicode(txt2,"utf-8")+"\n" + except: + "" + return txt + +def deltitle(path,title): + dp=[] + dp.append(os.path.join(path,title+".avi")) + dp.append(os.path.join(path,title+".264")) + dp.append(os.path.join(path,title+".120.avi")) + dp.append(os.path.join(path,title+".noodml.avi")) + dp.append(os.path.join(path,title+".aac")) + dp.append(os.path.join(path,title+".m2v")) + dp.append(os.path.join(path,title+"_1.aac")) + dp.append(os.path.join(path,title+"_2.aac")) + dp.append(os.path.join(path,title+"_1.mp3")) + dp.append(os.path.join(path,title+"_2.mp3")) + dp.append(os.path.join(path,title+".srt")) + if configreader.getenv("remove_ts")=="1": + dp.append(os.path.join(path,title+".ts")) + if os.path.exists(os.path.join(path,title+".ts"))and os.path.exists(os.path.join(path,title+".ts.b25")): + if os.path.getsize(os.path.join(path,title+".ts"))*10>os.path.getsize(os.path.join(path,title+".ts.b25")): + if os.path.getsize(os.path.join(path,title+".ts"))>1000*1000*100: + dp.append(os.path.join(path,title+".ts.b25")) + if os.path.exists(os.path.join(path,title+".mp4")): + if os.path.getsize(os.path.join(path,title+".mp4"))>1000*1000*10: + for ip in dp: + try: + os.remove(ip) + except: + ""