OSDN Git Service

simplify codes.
[rec10/rec10-git.git] / rec10 / trunk / src / tv2mkv.py
index 44f3503..3516e25 100644 (file)
@@ -1,13 +1,13 @@
 #!/usr/bin/python
 # coding: UTF-8
 # Rec10 TS Recording Tools
-# Copyright (C) 2009 Yukikaze
-import sys
+# Copyright (C) 2009-2010 Yukikaze
+import status
+import os
 import commands
 import tv2avi
 import os.path
 import time
-import shutil
 import re
 import recdblist
 import configreader
@@ -15,49 +15,215 @@ def ts2mkv(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")
+    ##use avi
+    """tpavi=os.path.join(dir,title+".avi")
     tpmkv=os.path.join(dir, title+".mkv")
     tv2avi.ts2avi(pin, tpavi, opt)
     time.sleep(10)
-    isvfr=0
-    avi2mkv(tpavi,tpmkv,opt)
-    #shutil.move(tpavi,pout.replace(".mkv",".avi"))
-    #shutil.move(tpmkv,pout)
+    avi2mkv(tpavi,tpmkv,opt)"""
+    tpraw=os.path.join(dir, title+".264")
+    tpmkv=os.path.join(dir, title+".mkv")
+    tv2avi.ts2raw(pin, tpraw, opt)
+    time.sleep(10)
+    raw2mkv(tpraw, tpmkv, opt)
+def raw2mkv(pin,pout,opt):#x264
+    dir=os.path.split(pout)[0]
+    title=os.path.split(pout)[1]
+    title=os.path.splitext(title)[0]
+    duration="--default-duration 0:30000/1001fps "
+    ffmpeg=configreader.getpath("ffmpeg")
+    isdualaac=0
+    ispentaaudio=0
+    if re.search("a",opt):
+        duration="--default-duration 0:24000/1001fps "
+    elif re.search("v",opt):
+        duration="--default-duration 0:24000/1001fps "
+    if re.search("d",opt):
+        isdualaac=1
+        duration="--default-duration 0:30000/1001fps "##ffmpegが24fpsに対応していないための措置
+    elif re.search("5",opt):
+        ispentaaudio=1
+        duration="--default-duration 0:30000/1001fps "##ffmpegが24fpsに対応していないための措置
+    exe = configreader.getpath("mkvmerge")
+    txt=""
+    os.environ['LANG']="ja_JP.UTF-8"
+    if isdualaac==1:
+        wineexe=configreader.getpath("wine")
+        pincap=pin.replace(".264",".srt")
+        cap2ass=configreader.getpath("Caption2Ass")
+        pints=pin.replace(".264",".ts")
+        paac1=pin.replace(".264","_1.aac")
+        paac2=pin.replace(".264","_2.aac")
+        recdblist.printutf8(paac1)
+        if not os.path.exists(paac1):
+            paac1=pin.replace(".264","_1.mp3")
+        if not os.path.exists(paac2):
+            paac2=pin.replace(".264","_2.mp3")
+        e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pints+"\" \"Z:\\"+pincap+"\""
+        e1=exe +" -o \""+pout+u"\" --forced-track 0:no "+duration+"-d 0 -A -S \""+pin+"\" --forced-track 0:no -a 1 -D -S \""+paac1+"\" --forced-track 0:no -a 1 -D -S \""+paac2+"\" --track-order 0:0,1:1,2:1"
+        e1s=exe +" -o \""+pout+u"\" --forced-track 0:no "+duration+"-d 0 -A -S \""+pin+"\" --forced-track 0:no -a 1 -D -S \""+paac1+"\" --forced-track 0:no -a 1 -D -S \""+paac2+"\" --forced-track 0:no -s 0 -D -A \""+pincap+"\" --track-order 0:0,1:1,2:1,3:0"
+        if os.path.isfile(cap2ass):
+            txt=txt+execcomd(e0)
+        if os.path.exists(pincap) and (os.path.getsize(pincap)>1000):
+            txt=txt+u"\n Cmd:"+e1s+u"\n"+execcomd(e1s)
+        else:
+            txt=txt+u"\n Cmd:"+e1+u"\n"+execcomd(e1)
+    elif ispentaaudio==1:
+        wineexe=configreader.getpath("wine")
+        pincap=pin.replace(".264",".srt")
+        cap2ass=configreader.getpath("Caption2Ass")
+        pints=pin.replace(".264",".ts")
+        paac1=pin.replace(".264","_1.aac")
+        paac2=pin.replace(".264","_2.aac")
+        recdblist.printutf8(paac1)
+        if not os.path.exists(paac1):
+            paac1=pin.replace(".264","_1.mp3")
+        if not os.path.exists(paac2):
+            paac2=pin.replace(".264","_2.mp3")
+        e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pints+"\" \"Z:\\"+pincap+"\""
+        e1=exe +" -o \""+pout+u"\" --forced-track 0:no "+duration+"-d 0 -A -S \""+pin+"\" --forced-track 0:no -a 1 -D -S \""+paac1+"\" --forced-track 0:no -a 1 -D -S \""+paac2+"\" --track-order 0:0,1:1,2:1"
+        e1s=exe +" -o \""+pout+u"\" --forced-track 0:no "+duration+"-d 0 -A -S \""+pin+"\" --forced-track 0:no -a 1 -D -S \""+paac1+"\" --forced-track 0:no -a 1 -D -S \""+paac2+"\" --forced-track 0:no -s 0 -D -A \""+pincap+"\" --track-order 0:0,1:1,2:1,3:0"
+        if os.path.isfile(cap2ass):
+            txt=txt+execcomd(e0)
+        if os.path.exists(pincap) and (os.path.getsize(pincap)>1000):
+            txt=txt+u"\n Cmd:"+e1s+u"\n"+execcomd(e1s)
+        else:
+            txt=txt+u"\n Cmd:"+e1+u"\n"+execcomd(e1)
+    else:
+        wineexe=configreader.getpath("wine")
+        pincap=pin.replace(".264",".srt")
+        cap2ass=configreader.getpath("Caption2Ass")
+        pints=pin.replace(".264",".ts")
+        pinaac=pin.replace(".264",".aac")
+        e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pints+"\" \"Z:\\"+pincap+"\""
+        e1=ffmpeg+" -i \""+pints+"\" -vn -f aac -acodec copy \""+pinaac+"\""
+        e2=exe +" -o \""+pout+u"\" --forced-track 0:no "+duration+"-d 0 -A -S \""+pin+"\" --forced-track 0:no -a 0 -D -S \""+pinaac+"\" --track-order 0:0,1:0"
+        e2s=exe +" -o \""+pout+u"\" --forced-track 0:no "+duration+"-d 0 -A -S \""+pin+"\" --forced-track 0:no -a 0 -D -S \""+pinaac+"\" --forced-track 0:no -s 0 -D -A \""+pincap+"\" --track-order 0:0,1:0,2:0"
+        if os.path.isfile(cap2ass):
+            txt=txt+execcomd(e0)
+        txt=txt+execcomd(e1)
+        if os.path.exists(pincap) and (os.path.getsize(pincap)>1000):
+            txt=txt+u"\n Cmd:"+e2s+"\n"+execcomd(e2s)
+            recdblist.printutf8(e2s)
+        else:
+            txt=txt+u"\n Cmd:"+e2+"\n"+execcomd(e2)
+            recdblist.printutf8(e2)
+    recdblist.addlog(pout, txt, "mkvmerge-log")
+    logo=pout.replace(".mkv",".log")
+    if status.getSettings_auto_del_tmp()==1:
+        if os.path.exists(pout):
+            if os.path.getsize(pout)>100*1000*1000:
+                deltitle(dir,title)
 def avi2mkv(pin,pout,opt):
-    isvfr=0
-    if re.search("v", opt):
-        isvfr=1
+    dir=os.path.split(pout)[0]
+    title=os.path.split(pout)[1]
+    title=os.path.splitext(title)[0]
+    isdualaac=0
+    ispentaaudio=0
+    if re.search("d",opt):
+        isdualaac=1
+    elif re.search("5",opt):
+        ispentaaudio=1
     exe = configreader.getpath("mkvmerge")
-    doexe=""
-    doexe0=""
-    ffmpegdo=""
-    if isvfr==1:
+    txt=""
+    os.environ['LANG']="ja_JP.UTF-8"
+    if isdualaac==1:
         wineexe=configreader.getpath("wine")
-        cfr2tc=configreader.getpath("cfr2tc")
-        pin120=pin.replace(".avi",".120.avi")
+        pincap=pin.replace(".avi",".srt")
+        cap2ass=configreader.getpath("Caption2Ass")
         pints=pin.replace(".avi",".ts")
-        pintimecode=pin.replace(".avi",".timecode.txt")
-        pinaac=pin.replace(".avi",".aac")
-        ffmpegdo="ffmpeg -i '"+pints+"' -vn -f aac -acodec copy '"+pinaac+"'"
-        doexe0 = wineexe+u" "+cfr2tc+u" '"+"Z:\\"+pin+"' '"+"Z:\\"+pin120+u"' '"+"Z:\\"+pintimecode+u"' 2"
-        doexe = exe +" -o '"+pout+"'  --forced-track 0:no --timecodes '0:"+pintimecode+"' -d 0 -A -S '"+pin120+"' --sync 0:-250 --forced-track 0:no -a 0 -D -S '"+pinaac+"' --track-order 0:0,1:0"
+        paac1=pin.replace(".avi","_1.aac")
+        paac2=pin.replace(".avi","_2.aac")
+        recdblist.printutf8(paac1)
+        if not os.path.exists(paac1):
+            paac1=pin.replace(".avi","_1.mp3")
+        if not os.path.exists(paac2):
+            paac2=pin.replace(".avi","_2.mp3")
+        e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pints+"\" \"Z:\\"+pincap+"\""
+        e1=exe +" -o \""+pout+u"\"  --forced-track 0:no -d 0 -A -S \""+pin+"\" --sync 0:0 --forced-track 0:no -a 1 -D -S \""+paac1+"\" --forced-track 0:no -a 1 -D -S \""+paac2+"\" --track-order 0:0,1:1,2:1"
+        e1s=exe +" -o \""+pout+u"\"  --forced-track 0:no -d 0 -A -S \""+pin+"\" --sync 0:0 --forced-track 0:no -a 1 -D -S \""+paac1+"\" --forced-track 0:no -a 1 -D -S \""+paac2+"\" --forced-track 0:no -s 0 -D -A \""+pincap+"\" --track-order 0:0,1:1,2:1,3:0"
+        if os.path.isfile(cap2ass):
+            txt=txt+execcomd(e0)
+        if os.path.exists(pincap) and (os.path.getsize(pincap)>1000):
+            txt=txt+execcomd(e1s)
+        else:
+            txt=txt+execcomd(e1)
+    elif ispentaaudio==1:
+        wineexe=configreader.getpath("wine")
+        pincap=pin.replace(".avi",".srt")
+        cap2ass=configreader.getpath("Caption2Ass")
+        pints=pin.replace(".avi",".ts")
+        paac1=pin.replace(".avi","_1.aac")
+        paac2=pin.replace(".avi","_2.aac")
+        recdblist.printutf8(paac1)
+        if not os.path.exists(paac1):
+            paac1=pin.replace(".avi","_1.mp3")
+        if not os.path.exists(paac2):
+            paac2=pin.replace(".avi","_2.mp3")
+        e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pints+"\" \"Z:\\"+pincap+"\""
+        e1=exe +" -o \""+pout+u"\"  --forced-track 0:no -d 0 -A -S \""+pin+"\" --sync 0:0 --forced-track 0:no -a 1 -D -S \""+paac1+"\" --forced-track 0:no -a 1 -D -S \""+paac2+"\" --track-order 0:0,1:1,2:1"
+        e1s=exe +" -o \""+pout+u"\"  --forced-track 0:no -d 0 -A -S \""+pin+"\" --sync 0:0 --forced-track 0:no -a 1 -D -S \""+paac1+"\" --forced-track 0:no -a 1 -D -S \""+paac2+"\" --forced-track 0:no -s 0 -D -A \""+pincap+"\" --track-order 0:0,1:1,2:1,3:0"
+        if os.path.isfile(cap2ass):
+            txt=txt+execcomd(e0)
+        if os.path.exists(pincap) and (os.path.getsize(pincap)>1000):
+            txt=txt+execcomd(e1s)
+        else:
+            txt=txt+execcomd(e1)
     else:
-        doexe = exe + u" -o '" + pout + u"' --cues 0:all --forced-track 0:no --forced-track 1:no -a 1 -d 0 -S '" + pin + u"' --track-order 0:0,0:1"
-    doexe = "nice -n 17 " + doexe
-    recdblist.printutf8(doexe)
+        wineexe=configreader.getpath("wine")
+        pincap=pin.replace(".avi",".srt")
+        cap2ass=configreader.getpath("Caption2Ass")
+        pints=pin.replace(".avi",".ts")
+        pinaac=pin.replace(".avi",".aac")
+        e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pints+"\" \"Z:\\"+pincap+"\""
+        e1=ffmpeg+u" -i \""+pints+u"\" -vn -f aac -acodec copy \""+pinaac+"\""
+        e2=exe +" -o \""+pout+u"\"  --forced-track 0:no -d 0 -A -S \""+pin+"\" --sync 0:0 --forced-track 0:no -a 0 -D -S \""+pinaac+"\" --track-order 0:0,1:0"
+        e2s=exe +" -o \""+pout+u"\"  --forced-track 0:no -d 0 -A -S \""+pin+"\" --sync 0:0 --forced-track 0:no -a 0 -D -S \""+pinaac+"\" --forced-track 0:no -s 0 -D -A \""+pincap+"\" --track-order 0:0,1:0,2:0"
+        if os.path.isfile(cap2ass):
+            txt=txt+execcomd(e0)
+        txt=txt+execcomd(e1)
+        if os.path.exists(pincap) and (os.path.getsize(pincap)>1000):
+            txt=txt+execcomd(e2s)
+        else:
+            txt=txt+execcomd(e2)
+    recdblist.addlog(pout, txt, "mkvmerge-log")
     logo=pout.replace(".mkv",".log")
+    if status.getSettings_auto_del_tmp()==1:
+        if os.path.exists(pout):
+            if os.path.getsize(pout)>100*1000*1000:
+                deltitle(dir,title)
+def execcomd(cmd):
     txt=""
-    os.environ['LANG']="ja_JP.UTF-8"
-    if doexe0!="":
-        recdblist.printutf8(ffmpegdo)
-        txt= ffmpegdo+"\n"
-        txt=txt+unicode(commands.getoutput(ffmpegdo.encode('utf-8')),'utf-8')
-        recdblist.printutf8(doexe0)
-        txt= doexe0+"\n"
-        txt=txt+unicode(commands.getoutput(doexe0.encode('utf-8')),'utf-8')
-    txt= txt+ doexe+"\n"
-    txt = txt+unicode(commands.getoutput(doexe.encode('utf-8')),'utf-8')
-    txt = "\n####mkvmerge-log####\n"+txt
-    f=open(logo,'a')
-    f.write(txt.encode('utf-8'))
-    f.close()
\ No newline at end of file
+    cmdt=u"nice -n 17 "+cmd
+    try:
+        txt=u"Cmd : "+cmd+"\n"
+        txt2=commands.getoutput(cmdt.encode('utf-8'))
+        txt=txt+unicode(txt2,"utf-8")+"\n"
+    except:
+        ""
+    return txt
+def deltitle(path,title):
+    dp=[]
+    dp.append(os.path.join(path,title+".avi"))
+    dp.append(os.path.join(path,title+".264"))
+    dp.append(os.path.join(path,title+".120.avi"))
+    dp.append(os.path.join(path,title+".noodml.avi"))
+    dp.append(os.path.join(path,title+".aac"))
+    dp.append(os.path.join(path,title+".m2v"))
+    dp.append(os.path.join(path,title+"_1.aac"))
+    dp.append(os.path.join(path,title+"_2.aac"))
+    dp.append(os.path.join(path,title+"_1.mp3"))
+    dp.append(os.path.join(path,title+"_2.mp3"))
+    dp.append(os.path.join(path,title+".srt"))
+    if os.path.exists(os.path.join(path,title+".ts"))and os.path.exists(os.path.join(path,title+".ts.b25")):
+        if os.path.getsize(os.path.join(path,title+".ts"))*10>os.path.getsize(os.path.join(path,title+".ts.b25")):
+            if os.path.getsize(os.path.join(path,title+".ts"))>1000*1000*100:
+                dp.append(os.path.join(path,title+".ts.b25"))
+    if os.path.exists(os.path.join(path,title+".mkv")):
+        if os.path.getsize(os.path.join(path,title+".mkv"))>1000*1000*10:
+            for ip in dp:
+                try:
+                    os.remove(ip)
+                except:
+                    ""
+    
\ No newline at end of file