OSDN Git Service

add caption.
[rec10/rec10-git.git] / rec10 / trunk / src / tv2mkv.py
1 #!/usr/bin/python
2 # coding: UTF-8
3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009 Yukikaze
5 import os
6 import commands
7 import tv2avi
8 import os.path
9 import time
10 import re
11 import recdblist
12 import configreader
13 def ts2mkv(pin, pout, opt):
14     dir=os.path.split(pout)[0]
15     title=os.path.split(pout)[1]
16     title=os.path.splitext(title)[0]
17     tpavi=os.path.join(dir,title+".avi")
18     tpmkv=os.path.join(dir, title+".mkv")
19     tv2avi.ts2avi(pin, tpavi, opt)
20     time.sleep(10)
21     avi2mkv(tpavi,tpmkv,opt)
22 def avi2mkv(pin,pout,opt):
23     isvfr=0
24     if re.search("v", opt):
25         isvfr=1
26     exe = configreader.getpath("mkvmerge")
27     exes=[]
28     txt=""
29     os.environ['LANG']="ja_JP.UTF-8"
30     if isvfr==1:
31         wineexe=configreader.getpath("wine")
32         cfr2tc=configreader.getpath("cfr2tc")
33         cap2ass=configreader.getpath("Caption2Ass")
34         pin120=pin.replace(".avi",".120.avi")
35         pints=pin.replace(".avi",".ts")
36         pintimecode=pin.replace(".avi",".timecode.txt")
37         pincap=pin.replace(".avi",".srt")
38         pinaac=pin.replace(".avi",".aac")
39         e0="ffmpeg -i '"+pints+"' -vn -f aac -acodec copy '"+pinaac+"'"
40         e1=wineexe+u" "+cfr2tc+u" '"+"Z:\\"+pin+"' '"+"Z:\\"+pin120+u"' '"+"Z:\\"+pintimecode+u"' 2"
41         e2=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pints+"\" Z:\\"+pincap+"\""
42         e3=exe +" -o '"+pout+u"'  --forced-track 0:no --timecodes '0:"+pintimecode+"' -d 0 -A -S '"+pin120+"' --sync 0:-225 --forced-track 0:no -a 0 -D -S '"+pinaac+"' --track-order 0:0,1:0"
43         e3s=exe +" -o '"+pout+u"'  --forced-track 0:no --timecodes '0:"+pintimecode+"' -d 0 -A -S '"+pin120+"' --sync 0:-225 --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"
44         #exes.append(exe +" -o '"+pout+u"'  --forced-track 0:no --timecodes '0:"+pintimecode+"' -d 0 -A -S '"+pin120+"' --sync 0:0 --forced-track 0:no -a 0 -D -S '"+pinaac+"' --track-order 0:0,1:0")
45         #exes.append(doexe = exe + u" -o '" + pout + u"' --cues 0:all --forced-track 0:no --default-duration 0:120000/1001fps -d 0 -A -S '" + pin + u"' --sync 0:0 --forced-track 0:no -a 0 -D -S '"+pinaac+"' --track-order 0:0,1:0")
46         execcomd(e0)
47         execcomd(e1)
48         execcomd(e2)
49         if os.path.exists(pincap):
50             execcomd(e3s)
51         else:
52             execcomd(e3)
53         if (not os.path.exists(pin.replace(".avi",".120.avi"))and (not os.path.exists(pout))):
54             txt=txt+"CFR2TC error. maybe avi file is over 1GB.(ODML error would have happend.)"
55             ptmp=pin.replace(".avi",".noodml.avi")
56             e1="mencoder -noodml -forceidx -oac copy -ovc copy -o \'"+ptmp+"\'"+" \'"+pin+"\'"
57             e2=wineexe+u" "+cfr2tc+u" '"+"Z:\\"+ptmp+"' '"+"Z:\\"+pin120+u"' '"+"Z:\\"+pintimecode+u"' 2"
58             e3=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pints+"\" Z:\\"+pincap+"\""
59             e4=exe +" -o '"+pout+u"'  --forced-track 0:no --timecodes '0:"+pintimecode+"' -d 0 -A -S '"+pin120+"' --sync 0:-225 --forced-track 0:no -a 0 -D -S '"+pinaac+"' --track-order 0:0,1:0"
60             e4s=exe +" -o '"+pout+u"'  --forced-track 0:no --timecodes '0:"+pintimecode+"' -d 0 -A -S '"+pin120+"' --sync 0:-225 --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"
61             recdblist.printutf8(e)
62             txt=txt+e+"\n"
63             cmdt=execcomd(e1)
64             cmdt=cmdt+execcomd(e2)
65             cmdt=cmdt+execcomd(e3)
66             if os.path.exists(pincap):
67                 execcomd(e4s)
68             else:
69                 execcomd(e4)
70             os.remove(ptmp)
71             txt=txt+cmdt+"\n"
72     else:
73         wineexe=configreader.getpath("wine")
74         pincap=pin.replace(".avi",".srt")
75         cap2ass=configreader.getpath("Caption2Ass")
76         pints=pin.replace(".avi",".ts")
77         e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pints+"\" Z:\\"+pincap+"\""
78         e1=exe + u" -o '" + pout + u"' --forced-track 0:no --forced-track 1:no -a 1 -d 0 -S '" + pin + u"' --track-order 0:0,0:1"
79         e1s=exe + u" -o '" + pout + u"' --forced-track 0:no --forced-track 1:no -a 1 -d 0 -S '" + pin + u"' --forced-track 0:no -s 0 -A -D '"+pincap+"' --track-order 0:0,0:1,1:0"
80         txt=txt+execcomd(e0)
81         if os.path.exists(pincap):
82             txt=txt+execcomd(e1s)
83         else:
84             txt=txt+execcomd(e1)
85     logo=pout.replace(".mkv",".log")
86     """if (isvfr==1 and (not os.path.exists(pin.replace(".avi",".120.avi"))) and (not os.path.exists(pout))):
87         exes=[]
88         exes.append(exe + u" -o '" + pout + u"' --cues 0:all --forced-track 0:no --default-duration 0:120000/1001fps -d 0 -A -S '" + pin + u"' --sync 0:0 --forced-track 0:no -a 0 -D -S '"+pinaac+"' --track-order 0:0,1:0")
89         txt=txt+"120fps cfr exection\n"
90         for e2 in exes:
91             recdblist.printutf8(e2)
92             try:
93                 txt=txt+unicode(commands.getoutput(e2.encode('utf-8')),'utf-8')
94             except:
95                 ""
96     """
97     txt = "\n####mkvmerge-log####\n"+txt
98     f=open(logo,'a')
99     f.write(txt.encode('utf-8'))
100     f.close()
101 def execcomd(cmd):
102     txt=""
103     cmdt=u"nice -n 17 "+cmd
104     try:
105         txt=u"Cmd : "+cmd+"\n"
106         txt2=commands.getoutput(cmdt.encode('utf-8'))
107         txt=txt+unicode(txt2,"utf-8")+"\n"
108     except:
109         ""
110     return txt