Skip to content

Commit 3d499af

Browse files
committed
Added ability to reuse scenes
1 parent 7dea712 commit 3d499af

File tree

1 file changed

+41
-9
lines changed

1 file changed

+41
-9
lines changed

av1an.py

+41-9
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def __init__(self):
6464
self.encoding_params = ''
6565
self.video_filter = ''
6666
self.output_file = ''
67+
self.scenes = ''
6768
self.audio = ''
6869
# OS specific NULL pointer
6970

@@ -96,6 +97,7 @@ def arg_parsing(self):
9697
parser.add_argument('--force_fps', '-fps', type=int, default=0, help='Force fps of output file')
9798
parser.add_argument('--video_filter', '-vf', type=str, default=self.video_filter, help='FFmpeg video options')
9899
parser.add_argument('--pix_format', '-fmt', type=str, default=self.pix_format, help='FFmpeg pixel format')
100+
parser.add_argument('--scenes', '-s', type=str, default=self.scenes, help='File location for scenes')
99101

100102
# Test
101103
try:
@@ -107,6 +109,9 @@ def arg_parsing(self):
107109
# Pass command line args that were passed
108110
self.args = parser.parse_args()
109111

112+
# Set scenes if provided
113+
self.scenes = self.args.scenes
114+
110115
# Set encoder if provided
111116
self.encoder = self.args.encoder.strip()
112117
if self.encoder not in ('svt_av1', 'rav1e', 'aom'):
@@ -207,23 +212,50 @@ def extract_audio(self, input_vid):
207212
print('Audio stream file not found. Error in audio extraction')
208213
exit()
209214

210-
def split_video(self, input_vid):
211-
215+
def scenedetect(self, video, output):
212216
# PySceneDetect used split video by scenes and pass it to encoder
213217
# Optimal threshold settings 15-50
214-
print(f'\rSpliting video..', end='\r')
215218

216-
cmd2 = f'scenedetect -i {input_vid} --output .temp/ detect-content ' \
219+
cmd2 = f'scenedetect -i {video} --output {output} detect-content ' \
217220
f'--threshold {self.threshold} list-scenes '
218221
self.call_cmd(cmd2)
219-
scenes = '.'.join(input_vid.split('.')[:-1])
220-
with open(f'{join(self.here, ".temp", f"{scenes}-Scenes.csv")}') as csv_file:
222+
223+
def split(self, video, timecodes):
224+
cmd = f'{self.FFMPEG} -i {video} -map_metadata -1 -an -f segment -segment_times {timecodes} ' \
225+
f'-c copy -avoid_negative_ts 1 .temp/split/%04d.mkv'
226+
self.call_cmd(cmd)
227+
228+
def read_csv(self, file_path):
229+
with open(file_path) as csv_file:
221230
stamps = next(csv.reader(csv_file))
222231
stamps = stamps[1:]
223232
stamps = ','.join(stamps)
224-
cmd = f'{self.FFMPEG} -i {input_vid} -map_metadata -1 -an -f segment -segment_times {stamps} ' \
225-
f'-c copy -avoid_negative_ts 1 .temp/split/%04d.mkv'
226-
self.call_cmd(cmd)
233+
return stamps
234+
235+
def split_video(self, input_vid):
236+
237+
if self.scenes:
238+
239+
file_path = join(self.here, self.scenes)
240+
if os.path.exists(file_path):
241+
stamps = self.read_csv(file_path)
242+
self.split(input_vid, stamps)
243+
else:
244+
self.scenedetect(input_vid, '.')
245+
video = '.'.join(input_vid.split('.')[:-1])
246+
file_path = f'{join(self.here, f"{video}-Scenes.csv")}'
247+
248+
stamps = self.read_csv(file_path)
249+
self.split(input_vid, stamps)
250+
else:
251+
print(f'\rSpliting video..', end='\r')
252+
253+
self.scenedetect(input_vid, '.temp')
254+
video = '.'.join(input_vid.split('.')[:-1])
255+
file_path = f'{join(self.here, ".temp", f"{video}-Scenes.csv")}'
256+
257+
stamps = self.read_csv(file_path)
258+
self.split(input_vid, stamps)
227259

228260
def get_video_queue(self, source_path):
229261

0 commit comments

Comments
 (0)