OSDN Git Service

fix many bug.
[rec10/rec10-git.git] / rec10 / trunk / src / tv2mp4.py
1 #!/usr/bin/python
2 # coding: UTF-8
3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009 Yukikaze
5 import sys
6 import commands
7 import tv2avi
8 import os
9 import re
10 import os.path
11 import time
12 import recdblist
13 import configreader
14
15 def ts2mp4(pin, pout, opt):
16     dir=os.path.split(pout)[0]
17     title=os.path.split(pout)[1]
18     title=os.path.splitext(title)[0]
19     tpavi=os.path.join(dir, title+".avi")
20     tv2avi.ts2avi(pin, tpavi, opt)
21     time.sleep(10)
22     if not os.path.exists(tpavi):
23         if os.path.exists(pin):
24             tpavi=pin
25     avi2mp4(tpavi,pout,opt)
26 def avi2mp4(pin,pout,opt):
27     if re.search(u"v", opt):
28         isvfr=1
29     exe = configreader.getpath(u"MP4Box")
30     dir=os.path.split(pin)[0]
31     title=os.path.split(pin)[1]
32     title=os.path.splitext(title)[0]
33     audiopath=os.path.join(dir,title+u"_audio.raw")
34     videopath=os.path.join(dir,title+u"_video.h264")
35     exes=[]
36     exes.append(exe+u" -aviraw audio \'"+pin+u"\'")
37     exes.append(exe+u" -aviraw video \'"+pin+u"\'")
38     exes.append(exe+u" -new -fps 29.97 -add \'"+videopath+u"\'#video -add \'"+audiopath+u"\'#audio \'"+pout+u"\'")
39     if isvfr==1:
40         exes=[]
41         wineexe=configreader.getpath("wine")
42         cfr2tc=configreader.getpath("cfr2tc")
43         dtsedit=configreader.getpath("DtsEdit")
44         pin120=pin.replace(".avi",".120.avi")
45         pints=pin.replace(".avi",".ts")
46         pmp4=pin.replace(".avi",".tmp.mp4")
47         pintimecode=pin.replace(".avi",".timecode.txt")
48         pinaac=pin.replace(".avi",".aac")
49         evfr=[]
50         evfr.append("ffmpeg -i '"+pints+"' -vn -f aac -acodec copy '"+pinaac+"'")
51         evfr.append(wineexe+u" "+cfr2tc+u" '"+"Z:\\"+pin+"' '"+"Z:\\"+pin120+u"' 'Z:\\"+pintimecode+u"' 2")
52         evfr.append(exe+u" -aviraw video \'"+pin120+u"\'")
53         videotpath=os.path.join(dir,title+u".120_video.h264")
54         evfr.append(exe+u" -fps 29.97 -add \'"+videotpath+"\' -add \'"+pinaac+"\' -new \'"+pmp4+"\'")
55         #evfr.append(exe+u" -add \'"+pinaac+"\' \'"+pmp4+"\'")
56         evfr.append(wineexe+u" "+dtsedit+u" -tc \'Z:\\"+pintimecode+u"\' \'Z:\\"+pmp4+"\' -o \'Z:\\"+pout+"\'")
57         
58         exes=evfr
59     logo=pin.replace(u".avi",u".log")
60     os.environ['LANG']="ja_JP.UTF-8"
61     txt=""
62     print exes
63     for e in exes:
64         recdblist.printutf8(e)
65         txt=txt+e+"\n"
66         try:
67             txt=txt+unicode(commands.getoutput(e.encode('utf-8')),'utf-8')
68         except:
69             ""
70     txt = "\n####MP4Box-log####\n"+txt
71     f=open(logo,'a')
72     f.write(txt.encode('utf-8'))
73     f.close()
74     time.sleep(10)
75     if os.path.exists(pout):
76         if os.path.exists(audiopath):
77             os.remove(audiopath)
78         if os.path.exists(videopath):
79             os.remove(videopath)
80 def mkv2mp4(pin,pout):
81     exeb = configreader.getpath(u"mkvextract")
82     exe = configreader.getpath(u"MP4Box")
83     dir=os.path.split(pin)[0]
84     title=os.path.split(pin)[1]
85     title=os.path.splitext(title)[0]
86     audiopath=os.path.join(dir,title+u"_audio.mp3")
87     if not os.path.exists(audiopath):
88         if os.path.exists(os.path.join(dir,title+u"_audio.raw")):
89             audiopath=os.path.join(dir,title+u"_audio.raw")
90         if os.path.exists(os.path.join(dir,title+u"_audio.aac")):
91             audiopath=os.path.join(dir,title+u"_audio.aac")
92     videopath=os.path.join(dir,title+u"_video.h264")
93     #doexe = exe.encode('utf-8') + u' \"' + pin.encode('utf-8') + u'\" \"' + pout.encode('utf-8') + u'\"'
94     exe0=exeb+u" tracks \'"+pin+u"\' 1:\'"+videopath+u"\' 2:\'"+audiopath+u"\'"
95     exe1=exe+u" -aviraw audio \'"+pin+u"\'"
96     exe2=exe+u" -aviraw video \'"+pin+u"\'"
97     exefin=exe+u" -new -fps 29.970030 -add \'"+videopath+u"\'#video -add \'"+audiopath+u"\'#audio \'"+pout+u"\'"
98     logo=pin.replace(".avi",".log")
99     os.environ['LANG']="ja_JP.UTF-8"
100     txt= exe0+"\n"+exe1+"\n"+exe2+"\n"+exefin+"\n"
101     txt = txt+unicode(commands.getoutput(exe0.encode('utf-8')),'utf-8')
102     txt = txt+unicode(commands.getoutput(exe1.encode('utf-8')),'utf-8')
103     txt = txt+unicode(commands.getoutput(exe2.encode('utf-8')),'utf-8')
104     txt = txt+unicode(commands.getoutput(exefin.encode('utf-8')),'utf-8')
105     os.remove(audiopath)
106     os.remove(videopath)
107     txt = "\n####MKV2MP4-log####\n"+txt
108     f=open(logo,'a')
109     f.write(txt.encode('utf-8'))
110     f.close()
111     time.sleep(10)
112     if os.path.exists(pout):
113         if os.path.exists(audiopath):
114             os.remove(audiopath)
115         if os.path.exists(videopath):
116             os.remove(videopath)