OSDN Git Service

fix large number of bugs.
[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 os.path
10 import time
11 import configreader
12 def ts2mp4(pin, pout, opt):
13     dir=os.path.split(pout)[0]
14     title=os.path.split(pout)[1]
15     title=os.path.splitext(title)[0]
16     tpavi=os.path.join(dir, title+".avi")
17     tv2avi.ts2avi(pin, tpavi, opt)
18     time.sleep(10)
19     if not os.path.exists(tpavi):
20         if os.path.exists(pin):
21             tpavi=pin
22     isvfr=0
23     if re.search("v", opt):
24         isvfr=1
25     avi2mp4(tpavi,pout,isvfr)
26 def avi2mp4(pin,pout,isvfr):
27     exe = configreader.getpath(u"MP4Box")
28     dir=os.path.split(pin)[0]
29     title=os.path.split(pin)[1]
30     title=os.path.splitext(title)[0]
31     audiopath=os.path.join(dir,title+u"_audio.mp3")
32     videopath=os.path.join(dir,title+u"_video.h264")
33     if isvfr==1:
34         ""
35     #doexe = exe.encode('utf-8') + u' \"' + pin.encode('utf-8') + u'\" \"' + pout.encode('utf-8') + u'\"'
36     exe1=exe+u" -aviraw audio \'"+pin+u"\'"
37     exe2=exe+u" -aviraw video \'"+pin+u"\'"
38     exefin=exe+u" -new -fps 29.97 -add \'"+videopath+u"\'#video -add \'"+audiopath+u"\'#audio \'"+pout+u"\'"
39     logo=pin.replace(u".avi",u".log")
40     os.environ['LANG']="ja_JP.UTF-8"
41     txt= exe1+u"\n"+exe2+u"\n"+exefin+u"\n"
42     txt = txt+unicode(commands.getoutput(exe1.encode('utf-8')),'utf-8')
43     txt = txt+unicode(commands.getoutput(exe2.encode('utf-8')),'utf-8')
44     txt = txt+unicode(commands.getoutput(exefin.encode('utf-8')),'utf-8')
45     os.remove(audiopath)
46     os.remove(videopath)
47     txt = "\n####MP4Box-log####\n"+txt
48     f=open(logo,'a')
49     f.write(txt.encode('utf-8'))
50     f.close()
51     time.sleep(10)
52     if os.path.exists(pout):
53         if os.path.exists(audiopath):
54             os.remove(audiopath)
55         if os.path.exists(videopath):
56             os.remove(videopath)
57 def mkv2mp4(pin,pout):
58     exeb = configreader.getpath(u"mkvextract")
59     exe = configreader.getpath(u"MP4Box")
60     dir=os.path.split(pin)[0]
61     title=os.path.split(pin)[1]
62     title=os.path.splitext(title)[0]
63     audiopath=os.path.join(dir,title+u"_audio.mp3")
64     if not os.path.exists(audiopath):
65         if os.path.exists(os.path.join(dir,title+u"_audio.raw")):
66             audiopath=os.path.join(dir,title+u"_audio.raw")
67         if os.path.exists(os.path.join(dir,title+u"_audio.aac")):
68             audiopath=os.path.join(dir,title+u"_audio.aac")
69     videopath=os.path.join(dir,title+u"_video.h264")
70     #doexe = exe.encode('utf-8') + u' \"' + pin.encode('utf-8') + u'\" \"' + pout.encode('utf-8') + u'\"'
71     exe0=exeb+u" tracks \'"+pin+u"\' 1:\'"+videopath+u"\' 2:\'"+audiopath+u"\'"
72     exe1=exe+u" -aviraw audio \'"+pin+u"\'"
73     exe2=exe+u" -aviraw video \'"+pin+u"\'"
74     exefin=exe+u" -new -fps 29.970030 -add \'"+videopath+u"\'#video -add \'"+audiopath+u"\'#audio \'"+pout+u"\'"
75     logo=pin.replace(".avi",".log")
76     os.environ['LANG']="ja_JP.UTF-8"
77     txt= exe0+"\n"+exe1+"\n"+exe2+"\n"+exefin+"\n"
78     txt = txt+unicode(commands.getoutput(exe0.encode('utf-8')),'utf-8')
79     txt = txt+unicode(commands.getoutput(exe1.encode('utf-8')),'utf-8')
80     txt = txt+unicode(commands.getoutput(exe2.encode('utf-8')),'utf-8')
81     txt = txt+unicode(commands.getoutput(exefin.encode('utf-8')),'utf-8')
82     os.remove(audiopath)
83     os.remove(videopath)
84     txt = "\n####MKV2MP4-log####\n"+txt
85     f=open(logo,'a')
86     f.write(txt.encode('utf-8'))
87     f.close()
88     time.sleep(10)
89     if os.path.exists(pout):
90         if os.path.exists(audiopath):
91             os.remove(audiopath)
92         if os.path.exists(videopath):
93             os.remove(videopath)