OSDN Git Service

fix recording in DVB mode.
[rec10/rec10-git.git] / rec10 / trunk / src / recording_earth_pt1.py
1 #!/usr/bin/python
2 # coding: UTF-8
3 # Rec10 TS Recording Tools
4 # Copyright (C) 2009-2012 Yukikaze
5
6 import sys
7 import subprocess
8 import time
9 import os
10 import signal
11
12 import configreader
13 import recdblist
14 #
15 #CHの定義は
16 #1-64 地デジ
17 #BS1,BS3...BS23 BS放送
18 #CS2,CS4...CS24 CS放送
19
20 def channel2freq(channel):##freqを返す
21     freq=""
22     if channel[0:2].upper() != "CS" and channel[0:2].upper() != "BS":
23         ch=int(channel)
24         if ch<65:#TE 13ch 473_142857
25             freq=str(473+(ch-13)*6)+u"142857"
26     elif channel[0:2].upper() == "CS":#CS2,CS4,CS6...CS24
27         ch=int(channel.upper().replace("CS",""))
28         #freq = 12291000 + (CH_IDX - 2) * 40000 / 2 - 10678000;
29         freq=str((ch-2)*40000/2+1613000)
30     elif channel[0:2].upper() == "BS":#BS1,BS3,BS5...BS23
31         ch=int(channel.upper().replace("BS",""))
32         freq=str((ch-1)*38360/2+1049480)
33     return freq
34 def testTune(adapter,channel):
35     tunecmd=configreader.getConfDVB("DVBtune")+u" "+adapter+u" "+channel2freq(channel)
36     #p1=subprocess.Popen(tunecmd,shell=True)
37     p1=subprocess.Popen([configreader.getConfDVB("DVBtune"),adapter,channel2freq(channel)],stdout=subprocess.PIPE,stderr=subprocess.PIPE)
38     time.sleep(0.2)
39     try:
40         os.kill(p1.pid,signal.SIGKILL)
41     except:
42         ""
43     if p1.communicate()[1].find("busy")>-1:
44         try:
45             p1.wait()
46         except:
47             ""
48         return 0
49     else:
50         try:
51             p1.wait()
52         except:
53             ""
54         return 1
55 def getAdapter(channel):
56     chl=[]
57     if channel[0:2].upper()=="BS" or channel[0:2].upper()=="CS":
58         chl=configreader.getConfDVB("DVBBSCS").split(",")
59     elif int(channel)<65:
60         chl=configreader.getConfDVB("DVBTE").split(",")
61     chlr=[]
62     for i in chl:
63         chlr.append(int(i))
64     return chlr
65 def getActiveAdapter(channel):
66     for i in getAdapter(channel):
67         rt=testTune(str(i),channel)
68         if rt==1:
69             return str(i)
70 def record(channel,tsid,out,timelength,decode=1):
71     adapt=getActiveAdapter(channel)
72     if adapt!=None:
73         tunecmd=configreader.getConfDVB("DVBtune")+u" "+adapt+u" "+channel2freq(channel)+u" "+tsid
74         reccmd=u"/bin/cat \""+configreader.getConfDVB("DVBadapter")+adapt+"/dvr0\" > "+out
75         if decode==1:
76             reccmd=configreader.getConfPath("b25")+u" -v 0 \""+configreader.getConfDVB("DVBadapter")+adapt+"/dvr0\" \""+out+"\""
77         recdblist.addLog(out, tunecmd+"\n"+reccmd, "record(DVB)",100)
78         p1=subprocess.Popen([configreader.getConfDVB("DVBtune"),adapt,channel2freq(channel),tsid])
79         time.sleep(0.2)
80         if decode==1:
81             p2=subprocess.Popen([configreader.getConfPath("b25"),u"-v 0",u"\""+configreader.getConfDVB("DVBadapter")+adapt+"/dvr0\"","\""+out+"\""],preexec_fn=os.setsid,bufsize=-1)
82         else:
83             p2=subprocess.Popen(reccmd,shell=True,preexec_fn=os.setsid,bufsize=-1)
84         time.sleep(float(timelength))
85         os.killpg(p2.pid,signal.SIGKILL)
86         p2.wait()
87         os.kill(p1.pid,signal.SIGKILL)
88         p1.wait()
89     else:
90         recdblist.printutf8ex(u"tuner busy",100,100)
91         #f.close()
92 def useDVB():
93     try:
94         if int(configreader.getConfDVB("useDVB").replace(" ",""))==1:
95             return 1
96         else:
97             return 0
98     except:
99         return 0