OSDN Git Service

simplify codes.
[rec10/rec10-git.git] / rec10 / trunk / src / tv2mp4.py
index 5ec0fbf..ac30a9c 100644 (file)
@@ -8,22 +8,114 @@ import tv2avi
 import os\r
 import re\r
 import os.path\r
+import string\r
 import base64\r
 import time\r
 import recdblist\r
 import configreader\r
+import status\r
 \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
-    tpavi=os.path.join(dir, title+".avi")\r
-    tv2avi.ts2avi(pin, tpavi, opt)\r
+    tpraw=os.path.join(dir, title+".264")\r
+    tpmp4=os.path.join(dir, title+".mp4")\r
+    tv2avi.ts2raw(pin, tpraw, opt)\r
     time.sleep(10)\r
-    if not os.path.exists(tpavi):\r
-        if os.path.exists(pin):\r
-            tpavi=pin\r
-    avi2mp4(tpavi,pout,opt)\r
+    raw2mp4(tpraw, tpmp4, opt)\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
+    ffmpeg=configreader.getpath("ffmpeg")\r
+    isdualaac=0\r
+    ispentaaudio=0\r
+    if re.search("a",opt):\r
+        duration="-fps 23.976023 "\r
+    elif re.search("v",opt):\r
+        duration="-fps 23.976023 "\r
+    if re.search("d",opt):\r
+        isdualaac=1\r
+        duration="-fps 29.970030 "##ffmpegが24fpsに対応していないための措置\r
+    elif re.search("5",opt):\r
+        ispentaaudio=1\r
+        duration="-fps 29.970030 "##ffmpegが24fpsに対応していないための措置\r
+    exe = configreader.getpath("mp4box")\r
+    txt=""\r
+    os.environ['LANG']="ja_JP.UTF-8"\r
+    if isdualaac==1:\r
+        wineexe=configreader.getpath("wine")\r
+        pincap=pin.replace(".264",".srt")\r
+        cap2ass=configreader.getpath("Caption2Ass")\r
+        pints=pin.replace(".264",".ts")\r
+        paac1=pin.replace(".264","_1.aac")\r
+        paac2=pin.replace(".264","_2.aac")\r
+        recdblist.printutf8(paac1)\r
+        if not os.path.exists(paac1):\r
+            paac1=pin.replace(".264","_1.mp3")\r
+        if not os.path.exists(paac2):\r
+            paac2=pin.replace(".264","_2.mp3")\r
+        e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pints+"\" \"Z:\\"+pincap+"\""\r
+        e1=exe +u" "+duration+" -add \""+pin+"\" -new \""+pout+"\""\r
+        e1a1=exe +u" -add \""+paac1+"\" \""+pout+"\""\r
+        e1a2=exe +u" -add \""+paac2+"\" \""+pout+"\""\r
+        e1s=exe +u" -add \""+pincap+"\" \""+pout+"\""\r
+        if os.path.isfile(cap2ass):\r
+            txt=txt+execcomd(e0)\r
+        execmp4box(pin, pout, e1)\r
+        addmp4(paac1, pout, e1a1)\r
+        addmp4(paac2, pout, e1a2)\r
+        if os.path.exists(pincap) and (os.path.getsize(pincap)>1000):\r
+            addmp4(pincap, pout, e1s)\r
+    elif ispentaaudio==1:\r
+        wineexe=configreader.getpath("wine")\r
+        pincap=pin.replace(".264",".srt")\r
+        cap2ass=configreader.getpath("Caption2Ass")\r
+        pints=pin.replace(".264",".ts")\r
+        paac1=pin.replace(".264","_1.aac")\r
+        paac2=pin.replace(".264","_2.aac")\r
+        recdblist.printutf8(paac1)\r
+        if not os.path.exists(paac1):\r
+            paac1=pin.replace(".264","_1.mp3")\r
+        if not os.path.exists(paac2):\r
+            paac2=pin.replace(".264","_2.mp3")\r
+        e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pints+"\" \"Z:\\"+pincap+"\""\r
+        e1=exe +u" "+duration+" -add \""+pin+"\" -new \""+pout+"\""\r
+        e1a1=exe +u" -add \""+paac1+"\" \""+pout+"\""\r
+        e1a2=exe +u" -add \""+paac2+"\" \""+pout+"\""\r
+        e1s=exe +u" -add \""+pincap+"\" \""+pout+"\""\r
+        if os.path.isfile(cap2ass):\r
+            txt=txt+execcomd(e0)\r
+        execmp4box(pin, pout, e1)\r
+        addmp4(paac1, pout, e1a1)\r
+        addmp4(paac2, pout, e1a2)\r
+        if os.path.exists(pincap) and (os.path.getsize(pincap)>1000):\r
+            addmp4(pincap, pout, e1s)\r
+    else:\r
+        wineexe=configreader.getpath("wine")\r
+        pincap=pin.replace(".264",".srt")\r
+        cap2ass=configreader.getpath("Caption2Ass")\r
+        pints=pin.replace(".264",".ts")\r
+        pinaac=pin.replace(".264",".aac")\r
+        e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pints+"\" \"Z:\\"+pincap+"\""\r
+        e1=ffmpeg+" -i \""+pints+"\" -vn -f aac -acodec copy \""+pinaac+"\""\r
+        e2=exe +u" "+duration+" -add \""+pin+"\" -new \""+pout+"\""\r
+        e2a=exe +u" -add \""+pinaac+"\" \""+pout+"\""\r
+        e2s=exe +u" -add \""+pincap+"\" \""+pout+"\""\r
+        if os.path.isfile(cap2ass):\r
+            txt=txt+execcomd(e0)\r
+        txt=txt+execcomd(e1)\r
+        execmp4box(pin, pout, e2)\r
+        addmp4(pinaac, pout, e2a)\r
+        if os.path.exists(pincap) and (os.path.getsize(pincap)>1000):\r
+            addmp4(pincap, pout, e2s)\r
+    recdblist.addlog(pout, txt, "mp4box-log")\r
+    if status.getSettings_auto_del_tmp()==1:\r
+        if os.path.exists(pout):\r
+            if os.path.getsize(pout)>100*1000*1000:\r
+                deltitle(dir,title)\r
 def avi2mp4(pin,pout,opt):\r
     isvfr=0\r
     if re.search(u"v", opt):\r
@@ -49,7 +141,7 @@ def avi2mp4(pin,pout,opt):
         pintimecode=os.path.join(dir,etitle+u".timecode.txt")\r
         pinaac=os.path.join(dir,etitle+u".aac")\r
         pincat=os.path.join(dir,etitle+u".srt")\r
-        evfr0="ffmpeg -i '"+pints+"' -vn -f aac -acodec copy '"+pinaac+"'"\r
+        evfr0=ffmpeg+u" -i '"+pints+"' -vn -f aac -acodec copy '"+pinaac+"'"\r
         evfr1=wineexe+u" "+cfr2tc+u" '"+"Z:\\"+pin+"' '"+"Z:\\"+pin120+u"' 'Z:\\"+pintimecode+u"' 2"\r
         evfr2=exe+u" -aviraw video \'"+pin120+u"\'"\r
         videotpath=os.path.join(dir,etitle+u".120_video.h264")\r
@@ -90,7 +182,7 @@ def avi2mp4(pin,pout,opt):
         except:\r
             ""\r
         if (not os.path.exists(pin.replace(".avi",".120.avi"))and (not os.path.exists(pout))):\r
-            txt=txt+"CFR2TC error. maybe avi file is over 1GB.(ODML error would have happend.)"\r
+            txt=txt+"CFR2TC error. maybe avi file is over 1GB.(ODML error would have happened.)"\r
             ptmp=pin.replace(".avi",".noodml.avi")\r
             e="mencoder -noodml -forceidx -oac copy -ovc copy -o \'"+ptmp+"\'"+" \'"+pin+"\'"\r
             ex="nice -n 17 "+e\r
@@ -152,15 +244,16 @@ def avi2mp4(pin,pout,opt):
         e2=exe+u" -fps 29.970030 -add \'"+videopath+u"\' -add \'"+audiopath+u"\' -new \'"+ptmp+u"\'"\r
         os.system(e2)\r
         shutil.move(ptmp,pout)\r
-    logo=pin.replace(u".avi",u".log")\r
-    txt = "\n####MP4Box-log####\n"+txt\r
-    f=open(logo,'a')\r
-    f.write(txt.encode('utf-8'))\r
-    f.close()\r
+    recdblist.addlog(pin,txt,"MP4Box-log")\r
     time.sleep(10)\r
     delpath=[]\r
     delpath.append(pin.replace(u".avi",u".120.avi"))\r
     delpath.append(pin.replace(u".avi",u".sa.avi"))\r
+    delpath.append(pin.replace(u".avi",u".m2v"))\r
+    delpath.append(pin.replace(u".avi",u"_1.aac"))\r
+    delpath.append(pin.replace(u".avi",u"_2.aac"))\r
+    delpath.append(pin.replace(u".avi",u"_1.mp3"))\r
+    delpath.append(pin.replace(u".avi",u"_2.mp3"))\r
     delpath.append(pin.replace(u".avi",u".ts"))\r
     delpath.append(pin.replace(u".avi",u".aac"))\r
     delpath.append(pin.replace(u".avi",u".m2v"))\r
@@ -195,17 +288,17 @@ def mkv2mp4(pin,pout):
     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
-    logo=pin.replace(".avi",".log")\r
     os.environ['LANG']="ja_JP.UTF-8"\r
     txt=""\r
-    execcomd(exe0)\r
-    execcomd(exe1)\r
-    execcomd(exe2)\r
-    execcomd(exe3)\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, "MKV2MP4-log")\r
     txt = "\n####MKV2MP4-log####\n"+txt\r
-    f=open(logo,'a')\r
-    f.write(txt.encode('utf-8'))\r
-    f.close()\r
     time.sleep(10)\r
     if os.path.exists(pout):\r
         if os.path.exists(audiopath):\r
@@ -216,6 +309,56 @@ def mkv2mp4(pin,pout):
             os.remove(timecodepath)\r
         if os.path.exists(tmpmp4):\r
             os.remove(tmpmp4)\r
+def execmp4box(pin,pout,cmd):\r
+    title=os.path.splitext(os.path.split(pin)[1])[0]\r
+    ptin=os.path.join(os.path.dirname(pin),base64.b16encode(title.encode('utf-8'))+".264")\r
+    recdblist.printutf8(ptin)\r
+    shutil.move(pin,ptin)\r
+    time.sleep(10)\r
+    ptout=os.path.join(os.path.dirname(pout),base64.b16encode(title.encode('utf-8'))+".mp4")\r
+    cmdn=string.replace(cmd,pin,ptin)\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 execmp4box\n"\r
+        txt=txt+ str(type(inst))+"\n"\r
+        txt=txt+str(inst)\r
+    recdblist.addlog(pin, txt, "MP4Box-log")\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
+    ptadd=os.path.join(os.path.dirname(padd),base64.b16encode(title.encode('utf-8'))+ext)\r
+    ptoutb=os.path.join(os.path.dirname(pout),base64.b16encode(title.encode('utf-8'))+"_b.mp4")\r
+    ptout=os.path.join(os.path.dirname(pout),base64.b16encode(title.encode('utf-8'))+".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, txt, "MP4Box-log-add")\r
+    time.sleep(5)\r
+    os.remove(ptoutb)\r
+    shutil.move(ptadd,padd)\r
+    shutil.move(ptout,pout)\r
+    time.sleep(5)\r
 def execcomd(cmd):\r
     txt=""\r
     try:\r
@@ -225,3 +368,28 @@ def execcomd(cmd):
     except:\r
         ""\r
     return txt\r
+\r
+def deltitle(path,title):\r
+    dp=[]\r
+    dp.append(os.path.join(path,title+".avi"))\r
+    dp.append(os.path.join(path,title+".264"))\r
+    dp.append(os.path.join(path,title+".120.avi"))\r
+    dp.append(os.path.join(path,title+".noodml.avi"))\r
+    dp.append(os.path.join(path,title+".aac"))\r
+    dp.append(os.path.join(path,title+".m2v"))\r
+    dp.append(os.path.join(path,title+"_1.aac"))\r
+    dp.append(os.path.join(path,title+"_2.aac"))\r
+    dp.append(os.path.join(path,title+"_1.mp3"))\r
+    dp.append(os.path.join(path,title+"_2.mp3"))\r
+    dp.append(os.path.join(path,title+".srt"))\r
+    if os.path.exists(os.path.join(path,title+".ts"))and os.path.exists(os.path.join(path,title+".ts.b25")):\r
+        if os.path.getsize(os.path.join(path,title+".ts"))*10>os.path.getsize(os.path.join(path,title+".ts.b25")):\r
+            if os.path.getsize(os.path.join(path,title+".ts"))>1000*1000*100:\r
+                dp.append(os.path.join(path,title+".ts.b25"))\r
+    if os.path.exists(os.path.join(path,title+".mp4")):\r
+        if os.path.getsize(os.path.join(path,title+".mp4"))>1000*1000*10:\r
+            for ip in dp:\r
+                try:\r
+                    os.remove(ip)\r
+                except:\r
+                    ""\r