Skip to content

Commit

Permalink
Add float time input to extract_frame()
Browse files Browse the repository at this point in the history
  • Loading branch information
fisheggg committed Mar 6, 2024
1 parent 6fb4163 commit d4a8e9f
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions musicalgestures/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 \<input name\>FRAME\<frame number\>.\<file extension\>. 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:
Expand All @@ -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)
Expand All @@ -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:')

Expand Down

0 comments on commit d4a8e9f

Please sign in to comment.