OSDN Git Service

5bb15a5b7dbdff97653ab31502618ef95c3216fa
[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 os.path
11 import signal
12
13 import configreader
14 import recdblist
15 #
16 #CHの定義は
17 #1-64 地デジ
18 #BS1,BS3...BS23 BS放送
19 #CS2,CS4...CS24 CS放送
20
21 tmppath = configreader.getConfPath("tmp")+"/"
22 def channel2freq(channel):##freqを返す
23     freq=""
24     if channel[0:2].upper() != "CS" and channel[0:2].upper() != "BS":
25         ch=int(channel)
26         if ch<65:#TE 13ch 473_142857
27             freq=str(473+(ch-13)*6)+u"142857"
28     elif channel[0:2].upper() == "CS":#CS2,CS4,CS6...CS24
29         ch=int(channel.upper().replace("CS",""))
30         #freq = 12291000 + (CH_IDX - 2) * 40000 / 2 - 10678000;
31         freq=str((ch-2)*40000/2+1613000)
32     elif channel[0:2].upper() == "BS":#BS1,BS3,BS5...BS23
33         ch=int(channel.upper().replace("BS",""))
34         freq=str((ch-1)*38360/2+1049480)
35     return freq
36 def testTune(adapter,channel):
37     my_env=recdblist.getEnv()
38     tunecmd=configreader.getConfDVB("DVBtune")+u" "+adapter+u" "+channel2freq(channel)
39     #p1=subprocess.Popen(tunecmd,shell=True)
40     p1=subprocess.Popen([configreader.getConfDVB("DVBtune"),adapter,channel2freq(channel)],env=my_env,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
41     time.sleep(0.2)
42     try:
43         os.kill(p1.pid,signal.SIGKILL)
44     except:
45         ""
46     if p1.communicate()[1].find("busy")>-1:
47         try:
48             p1.wait()
49         except:
50             ""
51         return 0
52     else:
53         try:
54             p1.wait()
55         except:
56             ""
57         return 1
58 def getAdapter(channel):
59     chl=[]
60     if channel[0:2].upper()=="BS" or channel[0:2].upper()=="CS":
61         chl=configreader.getConfDVB("DVBBSCS").split(",")
62     elif int(channel)<65:
63         chl=configreader.getConfDVB("DVBTE").split(",")
64     chlr=[]
65     for i in chl:
66         chlr.append(int(i))
67     return chlr
68 def getActiveAdapter(channel):
69     for i in getAdapter(channel):
70         rt=testTune(str(i),channel)
71         if rt==1:
72             return str(i)
73 def record(channel,tsid,out,timelength,decode=1):
74     if not os.path.exists(tmppath+u"rec.sh"):
75         ft=open(tmppath+u"rec.sh","w")
76         ft.write(u"#!/bin/bash\n/bin/cat $1 > \"$2\"")
77         ft.close()
78         os.system(u"chmod +x "+tmppath+u"rec.sh")
79     adapt=getActiveAdapter(channel)
80     my_env=recdblist.getEnv()
81     if adapt!=None:
82         tunecmd=configreader.getConfDVB("DVBtune")+u" "+adapt+u" "+channel2freq(channel)+u" "+tsid
83         reccmd=u"/bin/cat \""+configreader.getConfDVB("DVBadapter")+adapt+u"/dvr0\" > \""+out+u"\""
84         if decode==1:
85             reccmd=configreader.getConfPath("b25")+u" -v 0 \""+configreader.getConfDVB("DVBadapter")+adapt+u"/dvr0\" \""+out+"\""
86         recdblist.addLog(out, tunecmd+"\n"+reccmd, "record(DVB)",100)
87         p1=subprocess.Popen([configreader.getConfDVB("DVBtune"),adapt,channel2freq(channel),tsid],env=my_env)
88         time.sleep(0.2)
89         if decode==1:
90             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)
91         else:
92             p2=subprocess.Popen([tmppath+u"rec.sh",configreader.getConfDVB("DVBadapter")+adapt+"/dvr0",out],preexec_fn=os.setsid,env=my_env,bufsize=-1)
93             #p2=subprocess.Popen(reccmd,shell=True,preexec_fn=os.setsid,env=my_env,bufsize=-1)
94         time.sleep(float(timelength))
95         os.killpg(p2.pid,signal.SIGKILL)
96         p2.wait()
97         os.kill(p1.pid,signal.SIGKILL)
98         p1.wait()
99     else:
100         recdblist.printutf8ex(u"tuner busy",100,100)
101 def useDVB():
102     try:
103         if int(configreader.getConfDVB("useDVB").replace(" ",""))==1:
104             return 1
105         else:
106             return 0
107     except:
108         return 0