-
Notifications
You must be signed in to change notification settings - Fork 0
/
PostShow.py
113 lines (97 loc) · 4.17 KB
/
PostShow.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
#!/usr/bin/python3
"""PostShow v1.2
Python script to generate JSON MP3 chapters, LRC, CUE and simple timestamp files from an Audacity label file.
Written by Manual (@manualmanul on Twitter)
"""
import csv
import os
import sys
import time
import argparse
def main(args):
if not os.path.isfile(args.input):
sys.exit("File does not exist. Aborting.")
reader = csv.reader(open(args.input, encoding="utf-8"), delimiter=' ',
quoting=csv.QUOTE_NONE)
json_output = open(os.path.join(args.output, args.title) + '.json', 'w',
encoding="utf-8")
cue_output = open(os.path.join(args.output, args.title) + '.cue', 'w',
encoding="utf-8")
lrc_output = open(os.path.join(args.output, args.title) + '.lrc', 'w',
encoding="utf-8")
simple_output = open(os.path.join(args.output, args.title) + '-simple.txt',
'w', encoding="utf-8")
trackno = 1
cue_initialized = False # write CUE header before loop starts
lrc_initialized = False # write LRC header before loop starts
cue_output.write('\ufeff') # Write UTF-8 BOM for foobar2000
bit = []
json_bit = []
for row in reader:
if not row:
break # Reached EOF
# Math
math_milliseconds_total = format(float(row[0]) * 1000, '.1f')
math_minutes_total = int(
time.strftime('%H', time.gmtime(int(float(row[0]))))
) * 60 + int(time.strftime('%M', time.gmtime(int(float(row[0])))))
math_seconds = int(time.strftime('%S', time.gmtime(int(float(row[0])))))
math_centiseconds = int((float(row[0]) % 1) * 100)
math_cueframes = int((float(row[0]) % 1) * 75)
# JSON
bit.append(row[2].replace('"', '\u201d'))
bit.append(float(math_milliseconds_total))
json_bit.append(bit)
bit = []
# LRC
if lrc_initialized is not True:
lrc_output.write('[ti:{}]\n'.format(args.title))
lrc_output.write('[ar:{}]\n'.format("..::XANA Creations::.."))
lrc_output.write('[al:{}]\n'.format(
"The Friday Night Tech Podcast"))
lrc_initialized = True
lrc_output.write('[{:02d}:{:02d}.{:02d}]{}\n'.format(
math_minutes_total,
math_seconds,
math_centiseconds,
row[2]
))
# CUE
if cue_initialized is not True:
cue_output.write('REM GENRE Podcast\n')
cue_output.write('REM COMMENT "This cue file has been generated by '
'PostShow v2 ('
'https://github.com/vladasbarisas/XBN)"\n')
cue_output.write('TITLE "{}"\n'.format(args.title))
cue_output.write('PERFORMER "XBN"\n')
cue_output.write('FILE "{}" MP3\n'.format(args.filename))
cue_initialized = True
cue_output.write(' TRACK {:02d} AUDIO\n'.format(trackno))
cue_output.write(' TITLE "{}"\n'.format(
row[2].replace('"', '_')
))
cue_output.write(' INDEX 01 {:02d}:{:02d}:{:02d}\n'.format(
math_minutes_total,
math_seconds,
math_cueframes
))
trackno += 1
# Simple
simple_output.write('{} - {}'.format(
time.strftime('%H:%M:%S', time.gmtime(int(float(row[0])))),
row[2]
))
# Fix up JSON after the loop is done
json_output.write(str(json_bit).replace("['", '["').replace("', ", '", '))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Generate CUE, LRC, JSON and '
'timestamp files from an '
'Audacity label file')
parser.add_argument("input", help="path to audacity file")
parser.add_argument("output", help="output directory "
"(with a leading slash!)")
parser.add_argument("title", help="episode title")
parser.add_argument("filename", help="episode file name (for example "
"fnt-200.mp3)")
arguments = parser.parse_args()
main(arguments)