Skip to content

Commit 390da5a

Browse files
author
Daniel Flores
committed
Factor out shared ffmpeg functions
1 parent 9148c52 commit 390da5a

File tree

1 file changed

+40
-61
lines changed

1 file changed

+40
-61
lines changed

test/generate_reference_resources.py

Lines changed: 40 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,40 @@ def convert_image_to_tensor(image_path):
2828
os.remove(image_path)
2929

3030

31+
def get_frame_by_index(video_path, frame, output_path, stream):
32+
cmd = [
33+
"ffmpeg",
34+
"-y",
35+
"-i",
36+
video_path,
37+
"-map",
38+
f"0:{stream}",
39+
"-vf",
40+
f"select=eq(n\\,{frame})",
41+
"-vsync",
42+
"vfr",
43+
"-q:v",
44+
"2",
45+
output_path,
46+
]
47+
subprocess.run(cmd, check=True)
48+
49+
50+
def get_frame_by_timestamp(video_path, timestamp, output_path):
51+
cmd = [
52+
"ffmpeg",
53+
"-y",
54+
"-ss",
55+
str(timestamp),
56+
"-i",
57+
video_path,
58+
"-frames:v",
59+
"1",
60+
output_path,
61+
]
62+
subprocess.run(cmd, check=True)
63+
64+
3165
def main():
3266
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
3367
TORCHCODEC_PATH = os.path.dirname(SCRIPT_DIR)
@@ -47,42 +81,15 @@ def main():
4781
# so that the name of reference frames matches the index when accessing that
4882
# frame in the Python decoder.
4983
output_bmp = f"{VIDEO_PATH}.stream{stream}.frame{frame:06d}.bmp"
50-
frame_name = f"{frame:06d}"
51-
cmd = [
52-
"ffmpeg",
53-
"-y",
54-
"-i",
55-
VIDEO_PATH,
56-
"-map",
57-
f"0:{stream}",
58-
"-vf",
59-
f"select=eq(n\\,{frame})",
60-
"-vsync",
61-
"vfr",
62-
"-q:v",
63-
"2",
64-
output_bmp,
65-
]
66-
subprocess.run(cmd, check=True)
84+
get_frame_by_index(VIDEO_PATH, frame, output_bmp, stream=stream)
6785
convert_image_to_tensor(output_bmp)
6886

6987
# Extract individual frames at specific timestamps, including the last frame of the video.
7088
seek_timestamp = [6.0, 6.1, 10.0, 12.979633]
7189
timestamp_name = [f"{seek_timestamp:06f}" for seek_timestamp in seek_timestamp]
7290
for timestamp, name in zip(seek_timestamp, timestamp_name):
7391
output_bmp = f"{VIDEO_PATH}.time{name}.bmp"
74-
cmd = [
75-
"ffmpeg",
76-
"-y",
77-
"-ss",
78-
str(timestamp),
79-
"-i",
80-
VIDEO_PATH,
81-
"-frames:v",
82-
"1",
83-
f"{VIDEO_PATH}.time{name}.bmp",
84-
]
85-
subprocess.run(cmd, check=True)
92+
get_frame_by_timestamp(VIDEO_PATH, timestamp, output_bmp)
8693
convert_image_to_tensor(output_bmp)
8794

8895
# This video was generated by running the following:
@@ -93,22 +100,8 @@ def main():
93100
VIDEO_PATH = os.path.join(RESOURCES_DIR, "h265_video.mp4")
94101
FRAMES = [5]
95102
for frame in FRAMES:
96-
frame_name = f"{frame:06d}"
97-
output_bmp = f"{VIDEO_PATH}.stream0.frame{frame_name}.bmp"
98-
cmd = [
99-
"ffmpeg",
100-
"-y",
101-
"-i",
102-
VIDEO_PATH,
103-
"-vf",
104-
f"select=eq(n\\,{frame})",
105-
"-vsync",
106-
"vfr",
107-
"-q:v",
108-
"2",
109-
output_bmp,
110-
]
111-
subprocess.run(cmd, check=True)
103+
output_bmp = f"{VIDEO_PATH}.stream0.frame{frame:06d}.bmp"
104+
get_frame_by_index(VIDEO_PATH, frame, output_bmp, stream=0)
112105
convert_image_to_tensor(output_bmp)
113106

114107
# This video was generated by running the following:
@@ -118,22 +111,8 @@ def main():
118111
FRAMES = [10]
119112

120113
for frame in FRAMES:
121-
frame_name = f"{frame:06d}"
122-
output_bmp = f"{VIDEO_PATH}.stream0.frame{frame_name}.bmp"
123-
cmd = [
124-
"ffmpeg",
125-
"-y",
126-
"-i",
127-
VIDEO_PATH,
128-
"-vf",
129-
f"select=eq(n\\,{frame})",
130-
"-vsync",
131-
"vfr",
132-
"-q:v",
133-
"2",
134-
output_bmp,
135-
]
136-
subprocess.run(cmd, check=True)
114+
output_bmp = f"{VIDEO_PATH}.stream0.frame{frame:06d}.bmp"
115+
get_frame_by_index(VIDEO_PATH, frame, output_bmp, stream=0)
137116
convert_image_to_tensor(output_bmp)
138117

139118

0 commit comments

Comments
 (0)