-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_video.py
139 lines (112 loc) · 4.26 KB
/
process_video.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
139
import re
import subprocess
import os
import sys
def parse_srt(srt_path):
"""
Parses the SRT file and extracts start and end times.
Returns a list of tuples: [(start1, end1), (start2, end2), ...]
"""
with open(srt_path, 'r', encoding='utf-8') as file:
content = file.read()
# Regular expression to match time ranges
pattern = re.compile(r'(\d{2}:\d{2}:\d{2}),(\d{3})\s-->\s(\d{2}:\d{2}:\d{2}),(\d{3})')
matches = pattern.findall(content)
# Remove the first segment if it's a placeholder (e.g., all zeros)
if matches and matches[0][0] == '00' and matches[0][1] == '000':
matches = matches[1:]
time_ranges = []
for match in matches:
start_time = f"{match[0]}.{match[1]}"
end_time = f"{match[2]}.{match[3]}"
time_ranges.append((start_time, end_time))
return time_ranges
def extract_segments(input_video, time_ranges, segments_dir):
"""
Extracts video segments based on the provided time ranges.
Saves segments in the specified directory.
Returns a list of segment file paths.
"""
os.makedirs(segments_dir, exist_ok=True)
segments = []
for idx, (start, end) in enumerate(time_ranges, start=1):
segment_filename = f"segment_{idx:03d}.mov"
segment_path = os.path.join(segments_dir, segment_filename)
print(f"Extracting Segment {idx}: {start} to {end}")
# FFmpeg command to extract the segment
cmd = [
'ffmpeg',
'-y', # Overwrite output files without asking
'-i', input_video,
'-ss', start,
'-to', end,
'-c', 'copy',
segment_path
]
# Execute the command
result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if result.returncode != 0:
print(f"Error extracting segment {idx}: {result.stderr.decode('utf-8')}")
sys.exit(1)
segments.append(segment_path)
return segments
def create_concat_list(segments, concat_list_path):
"""
Creates a concat list file required by FFmpeg for concatenation.
"""
with open(concat_list_path, 'w', encoding='utf-8') as f:
for segment in segments:
# FFmpeg requires paths to be escaped if they contain special characters
escaped_path = segment.replace("'", "'\\''")
f.write(f"file '{escaped_path}'\n")
print(f"Created concatenation list at {concat_list_path}")
def concatenate_segments(concat_list_path, final_video):
"""
Concatenates video segments into a final video using FFmpeg.
"""
print("Concatenating segments into final video...")
cmd = [
'ffmpeg',
'-y',
'-f', 'concat',
'-safe', '0',
'-i', concat_list_path,
'-c', 'copy',
final_video
]
result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if result.returncode != 0:
print(f"Error concatenating segments: {result.stderr.decode('utf-8')}")
sys.exit(1)
print(f"Final video created: {final_video}")
def main():
# Configuration
input_video = 'IMG_0106.mov'
srt_file = '90 days in Japan without a plan.srt'
segments_dir = 'segments'
concat_list = 'concat_list.txt'
final_video = 'final_video.mov'
# Check if input files exist
if not os.path.isfile(input_video):
print(f"Input video file '{input_video}' not found.")
sys.exit(1)
if not os.path.isfile(srt_file):
print(f"SRT file '{srt_file}' not found.")
sys.exit(1)
# Step 1: Parse the SRT file
print("Parsing SRT file...")
time_ranges = parse_srt(srt_file)
print(f"Found {len(time_ranges)} segments to extract.")
if not time_ranges:
print("No valid time ranges found in the SRT file.")
sys.exit(1)
# Step 2: Extract video segments
segments = extract_segments(input_video, time_ranges, segments_dir)
print(f"Extracted {len(segments)} segments.")
# Step 3: Create concat list
create_concat_list(segments, concat_list)
# Step 4: Concatenate segments
concatenate_segments(concat_list, final_video)
print("All tasks completed successfully!")
if __name__ == "__main__":
main()