Skip to content

Commit cae142e

Browse files
authored
fix: sample video quality (#761)
* fix: sample video quality * add comment * fix typo * generate_binary_search * Revert "generate_binary_search" This reverts commit 9c595ec.
1 parent a1dd46e commit cae142e

1 file changed

Lines changed: 10 additions & 14 deletions

File tree

mapillary_tools/ffmpeg.py

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,10 @@ def extract_frames(
169169
sample_prefix = sample_dir.joinpath(video_path.stem)
170170
if stream_idx is not None:
171171
stream_selector = ["-map", f"0:{stream_idx}"]
172-
ouput_template = f"{sample_prefix}_{stream_idx}_%06d{FRAME_EXT}"
173-
stream_specifier = f"{stream_idx}"
172+
output_template = f"{sample_prefix}_{stream_idx}_%06d{FRAME_EXT}"
174173
else:
175174
stream_selector = []
176-
ouput_template = f"{sample_prefix}_{NA_STREAM_IDX}_%06d{FRAME_EXT}"
177-
stream_specifier = "v"
175+
output_template = f"{sample_prefix}_{NA_STREAM_IDX}_%06d{FRAME_EXT}"
178176

179177
cmd: list[str] = [
180178
# global options should be specified first
@@ -186,12 +184,12 @@ def extract_frames(
186184
# filter videos
187185
*["-vf", f"fps=1/{sample_interval}"],
188186
# video quality level (or the alias -q:v)
189-
*[f"-qscale:{stream_specifier}", "2"],
187+
*["-qscale:v", "2"],
190188
# -q:v=1 is the best quality but larger image sizes
191189
# see https://stackoverflow.com/a/10234065
192190
# *["-qscale:v", "1", "-qmin", "1"],
193191
# output
194-
ouput_template,
192+
output_template,
195193
]
196194

197195
self._run_ffmpeg(cmd)
@@ -227,12 +225,10 @@ def extract_specified_frames(
227225
sample_prefix = sample_dir.joinpath(video_path.stem)
228226
if stream_idx is not None:
229227
stream_selector = ["-map", f"0:{stream_idx}"]
230-
ouput_template = f"{sample_prefix}_{stream_idx}_%06d{FRAME_EXT}"
231-
stream_specifier = f"{stream_idx}"
228+
output_template = f"{sample_prefix}_{stream_idx}_%06d{FRAME_EXT}"
232229
else:
233230
stream_selector = []
234-
ouput_template = f"{sample_prefix}_{NA_STREAM_IDX}_%06d{FRAME_EXT}"
235-
stream_specifier = "v"
231+
output_template = f"{sample_prefix}_{NA_STREAM_IDX}_%06d{FRAME_EXT}"
236232

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

0 commit comments

Comments
 (0)