OSDN Git Service

fix mp4box IO Error.
[rec10/rec10-git.git] / rec10 / trunk / src / tv2mp4.py
index 821a057..1facd36 100644 (file)
@@ -1,7 +1,7 @@
 #!/usr/bin/python\r
 # coding: UTF-8\r
 # Rec10 TS Recording Tools\r
-# Copyright (C) 2009-2010 Yukikaze\r
+# Copyright (C) 2009-2011 Yukikaze\r
 import commands\r
 import shutil\r
 import auto_process\r
@@ -12,13 +12,20 @@ import string
 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
-\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
@@ -31,8 +38,11 @@ def ts2mp4(pin, pout, opt):
         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):\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
@@ -40,15 +50,9 @@ def raw2mp4(pin,pout,opt):
     duration="-fps 29.970030 "\r
     if re.search("a",opt):\r
         duration="-fps 23.976023 "\r
-    elif re.search("I",opt):\r
+    if re.search("I",opt):\r
         duration="-fps 29.970030 "\r
-    elif re.search("v",opt):\r
-        duration="-fps 23.976023 "\r
-    if re.search("d",opt):\r
-        duration="-fps 29.970030 "##ffmpegが24fpsに対応していないための措置\r
-    elif re.search("5",opt):\r
-        duration="-fps 29.970030 "##ffmpegが24fpsに対応していないための措置\r
-    exe = configreader.getpath("mp4box")\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
@@ -56,21 +60,19 @@ def raw2mp4(pin,pout,opt):
     execmp4box(pin, pout, e1)\r
     addAudio(pints, pout, opt)\r
     addCaption(pints, pout)\r
-    #recdblist.addlog(pout, txt, "mp4boxログ-コマンド")\r
-    #recdblist.addlog(pout, txt, "mp4boxログ-詳細")\r
     if status.getSettings_auto_del_tmp()==1:\r
         if os.path.exists(pout):\r
-            if re.search(opt,"MW8") or re.search(opt,"MW9"):\r
-                auto_process.deltmpfile(dir, title, ".mp4")\r
-            elif re.search(opt,"8") or re.search(opt,"9"):\r
-                ""\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.deltmpfile(dir, title, ".mp4")\r
+                auto_process.deleteTmpFile(dir, title, ".mp4")\r
 def mkv2mp4(pin,pout):\r
-    exeb = configreader.getpath(u"mkvextract")\r
-    exe = configreader.getpath(u"mp4Box")\r
-    #dtsedit=configreader.getpath("DtsEdit")\r
-    wineexe=configreader.getpath("wine")\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
@@ -92,17 +94,17 @@ def mkv2mp4(pin,pout):
         txt=txt+execcomd(exe3)+"\n"\r
     except:\r
         ""\r
-    recdblist.addlog(pin, txt, u"MKV2MP4-log")\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.deltmpfile(dir, title, ".mp4")\r
+            auto_process.deleteTmpFile(dir, title, ".mp4")\r
 def addCaption(pts,pmp4):##字幕の追加を試みる。\r
-    wineexe=configreader.getpath("wine")\r
+    wineexe=configreader.getConfPath("wine")\r
     pincap=pts.replace(".ts",".srt")\r
     try:\r
-        cap2ass=configreader.getpath("caption2ass")\r
+        cap2ass=configreader.getConfPath("caption2ass")\r
     except:\r
         cap2ass=""\r
     if os.path.isfile(cap2ass):\r
@@ -116,16 +118,17 @@ def addCaption(pts,pmp4):##字幕の追加を試みる。
             #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.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.getpath("mp4box")\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,opt):#オプションに応じた音声の追加を行う\r
-    exe = configreader.getpath("mp4box")\r
-    if re.search("d",opt) or re.search("5",opt):#二カ国語放送/5.1ch放送の場合\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
@@ -135,15 +138,25 @@ def addAudio(pts,pmp4,opt):#オプションに応じた音声の追加を行う
             paac2=pts.replace(".ts","_2.mp3")\r
         e1a1=exe +u" -add \""+paac1+"\" \""+pmp4+"\""\r
         e1a2=exe +u" -add \""+paac2+"\" \""+pmp4+"\""\r
-        addmp4(paac1, pmp4, e1a1)\r
-        addmp4(paac2, pmp4, e1a2)\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)\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
-        addmp4(pinaac, pmp4, e1a)\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
@@ -157,6 +170,7 @@ def execmp4box(pin,pout,cmd):
     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
@@ -164,8 +178,10 @@ def execmp4box(pin,pout,cmd):
         txt= "error occures in execmp4box\n"\r
         txt=txt+ str(type(inst))+"\n"\r
         txt=txt+str(inst)\r
-    recdblist.addlog(pin, cmdn, u"MP4Boxログ-コマンド")\r
-    recdblist.addlog(pin, txt, u"MP4Boxログ-詳細")\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
@@ -195,8 +211,9 @@ def addmp4(padd,pout,cmd):#without video
         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.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
@@ -204,7 +221,7 @@ def addmp4(padd,pout,cmd):#without video
         os.remove(ptoutb)\r
     else:\r
         txtt=padd+u"のインポートエラー"\r
-        recdblist.addlog(pout, txtt, u"MP4Box追加ログ-コマンド")\r
+        recdblist.addLog(pout, txtt, u"MP4Box追加ログ-コマンド")\r
         shutil.move(ptoutb,pout)\r
     time.sleep(5)\r
 def execcomd(cmd):\r
@@ -215,4 +232,4 @@ def execcomd(cmd):
         txt=txt+unicode(txt2,"utf-8")+"\n"\r
     except:\r
         ""\r
-    return txt
\ No newline at end of file
+    return txt\r