OSDN Git Service

fix mp4 vfr.
[rec10/rec10-git.git] / rec10 / trunk / src / tv2mp4.py
index 03f87f6..25c11bc 100644 (file)
@@ -2,13 +2,17 @@
 # coding: UTF-8
 # Rec10 TS Recording Tools
 # Copyright (C) 2009 Yukikaze
-import sys
 import commands
+import shutil
 import tv2avi
 import os
+import re
 import os.path
+import base64
 import time
+import recdblist
 import configreader
+
 def ts2mp4(pin, pout, opt):
     dir=os.path.split(pout)[0]
     title=os.path.split(pout)[1]
@@ -19,72 +23,179 @@ def ts2mp4(pin, pout, opt):
     if not os.path.exists(tpavi):
         if os.path.exists(pin):
             tpavi=pin
-    avi2mp4(tpavi,pout)
-def avi2mp4(pin,pout):
+    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.mp3")
-    videopath=os.path.join(dir,title+u"_video.h264")
-    #doexe = exe.encode('utf-8') + u' \"' + pin.encode('utf-8') + u'\" \"' + pout.encode('utf-8') + u'\"'
-    exe1=exe+u" -aviraw audio \'"+pin+u"\'"
-    exe2=exe+u" -aviraw video \'"+pin+u"\'"
-    exefin=exe+u" -new -fps 29.97 -add \'"+videopath+u"\'#video -add \'"+audiopath+u"\'#audio \'"+pout+u"\'"
-    logo=pin.replace(u".avi",u".log")
+    etitle=base64.b16encode(title.encode('utf-8'))
+    audiopath=os.path.join(dir,etitle+u"_audio.raw")
+    videopath=os.path.join(dir,etitle+u"_video.264")
+    exes=[]
     os.environ['LANG']="ja_JP.UTF-8"
-    txt= exe1+u"\n"+exe2+u"\n"+exefin+u"\n"
-    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(exefin.encode('utf-8')),'utf-8')
-    os.remove(audiopath)
-    os.remove(videopath)
+    txt=""
+    if isvfr==1:
+        wineexe=configreader.getpath("wine")
+        cfr2tc=configreader.getpath("cfr2tc")
+        dtsedit=configreader.getpath("DtsEdit")
+        pin120=os.path.join(dir,etitle+u".120.avi")
+        pints=os.path.join(dir,title+u".ts")
+        pmp4=os.path.join(dir,etitle+u".tmp.mp4")
+        pintimecode=os.path.join(dir,etitle+u".timecode.txt")
+        pinaac=os.path.join(dir,etitle+u".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,etitle+u".120_video.h264")
+        evfr.append(exe+u" -fps 29.970030 -add \'"+videotpath+"\' -add \'"+pinaac+"\' -new \'"+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')
+            except:
+                ""
+            try:
+                cmdt=unicode(commands.getoutput(ex2.encode('utf-8')),'utf-8')
+            except:
+                ""
+            try:
+                cmdt=unicode(commands.getoutput(ex3.encode('utf-8')),'utf-8')
+            except:
+                ""
+            try:
+                cmdt=unicode(commands.getoutput(ex4.encode('utf-8')),'utf-8')
+            except:
+                ""
+            try:
+                cmdt=unicode(commands.getoutput(ex5.encode('utf-8')),'utf-8')
+            except:
+                ""
+            try:
+                os.remove(ptmp)
+            except:
+                ""
+            txt=txt+cmdt+"\n"
+    else:
+        try:
+            shutil.move(pin,os.path.join(dir,etitle+".avi"))
+        except:
+            ""
+        e0=exe+u" -aviraw audio \'"+os.path.join(dir,etitle+".avi")+u"\'"
+        e1=exe+u" -aviraw video \'"+os.path.join(dir,etitle+".avi")+u"\'"
+        os.system(e0)
+        os.system(e1)
+        if not os.path.exists(audiopath):
+            if os.path.exists(os.path.join(dir,etitle+u"_audio.mp3")):
+                audiopath=os.path.join(dir,etitle+u"_audio.mp3")
+            elif os.path.exists(os.path.join(dir,etitle+u"_audio.aac")):
+                audiopath=os.path.join(dir,etitle+u"_audio.aac")
+        else:
+            shutil.move(audiopath,os.path.join(dir,etitle+u"_audio.aac"))
+            audiopath=os.path.join(dir,etitle+u"_audio.aac")
+        ptmp=os.path.exists(os.path.join(dir,etitle+u".mp4"))
+        e2=exe+u" -fps 29.970030 -add \'"+videopath+u"\' -add \'"+audiopath+u"\' -new \'"+ptmp+u"\'"
+        os.system(e2)
+        shutil.move(ptmp,pout)
+    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()
-    sys.stdout.flush()
     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.264"))
+    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.mp3")
-    if not os.path.exists(audiopath):
-        if os.path.exists(os.path.join(dir,title+u"_audio.raw")):
-            audiopath=os.path.join(dir,title+u"_audio.raw")
-        if os.path.exists(os.path.join(dir,title+u"_audio.aac")):
-            audiopath=os.path.join(dir,title+u"_audio.aac")
-    videopath=os.path.join(dir,title+u"_video.h264")
-    #doexe = exe.encode('utf-8') + u' \"' + pin.encode('utf-8') + u'\" \"' + pout.encode('utf-8') + u'\"'
+    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=exe+u" -aviraw audio \'"+pin+u"\'"
-    exe2=exe+u" -aviraw video \'"+pin+u"\'"
-    exefin=exe+u" -new -fps 29.97 -add \'"+videopath+u"\'#video -add \'"+audiopath+u"\'#audio \'"+pout+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"\'"
     logo=pin.replace(".avi",".log")
     os.environ['LANG']="ja_JP.UTF-8"
-    txt= exe0+"\n"+exe1+"\n"+exe2+"\n"+exefin+"\n"
-    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(exefin.encode('utf-8')),'utf-8')
-    os.remove(audiopath)
-    os.remove(videopath)
+    txt= exe0+"\n"+exe1+"\n"+exe2+"\n"+exe3+"\n"
+    try:
+        txt = txt+unicode(commands.getoutput(exe0.encode('utf-8')),'utf-8')
+    except:
+        ""
+    try:
+        txt = txt+unicode(commands.getoutput(exe1.encode('utf-8')),'utf-8')
+    except:
+        ""
+    try:
+        txt = txt+unicode(commands.getoutput(exe2.encode('utf-8')),'utf-8')
+    except:
+        ""
+    try:
+        os.system(exe3.encode('utf-8'))
+    except:
+        ""
     txt = "\n####MKV2MP4-log####\n"+txt
     f=open(logo,'a')
     f.write(txt.encode('utf-8'))
     f.close()
-    sys.stdout.flush()
     time.sleep(10)
     if os.path.exists(pout):
         if os.path.exists(audiopath):
             os.remove(audiopath)
         if os.path.exists(videopath):
             os.remove(videopath)
+        if os.path.exists(timecodepath):
+            os.remove(timecodepath)
+        if os.path.exists(tmpmp4):
+            os.remove(tmpmp4)
\ No newline at end of file