vse_toolbox/auto_splitter.py

37 lines
971 B
Python
Raw Normal View History

2024-05-24 09:44:19 +02:00
import re
import subprocess
from vse_toolbox.constants import AUTO_SPLITTER_LOG
from vse_toolbox import file_utils
class AutoSplitter(object):
def __init__(self, paths, fps=None):
self.paths = paths
self.fps = fps
self.log_path = AUTO_SPLITTER_LOG
def launch_analysis(self, threshold=0.6):
ffmpeg_cmd = f"ffmpeg -i {str(self.paths[0])} -filter:v \"select='gt(scene,{threshold})',showinfo\" -f null - 2> {str(self.log_path)}"
print(ffmpeg_cmd)
subprocess.call(ffmpeg_cmd, shell=True)
def get_split_times(self, as_frame=True):
log = file_utils.read_file(self.log_path)
timecodes = re.findall(r'pts_time:([\d.]+)', log)
if as_frame:
# convert timecode to frame number
if not self.fps:
self.fps = float(re.findall(r'([\d]+) fps', log)[0])
return [round(float(time) * self.fps) for time in timecodes]
return timecodes