OSDN Git Service

5826ff2b60a7579e217be443b6df2dc0f1438b7e
[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     my_env=recdblist.getEnv()
36     tunecmd=configreader.getConfDVB("DVBtune")+u" "+adapter+u" "+channel2freq(channel)
37     #p1=subprocess.Popen(tunecmd,shell=True)
38     p1=subprocess.Popen([configreader.getConfDVB("DVBtune"),adapter,channel2freq(channel)],env=my_env,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
39     time.sleep(0.2)
40     try:
41         os.kill(p1.pid,signal.SIGKILL)
42     except:
43         ""
44     if p1.communicate()[1].find("busy")>-1:
45         try:
46             p1.wait()
47         except:
48             ""
49         return 0
50     else:
51         try:
52             p1.wait()
53         except:
54             ""
55         return 1
56 def getAdapter(channel):
57     chl=[]
58     if channel[0:2].upper()=="BS" or channel[0:2].upper()=="CS":
59         chl=configreader.getConfDVB("DVBBSCS").split(",")
60     elif int(channel)<65:
61         chl=configreader.getConfDVB("DVBTE").split(",")
62     chlr=[]
63     for i in chl:
64         chlr.append(int(i))
65     return chlr
66 def getActiveAdapter(channel):
67     for i in getAdapter(channel):
68         rt=testTune(str(i),channel)
69         if rt==1:
70             return str(i)
71 def record(channel,tsid,out,timelength,decode=1):
72     adapt=getActiveAdapter(channel)
73     my_env=recdblist.getEnv()
74     if adapt!=None:
75         tunecmd=configreader.getConfDVB("DVBtune")+u" "+adapt+u" "+channel2freq(channel)+u" "+tsid
76         reccmd=u"/bin/cat \""+configreader.getConfDVB("DVBadapter")+adapt+"/dvr0\" > "+out
77         if decode==1:
78             reccmd=configreader.getConfPath("b25")+u" -v 0 \""+configreader.getConfDVB("DVBadapter")+adapt+"/dvr0\" \""+out+"\""
79         recdblist.addLog(out, tunecmd+"\n"+reccmd, "record(DVB)",100)
80         p1=subprocess.Popen([configreader.getConfDVB("DVBtune"),adapt,channel2freq(channel),tsid],env=my_env)
81         time.sleep(0.2)
82         if decode==1:
83             p2=subprocess.Popen([configreader.getConfPath("b25"),u"-v 0",u"\""+configreader.getConfDVB("DVBadapter")+adapt+"/dvr0\"","\""+out+"\""],preexec_fn=os.setsid,env=my_env,bufsize=-1)
84         else:
85             p2=subprocess.Popen(reccmd,shell=True,preexec_fn=os.setsid,env=my_env,bufsize=-1)
86         time.sleep(float(timelength))
87         os.killpg(p2.pid,signal.SIGKILL)
88         p2.wait()
89         os.kill(p1.pid,signal.SIGKILL)
90         p1.wait()
91     else:
92         recdblist.printutf8ex(u"tuner busy",100,100)
93         #f.close()
94 def useDVB():
95     try:
96         if int(configreader.getConfDVB("useDVB").replace(" ",""))==1:
97             return 1
98         else:
99             return 0
100     except:
101         return 0