-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadCSVRecord.py
executable file
·139 lines (110 loc) · 3.9 KB
/
readCSVRecord.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/env python
"""Read CSV and Schedule Recording
Usage:
readCSVRecord.py <pathCSV> <outPath>
Options:
-h --help Show this screen.
--version Show version.
"""
from docopt import docopt
from datetime import datetime
from dateutil import parser
import csv, time, re, os, sched
from fileutils import execute, ExecuteError
SharkAudioAddr="1"
def ensure_dir(f):
d = os.path.dirname(f)
if not os.path.exists(d):
os.makedirs(d)
def gameToStr(game):
return "%s-%s" % (game["team"],game["start"][:10])
def getGameDurationSec(game):
start = parser.parse(game["start"])
end = parser.parse(game["end"])
return int((end - start).total_seconds())
def getRecordCommand(game):
RecordingDurationSec = getGameDurationSec(game)
print RecordingDurationSec
recorder = ' '.join([
'arecord' # audio recorder
, '-q' # quiet
, '-d {duration}' # recording time
, '--max-file-time {duration}' # recording time before switching files (must be >= recording time)
, '-c 2' # input stream is 2 channels
, '-f S16' # input stream is 16 bit signed
, '-r 44100' # rate of input stream is 44.1kHz
, '-d {device}' # audio generator
, '-t raw' # output format is raw (don't use .wav, it cuts out after 3 hours and 22 minutes because of a size limit on .wav files)
]).format(
duration = RecordingDurationSec, device = SharkAudioAddr
)
return recorder
def getMp3Command(filename, game):
encoder = ' '.join([
'lame'
, '--quiet' # quiet
, '-r' # raw format input
#, '--resample 8' # resample to rate
#, '-V3' # ???
#, '--vbr-new' # ???
#, '-q0' # quality level
#, '-B16' # maximum bit rate
#, '--lowpass 15.4' # apply lowpass filter
#, '--athaa-sensitivity 1' # ???
, '--preset 32' # average bitrate of 32kbps
, '--tt "{title}"' # title
, '--ta "{artist}"' # artist
, '-' # read from standard input
, '{filename}' # write to filename
]).format(
filename = filename
, title = game["name"]
, artist = game["team"]
, date = game["start"]
)
return encoder
def runRecordGame(pipeline, game):
try:
freq = float(game['freq'])
if game['band'] == 'am':
freq = freq / 1000
# Configure the shark (set station, turn fin red to indicate recording)
execute('fm -q %.2f' % (freq))
# Record the game
print 'Recording ...', game
execute(pipeline)
print 'Recording complete.'
except ExecuteError, err:
print err
def postRecord(filename, game):
print "Doing post record"
def recordGame(outpath, game):
filename = os.path.join(outpath, gameToStr(game)+".mp3")
recorder = getRecordCommand(game)
encoder = getMp3Command(filename, game)
pipeline = '{recorder} | {encoder}'.format(
recorder=recorder, encoder=encoder
)
print pipeline
postRecord(filename, game)
if __name__ == "__main__":
arguments = docopt(__doc__, version='Read CSV and Record 0.1')
priority = 0
scheduler = sched.scheduler(time.time, time.sleep)
outPath = arguments["<outPath>"]
ensure_dir(outPath)
now = datetime.utcnow()
with open(arguments["<pathCSV>"],"r") as csvfile:
reader = csv.DictReader(csvfile)
for game in reader:
start = parser.parse(game["start"])
if start > datetime.now(start.tzinfo):
print priority, game, gameToStr(game)
scheduler.enterabs(time.mktime(start.timetuple()), priority, recordGame, (outPath, game))
priority = priority + 1
print "Next game:"
for scheduledGame in scheduler.queue[0]:
print scheduledGame
next = scheduler.queue[0]
next.action(*next.argument)
scheduler.run()