Last active
August 29, 2015 13:57
-
-
Save zheplusplus/9702481 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import os | |
import tempfile | |
import uuid | |
import subprocess | |
import collections | |
def parse_seq(sequence): | |
Segment = collections.namedtuple('Segment', 'epnum,start,duration,subt') | |
result = [] | |
total_dur = 0 | |
for line in sequence: | |
line = line.strip() | |
if len(line) == 0 or line[0] == '#': | |
continue | |
parts = filter(None, line.split(' ')) | |
if len(parts) == 3: | |
epnum, start, dur = parts | |
subt = None | |
else: | |
epnum, start, dur, subt = parts | |
start_parts = start.split(':') | |
if len(start_parts) == 2: | |
start_time = int(start_parts[0]) * 60 + float(start_parts[1]) | |
else: | |
start_time = float(start) | |
dur = float(dur) | |
total_dur += dur | |
result.append(Segment(int(epnum), start_time, dur, subt)) | |
return result, total_dur | |
def main(source_video_dir, audio_file, output_file, sequence_file): | |
with open(sequence_file, 'r') as f: | |
segments, total_dur = parse_seq(f.readlines()) | |
source_files = [os.path.join(source_video_dir, f) | |
for f in sorted(os.listdir(source_video_dir))] | |
tmp_name_id = str(uuid.uuid4()) | |
tmp_files = [] | |
for i, seg in enumerate(segments): | |
tmp_file = os.path.join(tempfile.gettempdir(), tmp_name_id + str(i)) | |
p = subprocess.Popen([ | |
'avconv', | |
'-ss', str(seg.start), | |
'-i', source_files[seg.epnum - 1], | |
'-t', str(seg.duration), | |
'-vf', 'scale=640:360', | |
'-f', 'mp4', | |
'-vcodec', 'libx264', | |
'-an', | |
tmp_file]) | |
p.wait() | |
if p.returncode != 0: | |
raise ValueError('process fail at ' + str(i)) | |
if seg.subt is not None: | |
p = subprocess.Popen([ | |
'avconv', | |
'-i', tmp_file, | |
'-vf', '''drawtext=fontfile='/usr/share/fonts/truetype/''' + | |
"""ttf-dejavu/DejaVuSans.ttf':text='""" + seg.subt + | |
"""':x=0:y=0:fontsize=36:fontcolor=black""", | |
'-vcodec', 'libx264', | |
'-f', 'mp4', | |
tmp_file + '.sub']) | |
p.wait() | |
if p.returncode != 0: | |
raise ValueError('process fail at ' + str(i)) | |
tmp_file += '.sub' | |
tmp_files.append(tmp_file) | |
tmp_file = os.path.join(tempfile.gettempdir(), tmp_name_id + '.mp4') | |
p = subprocess.Popen([ | |
'mencoder', '-ovc', 'copy', '-o', tmp_file] + | |
tmp_files) | |
p.wait() | |
if p.returncode != 0: | |
raise ValueError('fail') | |
p = subprocess.Popen([ | |
'mencoder', | |
'-audiofile', audio_file, | |
'-of', 'lavf', '-lavfopts', 'format=mp4', | |
'-oac', 'copy', '-ovc', 'x264', | |
'-o', output_file, | |
tmp_file]) | |
p.wait() | |
if p.returncode != 0: | |
raise ValueError('fail') | |
print 'Total length:', total_dur | |
if __name__ == '__main__': | |
if len(sys.argv) != 5: | |
print >> sys.stderr, 'Args:' | |
print >> sys.stderr, ' INPUT_DIRECTORY INPUT_BGM OUTPUT_PATH', | |
print >> sys.stderr, 'SEQUENCE_FILE' | |
sys.exit(1) | |
if os.path.exists(sys.argv[3]): | |
print >> sys.stderr, sys.argv[3], 'already exists.' | |
print >> sys.stderr, 'Please delete it to proceed.' | |
sys.exit(1) | |
main(*sys.argv[1:]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 18:55.50 0.8 | |
20 18:20.00 2.7 | |
20 18:56.00 0.4 | |
20 20:00.00 1.8 | |
20 19:05.80 1.2 | |
20 19:15.00 1.4 | |
20 19:20.80 1.7 | |
20 19:26.20 1.5 | |
20 20:00.00 2 | |
20 19:46.20 1.4 | |
20 20:35.70 1 | |
20 20:47.00 2 | |
20 21:05.50 1.6 | |
20 20:15.10 1.5 | |
20 21:07.20 0.9 | |
20 20:26.50 3.2 | |
# 気がついたら同じ面ばかりプレイ | |
7 02:29.80 1.5 | |
7 03:14.50 1.75 | |
7 03:16.00 0.75 | |
# そしていつも同じ場所で死ぬ | |
7 04:08.80 2.1 | |
7 04:09.20 1.7 | |
7 04:12.00 1.5 | |
# 諦めずに消える足場に挑戦するけど、すぐに下に落ちるよ | |
18 19:38.00 1.1 | |
18 19:44.00 1.4 | |
18 20:15.00 6.8 | |
# アイテム2号があれば楽に向こうの岸まで着くけど | |
21 16:00.00 1.0 | |
21 16:08.00 1.0 | |
21 18:17.00 2.4 | |
# 何回やっても、何回やっても、エアーマンが倒せないよ | |
21 16:15.00 1.0 | |
21 14:55.00 1.1 | |
21 19:04.00 1.65 | |
21 19:44.50 2.4 | |
# あの竜巻何回やっても避けれない | |
21 16:17.00 1.2 | |
21 16:20.80 2.0 | |
# うしろに回って撃ち続けてもいずれは風に飛ばされる | |
21 20:15.50 2.2 | |
21 19:47.00 1 | |
21 19:50.00 1.1 | |
# 01:05.62 タイム连打(れんだ)を试(ため)してみたけど、 | |
21 19:52.80 0.6 | |
21 19:52.80 0.5 | |
21 19:52.80 0.6 | |
21 19:52.80 0.5 | |
21 19:52.80 0.6 | |
# タイム連打も試してみたけど竜巻相手じゃ意味が無い | |
21 16:20.80 2.0 | |
# だから次は絶対勝つために | |
18 16:51.00 2.3 | |
# 僕はE缶だけは最後までとっておく | |
18 16:55.00 1.5 | |
18 17:01.00 3.2 | |
# --- | |
18 16:03.00 1.8 | |
18 19:53.00 3.2 | |
18 17:20.00 2.1 | |
18 17:04.00 3.2 | |
18 21:15.00 4.2 | |
18 21:35.80 5.2 | |
# 気がついたらライフももう少ししかない | |
21 20:22.20 1 | |
21 20:28.00 0.8 | |
21 20:29.00 1.5 | |
21 00:45.50 0.2 | |
21 00:45.50 0.2 | |
21 00:46.70 0.2 | |
21 00:46.70 0.2 | |
21 00:45.00 0.2 | |
21 00:45.00 0.2 | |
21 00:45.00 0.2 | |
21 00:45.00 0.2 | |
# そしていつもそこでE缶使う | |
06 18:38.20 1.5 | |
06 18:50.00 3 | |
06 18:52.40 0.8 | |
06 18:52.40 0.8 | |
# 諦めずにエアーマンまでたどり着くけれど、すぐに残機なくなる | |
25 08:17.60 .8 | |
25 08:20.00 3.8 | |
10 06:35.10 0.8 | |
10 06:45.30 2.7 | |
10 06:47.50 0.2 | |
# リーフシールドがあれば楽にエアーマンを倒せるけど | |
26 11:58.40 0.6 | |
26 11:58.40 0.6 | |
26 11:58.40 0.6 | |
26 12:28.00 1.7 | |
23 03:26.60 0.9 | |
23 02:46.60 0.7 | |
# 何回やっても 何回やっても ウッドマンが倒せないよ | |
26 11:44.80 1.1 | |
26 11:33.00 2.2 | |
26 12:30.00 0.8 | |
26 12:31.00 1.3 | |
26 12:31.80 0.4 | |
# 落ちる木の葉は何回やっても避けれない | |
1 4:37.00 1 | |
1 5:02.20 1 | |
1 5:24.80 0.8 | |
# 後に下がって距離をとってもいずれは距離を詰められる | |
2 15:30.00 1 | |
2 15:38.10 0.4 | |
2 16:34.00 1.0 | |
2 16:51.00 0.7 | |
2 17:11.00 1.5 | |
# タイム連打も試してみたけどあいつのジャンプはくぐれない | |
9 12:18.00 2.8 | |
19 09:16.90 2 | |
# だから次は絶対勝つために | |
8 05:42.20 2.8 | |
# だから次は絶対勝つために僕はE缶だけは最後までとっておく | |
6 10:33.00 1.2 | |
6 10:38.00 1.2 | |
6 11:00.00 1.2 | |
# ---- | |
10 19:26.80 2 | |
10 20:51.20 2 | |
15 18:48.00 4 | |
10 20:51.20 2 | |
10 20:56.00 2 | |
10 20:57.00 0.6 | |
11 06:49.20 1 | |
11 06:51.00 2 | |
17 21:47.50 2 | |
# アイテム2号があれば楽に向こうの岸まで着くけど | |
23 04:00.20 1.8 | |
25 08:57.80 2.8 | |
# 何回やっても 何回やっても エアーマンが倒せないよ | |
14 17:46.80 1.2 | |
13 09:52.50 1.1 | |
6 19:39.50 3.7 | |
# あの竜巻何回やっても避けれない | |
2 06:04.90 1.5 | |
2 06:18.95 0.8 | |
# うしろに回って撃ち続けてもいずれは風に飛ばされる | |
19 09:30.00 3 | |
6 20:31.20 2.4 | |
# タイム連打も試してみたけど | |
9 11:57.00 1.5 | |
9 11:54.20 1.5 | |
# 竜巻相手じゃ意味が無い | |
16 10:50.50 1.0 | |
16 10:51.00 1.0 | |
# だから次は絶対勝つために | |
1 09:11.60 1.65 | |
1 09:21.60 0.6 | |
# 僕はE缶だけは最後までとっておく | |
1 09:24.80 5.5 | |
1 09:29.80 0.5 | |
1 09:29.80 0.5 | |
# --- | |
2 16:27.60 2 | |
2 16:29.00 1 | |
22 18:10.00 0.5 | |
22 18:14.00 3 | |
22 18:55.00 4 | |
6 19:45.60 1.3 | |
6 19:49.80 3.3 | |
6 19:47.60 1.2 | |
10 04:34.00 2.2 | |
10 03:02.00 1 | |
10 05:28.80 2.2 | |
3 21:12.00 1.95 | |
3 21:13.60 0.35 | |
3 21:13.60 0.35 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment