diff --git a/musicalgestures/_utils.py b/musicalgestures/_utils.py index eec73e8..93e25e9 100644 --- a/musicalgestures/_utils.py +++ b/musicalgestures/_utils.py @@ -571,7 +571,7 @@ def cast_into_avi(filename, target_name=None, overwrite=False): def extract_frame( filename: str, frame: int=None, - time: str=None, + time: str|float=None, target_name: str=None, overwrite: bool=False, )-> str: @@ -581,12 +581,13 @@ def extract_frame( Args: filename (str): Path to the input video file. frame (int): The frame number to extract. - time (str): The time in HH:MM:ss.ms where to extract the frame from. + time (str|float): The time in HH:MM:ss.ms where to extract the frame from. If float, it is interpreted as seconds from the start of the video. target_name (str, optional): The name for the output file. If None, the name will be \FRAME\.\. Defaults to None. overwrite (bool, optional): Whether to allow overwriting existing files or to automatically increment target filename to avoid overwriting. Defaults to False. """ import os + import datetime if frame is not None and time is not None: raise ValueError("frame and time cannot be both not None.") if frame is None and time is None: @@ -597,6 +598,7 @@ def extract_frame( if frame is not None: target_name = f"{name}_frame_{str(frame)}.png" elif time is not None: + time = time if isinstance(time, str) else datetime.datetime.fromtimestamp(time-3600).strftime('%H:%M:%S.%f') target_name = f"{name}_time_{time}.png" if not overwrite: target_name = generate_outfilename(target_name) @@ -606,14 +608,16 @@ def extract_frame( '-y' if overwrite else "-n", '-i', filename, "-vf", f"select='eq(n\,{frame})'", - "-vframes", "1", + "-vsync", "0", + # "-vframes", "1", target_name] elif time is not None: cmds = ['ffmpeg', '-y' if overwrite else "-n", '-i', filename, - "-ss", f"{time}", - "-frames:v", "1", + "-vf", f"select='eq(t\,{time})'", + "-vsync", "0", + # "-vframes", "1", target_name] ffmpeg_cmd(cmds, get_length(filename), pb_prefix='Extracting frame:')