Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions mapillary_tools/ffmpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,10 @@ def extract_frames(
sample_prefix = sample_dir.joinpath(video_path.stem)
if stream_idx is not None:
stream_selector = ["-map", f"0:{stream_idx}"]
ouput_template = f"{sample_prefix}_{stream_idx}_%06d{FRAME_EXT}"
stream_specifier = f"{stream_idx}"
output_template = f"{sample_prefix}_{stream_idx}_%06d{FRAME_EXT}"
else:
stream_selector = []
ouput_template = f"{sample_prefix}_{NA_STREAM_IDX}_%06d{FRAME_EXT}"
stream_specifier = "v"
output_template = f"{sample_prefix}_{NA_STREAM_IDX}_%06d{FRAME_EXT}"

cmd: list[str] = [
# global options should be specified first
Expand All @@ -186,12 +184,12 @@ def extract_frames(
# filter videos
*["-vf", f"fps=1/{sample_interval}"],
# video quality level (or the alias -q:v)
*[f"-qscale:{stream_specifier}", "2"],
*["-qscale:v", "2"],
# -q:v=1 is the best quality but larger image sizes
# see https://stackoverflow.com/a/10234065
# *["-qscale:v", "1", "-qmin", "1"],
# output
ouput_template,
output_template,
]

self._run_ffmpeg(cmd)
Expand Down Expand Up @@ -227,12 +225,10 @@ def extract_specified_frames(
sample_prefix = sample_dir.joinpath(video_path.stem)
if stream_idx is not None:
stream_selector = ["-map", f"0:{stream_idx}"]
ouput_template = f"{sample_prefix}_{stream_idx}_%06d{FRAME_EXT}"
stream_specifier = f"{stream_idx}"
output_template = f"{sample_prefix}_{stream_idx}_%06d{FRAME_EXT}"
else:
stream_selector = []
ouput_template = f"{sample_prefix}_{NA_STREAM_IDX}_%06d{FRAME_EXT}"
stream_specifier = "v"
output_template = f"{sample_prefix}_{NA_STREAM_IDX}_%06d{FRAME_EXT}"

# Write the select filter to a temp file because:
# The select filter could be large and
Expand Down Expand Up @@ -269,8 +265,8 @@ def extract_specified_frames(
# vsync is deprecated by fps_mode,
# but fps_mode is not avaliable on some older versions ;(
# *[f"-fps_mode:{stream_specifier}", "passthrough"],
# Set the number of video frames to output
*[f"-frames:{stream_specifier}", str(len(frame_indices))],
# Set the number of video frames to output (this is an optimization to let ffmpeg stop early)
*["-frames:v", str(len(frame_indices))],
# Disabled because it doesn't always name the sample images as expected
# For example "select(n\,1)" we expected the first sample to be IMG_001.JPG
# but it could be IMG_005.JPG
Expand All @@ -279,12 +275,12 @@ def extract_specified_frames(
# *["-frame_pts", "1"],
],
# video quality level (or the alias -q:v)
*[f"-qscale:{stream_specifier}", "2"],
*["-qscale:v", "2"],
# -q:v=1 is the best quality but larger image sizes
# see https://stackoverflow.com/a/10234065
# *["-qscale:v", "1", "-qmin", "1"],
# output
ouput_template,
output_template,
]
self._run_ffmpeg(cmd)
finally:
Expand Down