-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcompose.py
More file actions
65 lines (52 loc) · 1.77 KB
/
compose.py
File metadata and controls
65 lines (52 loc) · 1.77 KB
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
import os
import json
import subprocess
import shutil
best_matches_path = "./autodl-tmp/best_matches.json"
scenes_dir = "./autodl-tmp/scenes/"
output_video_path = "./autodl-tmp/output_video.mp4"
tmp_dir = "./autodl-tmp/tmp_segments/"
tmp_list_path = "./tmp_segments_list.txt"
# Create temporary directory for video segments
os.makedirs(tmp_dir, exist_ok=True)
# Read best match results
with open(best_matches_path, "r", encoding="utf-8") as f:
best_matches = json.load(f)
# Get all timestamps
timestamps = sorted(best_matches.keys(), key=lambda x: float(x))
# Process each timestamp and create temporary video segments
segment_files = []
for i, timestamp in enumerate(timestamps):
filename = best_matches[timestamp]
if filename == "None":
continue
scene_path = os.path.join(scenes_dir, filename)
segment_path = os.path.join(tmp_dir, f"segment_{i}.mp4")
# Calculate duration
if i < len(timestamps) - 1:
next_timestamp = float(timestamps[i + 1])
duration = next_timestamp - float(timestamp)
# Create a temporary segment with the specified duration
subprocess.run([
"ffmpeg", "-y", "-i", scene_path, "-t", str(duration), "-c", "copy", segment_path
])
else:
# If it's the last segment, just copy the file
shutil.copy(scene_path, segment_path)
segment_files.append(segment_path)
# Concatenate video segments using ffmpeg
with open(tmp_list_path, "w", encoding="utf-8") as f:
for segment_file in segment_files:
f.write(f"file '{segment_file}'\n")
command = [
"ffmpeg",
"-f", "concat",
"-safe", "0",
"-i", tmp_list_path,
"-c", "copy",
output_video_path
]
subprocess.run(command)
# Clean up temporary files
shutil.rmtree(tmp_dir)
os.remove(tmp_list_path)