-#!/usr/bin/python\r
-# coding: UTF-8\r
-# Rec10 TS Recording Tools\r
-# Copyright (C) 2009 Yukikaze\r
-import commands\r
-import shutil\r
-import tv2avi\r
-import os\r
-import re\r
-import os.path\r
-import base64\r
-import time\r
-import recdblist\r
-import configreader\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
- tpavi=os.path.join(dir, title+".avi")\r
- tv2avi.ts2avi(pin, tpavi, opt)\r
- time.sleep(10)\r
- if not os.path.exists(tpavi):\r
- if os.path.exists(pin):\r
- tpavi=pin\r
- avi2mp4(tpavi,pout,opt)\r
-def avi2mp4(pin,pout,opt):\r
- isvfr=0\r
- if re.search(u"v", opt):\r
- isvfr=1\r
- exe = configreader.getpath(u"MP4Box")\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.raw")\r
- videopath=os.path.join(dir,etitle+u"_video.264")\r
- exes=[]\r
- os.environ['LANG']="ja_JP.UTF-8"\r
- txt=""\r
- if isvfr==1:\r
- wineexe=configreader.getpath("wine")\r
- cfr2tc=configreader.getpath("cfr2tc")\r
- dtsedit=configreader.getpath("DtsEdit")\r
- caption2ass=configreader.getpath("Caption2Ass")\r
- pin120=os.path.join(dir,etitle+u".120.avi")\r
- pints=os.path.join(dir,title+u".ts")\r
- pmp4=os.path.join(dir,etitle+u".tmp.mp4")\r
- pintimecode=os.path.join(dir,etitle+u".timecode.txt")\r
- pinaac=os.path.join(dir,etitle+u".aac")\r
- pincat=os.path.join(dir,etitle+u".srt")\r
- evfr0="ffmpeg -i '"+pints+"' -vn -f aac -acodec copy '"+pinaac+"'"\r
- evfr1=wineexe+u" "+cfr2tc+u" '"+"Z:\\"+pin+"' '"+"Z:\\"+pin120+u"' 'Z:\\"+pintimecode+u"' 2"\r
- evfr2=exe+u" -aviraw video \'"+pin120+u"\'"\r
- videotpath=os.path.join(dir,etitle+u".120_video.h264")\r
- evfr3=exe+u" -fps 29.970030 -add \'"+videotpath+"\' -add \'"+pinaac+"\' -new \'"+pmp4+"\'"\r
- evfr4=wineexe+u" "+dtsedit+u" -tc \'Z:\\"+pintimecode+u"\' \'Z:\\"+pmp4+"\' -o \'Z:\\"+pout+"\'"\r
- try:\r
- recdblist.printutf8(evfr0)\r
- txt=txt+evfr0+"\n"\r
- txt=txt+unicode(commands.getoutput(evfr0.encode('utf-8')),'utf-8')\r
- except:\r
- ""\r
- try:\r
- recdblist.printutf8(evfr1)\r
- txt=txt+evfr1+"\n"\r
- txt=txt+unicode(commands.getoutput(evfr1.encode('utf-8')),'utf-8')\r
- except:\r
- ""\r
- try:\r
- if not os.path.exists(pin120):\r
- evfr2=exe+u" -aviraw video \'"+pin+u"\'"\r
- videotpath=os.path.join(dir,etitle+u"_video.h264")\r
- evfr3=exe+u" -fps 29.970030 -add \'"+videotpath+"\' -add \'"+pinaac+"\' -new \'"+pmp4+"\'"\r
- recdblist.printutf8(evfr2)\r
- txt=txt+evfr2+"\n"\r
- txt=txt+unicode(commands.getoutput(evfr2.encode('utf-8')),'utf-8')\r
- except:\r
- ""\r
- try:\r
- recdblist.printutf8(evfr3)\r
- txt=txt+evfr3+"\n"\r
- txt=txt+unicode(commands.getoutput(evfr3.encode('utf-8')),'utf-8')\r
- except:\r
- ""\r
- try:\r
- recdblist.printutf8(evfr4)\r
- txt=txt+evfr4+"\n"\r
- txt=txt+unicode(commands.getoutput(evfr4.encode('utf-8')),'utf-8')\r
- except:\r
- ""\r
- if (not os.path.exists(pin.replace(".avi",".120.avi"))and (not os.path.exists(pout))):\r
- txt=txt+"CFR2TC error. maybe avi file is over 1GB.(ODML error would have happend.)"\r
- ptmp=pin.replace(".avi",".noodml.avi")\r
- e="mencoder -noodml -forceidx -oac copy -ovc copy -o \'"+ptmp+"\'"+" \'"+pin+"\'"\r
- ex="nice -n 17 "+e\r
- e2=wineexe+u" "+cfr2tc+u" '"+"Z:\\"+ptmp+"' '"+"Z:\\"+pin120+u"' '"+"Z:\\"+pintimecode+u"' 2"\r
- ex2="nice -n 17 "+e2\r
- e3=exe+u" -aviraw video \'"+pin120+u"\'"\r
- ex3="nice -n 17 "+e3\r
- e4=exe+u" -fps 29.97 -add \'"+videotpath+"\' -add \'"+pinaac+"\' -new \'"+pmp4+"\'"\r
- ex4="nice -n 17 "+e4\r
- e5=wineexe+u" "+dtsedit+u" -tc \'Z:\\"+pintimecode+u"\' \'Z:\\"+pmp4+"\' -o \'Z:\\"+pout+"\'"\r
- ex5="nice -n 17 "+e5\r
- recdblist.printutf8(e)\r
- txt=txt+e+"\n"\r
- try:\r
- cmdt=unicode(commands.getoutput(ex.encode('utf-8')),'utf-8')\r
- except:\r
- ""\r
- try:\r
- cmdt=unicode(commands.getoutput(ex2.encode('utf-8')),'utf-8')\r
- except:\r
- ""\r
- try:\r
- if os.path.exists(pin120):\r
- cmdt=unicode(commands.getoutput(ex3.encode('utf-8')),'utf-8')\r
- except:\r
- ""\r
- try:\r
- cmdt=unicode(commands.getoutput(ex4.encode('utf-8')),'utf-8')\r
- except:\r
- ""\r
- try:\r
- cmdt=unicode(commands.getoutput(ex5.encode('utf-8')),'utf-8')\r
- except:\r
- ""\r
- try:\r
- os.remove(ptmp)\r
- except:\r
- ""\r
- txt=txt+cmdt+"\n"\r
- else:\r
- try:\r
- shutil.move(pin,os.path.join(dir,etitle+".avi"))\r
- except:\r
- ""\r
- if os.path.exists(os.path.join(dir,etitle+".avi")):\r
- e0=exe+u" -aviraw audio \'"+os.path.join(dir,etitle+".avi")+u"\'"\r
- e1=exe+u" -aviraw video \'"+os.path.join(dir,etitle+".avi")+u"\'"\r
- os.system(e0)\r
- os.system(e1)\r
- if not os.path.exists(audiopath):\r
- if os.path.exists(os.path.join(dir,etitle+u"_audio.mp3")):\r
- audiopath=os.path.join(dir,etitle+u"_audio.mp3")\r
- elif os.path.exists(os.path.join(dir,etitle+u"_audio.aac")):\r
- audiopath=os.path.join(dir,etitle+u"_audio.aac")\r
- else:\r
- shutil.move(audiopath,os.path.join(dir,etitle+u"_audio.aac"))\r
- audiopath=os.path.join(dir,etitle+u"_audio.aac")\r
- ptmp=os.path.exists(os.path.join(dir,etitle+u".mp4"))\r
- e2=exe+u" -fps 29.970030 -add \'"+videopath+u"\' -add \'"+audiopath+u"\' -new \'"+ptmp+u"\'"\r
- os.system(e2)\r
- shutil.move(ptmp,pout)\r
- logo=pin.replace(u".avi",u".log")\r
- txt = "\n####MP4Box-log####\n"+txt\r
- f=open(logo,'a')\r
- f.write(txt.encode('utf-8'))\r
- f.close()\r
- time.sleep(10)\r
- delpath=[]\r
- delpath.append(pin.replace(u".avi",u".120.avi"))\r
- delpath.append(pin.replace(u".avi",u".sa.avi"))\r
- delpath.append(pin.replace(u".avi",u".ts"))\r
- delpath.append(pin.replace(u".avi",u".aac"))\r
- delpath.append(pin.replace(u".avi",u".m2v"))\r
- delpath.append(pin.replace(u".avi",u".120_video.264"))\r
- delpath.append(pin.replace(u".avi",u".ts.b25"))\r
- if os.path.exists(pout):\r
- if os.path.exists(audiopath):\r
- os.remove(audiopath)\r
- if os.path.exists(videopath):\r
- os.remove(videopath)\r
- """\r
- for dp in delpath:\r
- try:\r
- os.remove(dp)\r
- except:\r
- ""\r
- """\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
- logo=pin.replace(".avi",".log")\r
- os.environ['LANG']="ja_JP.UTF-8"\r
- txt=""\r
- execcomd(exe0)\r
- execcomd(exe1)\r
- execcomd(exe2)\r
- execcomd(exe3)\r
- txt = "\n####MKV2MP4-log####\n"+txt\r
- f=open(logo,'a')\r
- f.write(txt.encode('utf-8'))\r
- f.close()\r
- time.sleep(10)\r
- if os.path.exists(pout):\r
- if os.path.exists(audiopath):\r
- os.remove(audiopath)\r
- if os.path.exists(videopath):\r
- os.remove(videopath)\r
- if os.path.exists(timecodepath):\r
- os.remove(timecodepath)\r
- if os.path.exists(tmpmp4):\r
- os.remove(tmpmp4)\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
+#!/usr/bin/python
+# coding: UTF-8
+# Rec10 TS Recording Tools
+# Copyright (C) 2009-2011 Yukikaze
+import commands
+import shutil
+import auto_process
+import os
+import re
+import os.path
+import string
+import base64
+import time
+import subprocess
+import traceback
+import zip
+
+import tv2avi
+import recdblist
+import configreader
+import status
+import tv2audio
+path = str(os.path.dirname(os.path.abspath(__file__))) + "/"
+tmppath = configreader.getConfPath("tmp")+"/"
+if tmppath=="/":
+ tmppath=path
+if not os.path.exists(tmppath):
+ os.mkdir(tmppath)
+def ts2mp4(pin, pout, opt):
+ dir=os.path.split(pout)[0]
+ title=os.path.split(pout)[1]
+ title=os.path.splitext(title)[0]
+ tpraw=os.path.join(dir, title+".264")
+ tpmp4=os.path.join(dir, title+".mp4")
+ if os.path.isfile(pin) and os.path.getsize(pin)>10*1000:
+ tv2avi.ts2raw(pin, tpraw, opt)
+ time.sleep(10)
+ if os.path.isfile(tpraw) and os.path.getsize(tpraw)>10*1000:
+ raw2mp4(tpraw, tpmp4, opt)
+ time.sleep(10)
+ if os.path.exists(tpraw) and not re.search("B",opt):
+ os.remove(tpraw)
+ zip.addFile2FileZip(recdblist.getLogTitle(pin)+".command.log", recdblist.getLogTitle(pin)+".log.zip")
+ if os.path.exists(recdblist.getLogTitle(pin)+".command.log"):
+ os.remove(recdblist.getLogTitle(pin)+".command.log")
+def raw2mp4(pin,pout,opt):
+ dir=os.path.split(pout)[0]
+ title=os.path.split(pout)[1]
+ title=os.path.splitext(title)[0]
+ duration="-fps 29.970030 "
+ if re.search("a",opt):
+ duration="-fps 23.976023 "
+ if re.search("I",opt):
+ duration="-fps 29.970030 "
+ exe = configreader.getConfPath("mp4box")+u" -tmp "+tmppath
+ txt=""
+ os.environ['LANG']="ja_JP.UTF-8"
+ pints=pin.replace(".264",".ts")
+ e1=exe +u" "+duration+" -add \""+pin+"\" -new \""+pout+"\""
+ execmp4box(pin, pout, e1)
+ addAudio(pints, pout, opt)
+ addCaption(pints, pout)
+ if status.getSettings_auto_del_tmp()==1:
+ if os.path.exists(pout):
+ if re.search("t",opt):
+ auto_process.deleteTmpFile(dir, title, ".264")
+ elif re.search("k",opt):
+ ""#削除しない
+ else:
+ auto_process.deleteTmpFile(dir, title, ".mp4")
+def mkv2mp4(pin,pout):
+ exeb = configreader.getConfPath(u"mkvextract")
+ exe = configreader.getConfPath(u"mp4Box")
+ #dtsedit=configreader.getConfPath("DtsEdit")
+ wineexe=configreader.getConfPath("wine")
+ dir=os.path.split(pin)[0]
+ title=os.path.split(pin)[1]
+ title=os.path.splitext(title)[0]
+ etitle=base64.b16encode(title.encode('utf-8'))
+ audiopath=os.path.join(dir,etitle+u"_audio.aac")
+ videopath=os.path.join(dir,etitle+u"_video.264")
+ timecodepath=os.path.join(dir,etitle+u"_1_timecode.txt")
+ tmpmp4=os.path.join(dir,etitle+u".tmp.mp4")
+ exe0=exeb+u" tracks \'"+pin+u"\' 1:\'"+videopath+u"\' 2:\'"+audiopath+u"\'"
+ exe1=exeb+u" timecodes_v2 \'"+pin+"\' 1:\'"+timecodepath+"\'"
+ exe2=exe+u" -fps 29.970030 -add \'"+videopath+u"\' -add \'"+audiopath+u"\' -new \'"+tmpmp4+u"\'"
+ exe3=wineexe+u" "+dtsedit+u" -tc \'Z:\\"+timecodepath+u"\' \'Z:\\"+tmpmp4+u"\' -o \'Z:\\"+pout+u"\'"
+ os.environ['LANG']="ja_JP.UTF-8"
+ txt=""
+ try:
+ txt=txt+execcomd(exe0)+"\n"
+ txt=txt+execcomd(exe1)+"\n"
+ txt=txt+execcomd(exe2)+"\n"
+ txt=txt+execcomd(exe3)+"\n"
+ except:
+ ""
+ recdblist.addLog(pin, txt, u"MKV2MP4-log")
+ txt = "\n####MKV2MP4-log####\n"+txt
+ time.sleep(10)
+ if status.getSettings_auto_del_tmp()==1:
+ if os.path.exists(pout):
+ auto_process.deleteTmpFile(dir, title, ".mp4")
+def addCaption(pts,pmp4):##字幕の追加を試みる。
+ wineexe=configreader.getConfPath("wine")
+ pincap=pts.replace(".ts",".srt")
+ try:
+ cap2ass=configreader.getConfPath("caption2ass")
+ except:
+ cap2ass=""
+ if os.path.isfile(cap2ass):
+ e0=wineexe+u" "+cap2ass+u" -format srt \"Z:\\"+pts+"\" \"Z:\\"+pincap+"\""
+ recdblist.printutf8(e0)
+ p0=subprocess.Popen(e0,shell=True,stdout=subprocess.PIPE)
+ time.sleep(100)
+ if p0.poll==None:#実行中
+ #if os.path.getsize(pincap)<1000:#2mで1kb以下の場合自動で終了
+ # os.kill(p0.pid,signal.SIGKILL)
+ #else:
+ os.waitpid(p0.pid, 0)
+ logt=unicode(p0.communicate()[0], "UTF-8")
+ recdblist.addLog(pts,e0, u"Captionログ-コマンド")
+ recdblist.addLog(pts,logt, u"Captionログ-詳細")
+ recdblist.addCommandLogZip(pts, "mp4box_caption", "mp4box_caption", e0, logt)
+ if os.path.exists(pincap):
+ if os.path.getsize(pincap)>1000:
+ exe = configreader.getConfPath("mp4box")+u" -tmp "+tmppath
+ e1s=exe +u" -add \""+pincap+"\" \""+pmp4+"\""
+ addmp4(pincap,pmp4,e1s)
+def addAudio(pts,pmp4,opts):#オプションに応じた音声の追加を行う
+ exe = configreader.getConfPath("mp4box")+u" -tmp "+tmppath
+ if re.search("d",opts) or re.search("5",opts):#二カ国語放送/5.1ch放送の場合
+ paac1=pts.replace(".ts","_1.aac")
+ paac2=pts.replace(".ts","_2.aac")
+ recdblist.printutf8(paac1)
+ e1a1=exe +u" -add \""+paac1+"\":mpeg4 \""+pmp4+"\""
+ e1a2=exe +u" -add \""+paac2+"\":mpeg4 \""+pmp4+"\""
+ if not os.path.exists(paac1):
+ paac1=pts.replace(".ts","_1.mp3")
+ e1a1=exe +u" -add \""+paac1+"\" \""+pmp4+"\""
+ if not os.path.exists(paac2):
+ paac2=pts.replace(".ts","_2.mp3")
+ e1a1=exe +u" -add \""+paac2+"\" \""+pmp4+"\""
+ if os.path.exists(paac1):
+ addmp4(paac1, pmp4, e1a1)
+ if os.path.exists(paac2):
+ addmp4(paac2, pmp4, e1a2)
+ elif re.search("b",opts):#BonTsDemuxを使って音声をスプリットした場合
+ paac=pts.replace(".ts",".aac")
+ e1a=exe +u" -add \""+pinaac+"\":mpeg4 \""+pmp4+"\""
+ if not os.path.exists(paac):
+ paac=pts.replace(".ts",".mp3")
+ e1a1=exe +u" -add \""+paac+"\" \""+pmp4+"\""
+ if os.path.exists(paac):
+ addmp4(paac, pmp4, e1a1)
+ else:
+ tv2audio.ts2single_audio(pts,opts)
+ pinaac=pts.replace(".ts",".aac")
+ e1a=exe +u" -add \""+pinaac+"\":mpeg4 \""+pmp4+"\""
+ if not os.path.exists(pinaac):
+ pinaac=pinaac.replace(".aac",".mp3")
+ e1a=exe +u" -add \""+pinaac+"\" \""+pmp4+"\""
+ if os.path.exists(pinaac):
+ addmp4(pinaac, pmp4, e1a)
+def execmp4box(pin,pout,cmd):
+ title=os.path.splitext(os.path.split(pin)[1])[0]
+ nt=base64.b16encode(title.encode('utf-8'))
+ if len(nt)>200:
+ nt=nt[:180]
+ ptin=os.path.join(os.path.dirname(pin),nt+".264")
+ recdblist.printutf8(ptin)
+ shutil.move(pin,ptin)
+ time.sleep(10)
+ ptout=os.path.join(os.path.dirname(pout),nt+".mp4")
+ cmdn=string.replace(cmd,pin,ptin)
+ cmdn=string.replace(cmdn,pout,ptout)
+ recdblist.printutf8(cmdn)
+ recdblist.addCommandSelfLog(pin, cmdn)
+ txt=""
+ try:
+ txt=execcomd(cmdn)
+ except Exception, inst:
+ txt= "error occures in execmp4box\n"
+ txt=txt+ str(type(inst))+"\n"
+ txt=txt+str(inst)
+ recdblist.addCommonlogEX("Error", "excecmp4box(tv2mp4.py)", str(type(inst)), str(inst)+traceback.format_exc(),verbose_level=200,log_level=200)
+ recdblist.addLog(pin, cmdn, u"MP4Boxログ-コマンド")
+ recdblist.addLog(pin, txt, u"MP4Boxログ-詳細")
+ recdblist.addCommandLogZip(pin, "MP4Box", "mp4box", cmdn,txt)
+ time.sleep(5)
+ shutil.move(ptin,pin)
+ shutil.move(ptout,pout)
+ time.sleep(5)
+def addmp4(padd,pout,cmd):#without video
+ title=os.path.splitext(os.path.split(padd)[1])[0]
+ ext=os.path.splitext(os.path.split(padd)[1])[1]
+ nt=base64.b16encode(title.encode('utf-8'))
+ if len(nt)>200:
+ nt=nt[:180]
+ ptadd=os.path.join(os.path.dirname(padd),nt+ext)
+ ptoutb=os.path.join(os.path.dirname(pout),nt+"_b.mp4")
+ ptout=os.path.join(os.path.dirname(pout),nt+".mp4")
+ shutil.move(padd,ptadd)
+ if os.path.isfile(pout):
+ shutil.move(pout,ptoutb)
+ time.sleep(5)
+ cmdn=string.replace(cmd,padd,ptadd)
+ cmdn=string.replace(cmdn,u"-out \""+pout,u"-add \""+ptoutb+"\" -new \""+ptout)
+ cmdn=string.replace(cmdn,u"\""+pout,u"-add \""+ptoutb+"\" -new \""+ptout)
+ cmdn=string.replace(cmdn,pout,ptout)
+ recdblist.printutf8(cmdn)
+ txt=""
+ try:
+ txt=execcomd(cmdn)
+ except Exception, inst:
+ txt= "error occures in addmp4\n"
+ txt=txt+ str(type(inst))+"\n"
+ txt=txt+str(inst)
+ recdblist.addLog(pout, cmdn, u"MP4Box追加ログ-コマンド")
+ recdblist.addLog(pout, txt, u"MP4Box追加ログ-詳細")
+ recdblist.addCommandLogZip(pout, "MP4Box_add", "mp4box_add", cmdn, txt)
+ time.sleep(5)
+ shutil.move(ptadd,padd)
+ if os.path.exists(ptout):
+ shutil.move(ptout,pout)
+ os.remove(ptoutb)
+ else:
+ txtt=padd+u"のインポートエラー"
+ recdblist.addLog(pout, txtt, u"MP4Box追加ログ-コマンド")
+ shutil.move(ptoutb,pout)
+ time.sleep(5)
+def execcomd(cmd):
+ txt=""
+ try:
+ txt=u"Cmd : "+cmd+"\n"
+ txt2=commands.getoutput(cmd.encode('utf-8'))
+ txt=txt+unicode(txt2,"utf-8")+"\n"
+ except:
+ ""
+ return txt