OSDN Git Service

fix mp4box IO Error.
[rec10/rec10-git.git] / rec10 / trunk / src / tv2mp4.py
index 6b7e3cc..1facd36 100644 (file)
-#!/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\r
+# coding: UTF-8\r
+# Rec10 TS Recording Tools\r
+# Copyright (C) 2009-2011 Yukikaze\r
+import commands\r
+import shutil\r
+import auto_process\r
+import os\r
+import re\r
+import os.path\r
+import string\r
+import base64\r
+import time\r
+import subprocess\r
+import traceback\r
+import zip\r
+\r
+import tv2avi\r
+import recdblist\r
+import configreader\r
+import status\r
+import tv2audio\r
+path = str(os.path.dirname(os.path.abspath(__file__))) + "/"\r
+tmppath = configreader.getConfPath("tmp")+"/"\r
+if tmppath=="/":\r
+    tmppath=path\r
+if not os.path.exists(tmppath):\r
+    os.mkdir(tmppath)\r
+def ts2mp4(pin, pout, opt):\r
+    dir=os.path.split(pout)[0]\r
+    title=os.path.split(pout)[1]\r
+    title=os.path.splitext(title)[0]\r
+    tpraw=os.path.join(dir, title+".264")\r
+    tpmp4=os.path.join(dir, title+".mp4")\r
+    if os.path.isfile(pin) and os.path.getsize(pin)>10*1000:\r
+        tv2avi.ts2raw(pin, tpraw, opt)\r
+        time.sleep(10)\r
+        if os.path.isfile(tpraw) and os.path.getsize(tpraw)>10*1000:\r
+            raw2mp4(tpraw, tpmp4, opt)\r
+        time.sleep(10)\r
+        if os.path.exists(tpraw) and not re.search("B",opt):\r
+            os.remove(tpraw)\r
+    zip.addFile2FileZip(recdblist.getLogTitle(pin)+".command.log", recdblist.getLogTitle(pin)+".log.zip")\r
+    if os.path.exists(recdblist.getLogTitle(pin)+".command.log"):\r
+        os.remove(recdblist.getLogTitle(pin)+".command.log")\r
+def raw2mp4(pin,pout,opt):\r
+    dir=os.path.split(pout)[0]\r
+    title=os.path.split(pout)[1]\r
+    title=os.path.splitext(title)[0]\r
+    duration="-fps 29.970030 "\r
+    if re.search("a",opt):\r
+        duration="-fps 23.976023 "\r
+    if re.search("I",opt):\r
+        duration="-fps 29.970030 "\r
+    exe = configreader.getConfPath("mp4box")+u" -tmp "+tmppath\r
+    txt=""\r
+    os.environ['LANG']="ja_JP.UTF-8"\r
+    pints=pin.replace(".264",".ts")\r
+    e1=exe +u" "+duration+" -add \""+pin+"\" -new \""+pout+"\""\r
+    execmp4box(pin, pout, e1)\r
+    addAudio(pints, pout, opt)\r
+    addCaption(pints, pout)\r
+    if status.getSettings_auto_del_tmp()==1:\r
+        if os.path.exists(pout):\r
+            if re.search("t",opt):\r
+                auto_process.deleteTmpFile(dir, title, ".264")\r
+            elif re.search("k",opt):\r
+                ""#削除しない\r
+            else:\r
+                auto_process.deleteTmpFile(dir, title, ".mp4")\r
+def mkv2mp4(pin,pout):\r
+    exeb = configreader.getConfPath(u"mkvextract")\r
+    exe = configreader.getConfPath(u"mp4Box")\r
+    #dtsedit=configreader.getConfPath("DtsEdit")\r
+    wineexe=configreader.getConfPath("wine")\r
+    dir=os.path.split(pin)[0]\r
+    title=os.path.split(pin)[1]\r
+    title=os.path.splitext(title)[0]\r
+    etitle=base64.b16encode(title.encode('utf-8'))\r
+    audiopath=os.path.join(dir,etitle+u"_audio.aac")\r
+    videopath=os.path.join(dir,etitle+u"_video.264")\r
+    timecodepath=os.path.join(dir,etitle+u"_1_timecode.txt")\r
+    tmpmp4=os.path.join(dir,etitle+u".tmp.mp4")\r
+    exe0=exeb+u" tracks \'"+pin+u"\' 1:\'"+videopath+u"\' 2:\'"+audiopath+u"\'"\r
+    exe1=exeb+u" timecodes_v2 \'"+pin+"\' 1:\'"+timecodepath+"\'"\r
+    exe2=exe+u" -fps 29.970030 -add \'"+videopath+u"\' -add \'"+audiopath+u"\' -new \'"+tmpmp4+u"\'"\r
+    exe3=wineexe+u" "+dtsedit+u" -tc \'Z:\\"+timecodepath+u"\' \'Z:\\"+tmpmp4+u"\' -o \'Z:\\"+pout+u"\'"\r
+    os.environ['LANG']="ja_JP.UTF-8"\r
+    txt=""\r
+    try:\r
+        txt=txt+execcomd(exe0)+"\n"\r
+        txt=txt+execcomd(exe1)+"\n"\r
+        txt=txt+execcomd(exe2)+"\n"\r
+        txt=txt+execcomd(exe3)+"\n"\r
+    except:\r
+        ""\r
+    recdblist.addLog(pin, txt, u"MKV2MP4-log")\r
+    txt = "\n####MKV2MP4-log####\n"+txt\r
+    time.sleep(10)\r
+    if status.getSettings_auto_del_tmp()==1:\r
+        if os.path.exists(pout):\r
+            auto_process.deleteTmpFile(dir, title, ".mp4")\r
+def addCaption(pts,pmp4):##字幕の追加を試みる。\r
+    wineexe=configreader.getConfPath("wine")\r
+    pincap=pts.replace(".ts",".srt")\r
+    try:\r
+        cap2ass=configreader.getConfPath("caption2ass")\r
+    except:\r
+        cap2ass=""\r
+    if os.path.isfile(cap2ass):\r
+        e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pts+"\" \"Z:\\"+pincap+"\""\r
+        recdblist.printutf8(e0)\r
+        p0=subprocess.Popen(e0,shell=True,stdout=subprocess.PIPE)\r
+        time.sleep(100)\r
+        if p0.poll==None:#実行中\r
+            #if os.path.getsize(pincap)<1000:#2mで1kb以下の場合自動で終了\r
+             #   os.kill(p0.pid,signal.SIGKILL)\r
+            #else:\r
+            os.waitpid(p0.pid, 0)\r
+            logt=unicode(p0.communicate()[0], "UTF-8")\r
+            recdblist.addLog(pts,e0, u"Captionログ-コマンド")\r
+            recdblist.addLog(pts,logt, u"Captionログ-詳細")\r
+            recdblist.addCommandLogZip(pts, "mp4box_caption", "mp4box_caption", e0, logt)\r
+        if os.path.exists(pincap):\r
+            if os.path.getsize(pincap)>1000:\r
+                exe = configreader.getConfPath("mp4box")+u" -tmp "+tmppath\r
+                e1s=exe +u" -add \""+pincap+"\" \""+pmp4+"\""\r
+                addmp4(pincap,pmp4,e1s)\r
+def addAudio(pts,pmp4,opts):#オプションに応じた音声の追加を行う\r
+    exe = configreader.getConfPath("mp4box")+u" -tmp "+tmppath\r
+    if re.search("d",opts) or re.search("5",opts):#二カ国語放送/5.1ch放送の場合\r
+        paac1=pts.replace(".ts","_1.aac")\r
+        paac2=pts.replace(".ts","_2.aac")\r
+        recdblist.printutf8(paac1)\r
+        if not os.path.exists(paac1):\r
+            paac1=pts.replace(".ts","_1.mp3")\r
+        if not os.path.exists(paac2):\r
+            paac2=pts.replace(".ts","_2.mp3")\r
+        e1a1=exe +u" -add \""+paac1+"\" \""+pmp4+"\""\r
+        e1a2=exe +u" -add \""+paac2+"\" \""+pmp4+"\""\r
+        if os.path.exists(paac1):\r
+            addmp4(paac1, pmp4, e1a1)\r
+        if os.path.exists(paac2):\r
+            addmp4(paac2, pmp4, e1a2)\r
+    elif re.search("b",opts):#BonTsDemuxを使って音声をスプリットした場合\r
+        paac=pts.replace(".ts",".aac")\r
+        if not os.path.exists(paac):\r
+            paac=pts.replace(".ts",".mp3")\r
+        e1a1=exe +u" -add \""+paac+"\" \""+pmp4+"\""\r
+        if os.path.exists(paac):\r
+            addmp4(paac, pmp4, e1a1)\r
+    else:\r
+        tv2audio.ts2single_audio(pts,opts)\r
+        pinaac=pts.replace(".ts",".aac")\r
+        if not os.path.exists(pinaac):\r
+            pinaac=pinaac.replace(".aac",".mp3")\r
+        e1a=exe +u" -add \""+pinaac+"\" \""+pmp4+"\""\r
+        if os.path.exists(pinaac):\r
+            addmp4(pinaac, pmp4, e1a)\r
+def execmp4box(pin,pout,cmd):\r
+    title=os.path.splitext(os.path.split(pin)[1])[0]\r
+    nt=base64.b16encode(title.encode('utf-8'))\r
+    if len(nt)>200:\r
+        nt=nt[:180]\r
+    ptin=os.path.join(os.path.dirname(pin),nt+".264")\r
+    recdblist.printutf8(ptin)\r
+    shutil.move(pin,ptin)\r
+    time.sleep(10)\r
+    ptout=os.path.join(os.path.dirname(pout),nt+".mp4")\r
+    cmdn=string.replace(cmd,pin,ptin)\r
+    cmdn=string.replace(cmdn,pout,ptout)\r
+    recdblist.printutf8(cmdn)\r
+    recdblist.addCommandSelfLog(pin, cmdn)\r
+    txt=""\r
+    try:\r
+        txt=execcomd(cmdn)\r
+    except Exception, inst:\r
+        txt= "error occures in execmp4box\n"\r
+        txt=txt+ str(type(inst))+"\n"\r
+        txt=txt+str(inst)\r
+        recdblist.addCommonlogEX("Error", "excecmp4box(tv2mp4.py)", str(type(inst)), str(inst)+traceback.format_exc(),verbose_level=200,log_level=200)\r
+    recdblist.addLog(pin, cmdn, u"MP4Boxログ-コマンド")\r
+    recdblist.addLog(pin, txt, u"MP4Boxログ-詳細")\r
+    recdblist.addCommandLogZip(pin, "MP4Box", "mp4box", cmdn,txt)\r
+    time.sleep(5)\r
+    shutil.move(ptin,pin)\r
+    shutil.move(ptout,pout)\r
+    time.sleep(5)\r
+def addmp4(padd,pout,cmd):#without video\r
+    title=os.path.splitext(os.path.split(padd)[1])[0]\r
+    ext=os.path.splitext(os.path.split(padd)[1])[1]\r
+    nt=base64.b16encode(title.encode('utf-8'))\r
+    if len(nt)>200:\r
+        nt=nt[:180]\r
+    ptadd=os.path.join(os.path.dirname(padd),nt+ext)\r
+    ptoutb=os.path.join(os.path.dirname(pout),nt+"_b.mp4")\r
+    ptout=os.path.join(os.path.dirname(pout),nt+".mp4")\r
+    shutil.move(padd,ptadd)\r
+    if os.path.isfile(pout):\r
+        shutil.move(pout,ptoutb)\r
+    time.sleep(5)\r
+    cmdn=string.replace(cmd,padd,ptadd)\r
+    cmdn=string.replace(cmdn,u"-out \""+pout,u"-add \""+ptoutb+"\" -new \""+ptout)\r
+    cmdn=string.replace(cmdn,u"\""+pout,u"-add \""+ptoutb+"\" -new \""+ptout)\r
+    cmdn=string.replace(cmdn,pout,ptout)\r
+    recdblist.printutf8(cmdn)\r
+    txt=""\r
+    try:\r
+        txt=execcomd(cmdn)\r
+    except Exception, inst:\r
+        txt= "error occures in addmp4\n"\r
+        txt=txt+ str(type(inst))+"\n"\r
+        txt=txt+str(inst)\r
+    recdblist.addLog(pout, cmdn, u"MP4Box追加ログ-コマンド")\r
+    recdblist.addLog(pout, txt, u"MP4Box追加ログ-詳細")\r
+    recdblist.addCommandLogZip(pout, "MP4Box_add", "mp4box_add", cmdn, txt)\r
+    time.sleep(5)\r
+    shutil.move(ptadd,padd)\r
+    if os.path.exists(ptout):\r
+        shutil.move(ptout,pout)\r
+        os.remove(ptoutb)\r
+    else:\r
+        txtt=padd+u"のインポートエラー"\r
+        recdblist.addLog(pout, txtt, u"MP4Box追加ログ-コマンド")\r
+        shutil.move(ptoutb,pout)\r
+    time.sleep(5)\r
+def execcomd(cmd):\r
+    txt=""\r
+    try:\r
+        txt=u"Cmd : "+cmd+"\n"\r
+        txt2=commands.getoutput(cmd.encode('utf-8'))\r
+        txt=txt+unicode(txt2,"utf-8")+"\n"\r
+    except:\r
+        ""\r
+    return txt\r