OSDN Git Service

try to fix caption system.
[rec10/rec10-git.git] / rec10 / trunk / src / tv2mp4.py
index dcb13d1..cbbc256 100644 (file)
-#!/usr/bin/python
-# coding: UTF-8
-# Rec10 TS Recording Tools
-# Copyright (C) 2009 Yukikaze
-import sys
-import commands
-import tv2avi
-import os
-import os.path
-import time
-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)
-def avi2mp4(pin,pout):
-    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 u'"+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")
-    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 = "\n####MP4Box-log####\n"+txt
-    f=open(logo,'a')
-    f.write(txt)
-    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)
-def mkv2mp4(pin,pout):
-    exeb = configreader.getpath("mkvextract")
-    exe = configreader.getpath("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+"_audio.mp3")
-    if not os.path.exists(audiopath):
-        if os.path.exists(os.path.join(dir,title+"_audio.raw")):
-            audiopath=os.path.join(dir,title+"_audio.raw")
-        if os.path.exists(os.path.join(dir,title+"_audio.aac")):
-            audiopath=os.path.join(dir,title+"_audio.aac")
-    videopath=os.path.join(dir,title+"_video.h264")
-    #doexe = exe.encode('utf-8') + u' \"' + pin.encode('utf-8') + u'\" \"' + pout.encode('utf-8') + u'\"'
-    exe0=exeb+" tracks \'"+pin+"\' 1:\'"+videopath+"\' 2:\'"+audiopath+"\'"
-    exe1=exe+" -aviraw audio \'"+pin+"\'"
-    exe2=exe+" -aviraw video \'"+pin+"\'"
-    exefin=exe+" -new -fps 29.97 -add \'"+videopath+"\'#video -add \'"+audiopath+"\'#audio \'"+pout+"\'"
-    logo=pin.replace(".avi",".log")
-    os.environ['LANG']="ja_JP.UTF-8"
-    txt= exe0+"\n"+exe1+"\n"+exe2+"\n"+exefin+"\n"
-    txt = txt+commands.getoutput(exe0.encode('utf-8'))
-    txt = txt+commands.getoutput(exe1.encode('utf-8'))
-    txt = txt+commands.getoutput(exe2.encode('utf-8'))
-    txt = txt+commands.getoutput(exefin.encode('utf-8'))
-    os.remove(audiopath)
-    os.remove(videopath)
-    txt = "\n####MKV2MP4-log####\n"+txt
-    f=open(logo,'a')
-    f.write(txt)
-    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)
+#!/usr/bin/python\r
+# coding: UTF-8\r
+# Rec10 TS Recording Tools\r
+# Copyright (C) 2009-2010 Yukikaze\r
+import commands\r
+import shutil\r
+import tv2avi\r
+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
+import subprocess\r
+import signal\r
+import tv2audio\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
+    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):\r
+            os.remove(tpraw)\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
+    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
+    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
+    recdblist.addlog(pout, txt, "mp4box-log")\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
+                if os.path.getsize(pout)>50*1000*1000:\r
+                    deltitle(dir,title)\r
+            elif re.search(opt,"8") or re.search(opt,"9"):\r
+                ""\r
+            else:\r
+                if os.path.getsize(pout)>100*1000*1000:\r
+                    deltitle(dir,title)\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
+    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, "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
+            if os.path.getsize(pout)>100*1000*1000:\r
+                deltitle(dir,title)\r
+\r
+def addCaption(pts,pmp4):##字幕の追加を試みる。\r
+    wineexe=configreader.getpath("wine")\r
+    pincap=pts.replace(".ts",".srt")\r
+    try:\r
+        cap2ass=configreader.getpath("caption2ass")\r
+    except:\r
+        cap2ass=""\r
+    if os.path.isfile(cap2ass):\r
+        print 1\r
+        e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pts+"\" \"Z:\\"+pincap+"\""\r
+        recdblist.printutf8(e0)\r
+        print 2\r
+        p0=subprocess.Popen(e0,shell=True,env="LANG=ja_JP.UTF-8",stdout=subprocess.PIPE)\r
+        print 3\r
+        time.sleep(120)\r
+        print p0.poll\r
+        print 4\r
+        if p0.poll==None:#実行中\r
+            print "4a"\r
+            if os.path.getsize(pincap)<1000:#2mで1kb以下の場合自動で終了\r
+                print "5a"\r
+                os.kill(p0.pid,signal.SIGKILL)\r
+            else:\r
+                print "4b"\r
+                os.waitpid(p0.pid, 0)\r
+                print "5b"\r
+        logt=unicode(p0.communicate, "UTF-8")\r
+        recdblist.printutf8(logt)\r
+        recdblist.addlog(pts,logt, "Caption-Log")\r
+        print "6"\r
+        if os.path.getsize(pincap)>1000:\r
+            print "7"\r
+            exe = configreader.getpath("mp4box")\r
+            print "8"\r
+            e1s=exe +u" -add \""+pincap+"\" \""+pout+"\""\r
+            print "9"\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
+        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
+        addmp4(paac1, pmp4, e1a1)\r
+        addmp4(paac2, pmp4, e1a2)\r
+    else:\r
+        tv2audio.ts2single_audio(pts)\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
+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
+    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
+    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, 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
+        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
+\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 configreader.getenv("remove_ts")=="1":\r
+        dp.append(os.path.join(path,title+".ts"))\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