-
Notifications
You must be signed in to change notification settings - Fork 1
/
ffmpeg_util.py
198 lines (166 loc) · 5.72 KB
/
ffmpeg_util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import json
import subprocess
import typing
from fractions import Fraction
import numpy as np
from pydantic import BaseModel
from exceptions import UserError
class VideoMetadata(BaseModel):
width: int = 0
height: int = 0
num_frames: int = 0
duration_sec: float = 0
fps: typing.Optional[float] = None
codec_name: typing.Optional[str] = None
class InputOutputVideoMetadata(BaseModel):
input: VideoMetadata
output: VideoMetadata
class AudioMetadata(BaseModel):
duration_sec: float = 0
codec_name: typing.Optional[str] = None
FFMPEG_ERR_MSG = (
"Unsupported File Format\n\n"
"We encountered an issue processing your file as it appears to be in a format not supported by our system or may be corrupted. "
"You can find a list of supported formats at [FFmpeg Formats](https://ffmpeg.org/general.html#File-Formats)."
)
def ffprobe_audio(input_path: str) -> AudioMetadata:
text = call_cmd(
"ffprobe",
"-v", "quiet",
"-print_format", "json",
"-show_streams", input_path,
"-select_streams", "a:0",
err_msg=FFMPEG_ERR_MSG,
) # fmt:skip
data = json.loads(text)
try:
stream = data["streams"][0]
except IndexError:
raise UserError(
"Input has no audio streams. Make sure the you have uploaded an appropriate audio/video file."
)
return AudioMetadata(
duration_sec=float(stream.get("duration") or 0),
codec_name=stream.get("codec_name"),
)
def ffprobe_video(input_path: str) -> VideoMetadata:
text = call_cmd(
"ffprobe",
"-v", "quiet",
"-print_format", "json",
"-show_streams", input_path,
"-select_streams", "v:0",
err_msg=FFMPEG_ERR_MSG,
) # fmt:skip
data = json.loads(text)
try:
stream = data["streams"][0]
except IndexError:
raise UserError(
"Input has no video streams. Make sure the video you have uploaded is not corrupted."
)
try:
fps = float(Fraction(stream["avg_frame_rate"]))
except ZeroDivisionError:
fps = None
metadata = VideoMetadata(
width=int(stream["width"]) // 2 * 2,
height=int(stream["height"]) // 2 * 2,
num_frames=int(stream.get("nb_frames") or 0),
duration_sec=float(stream.get("duration") or 0),
fps=fps,
codec_name=stream.get("codec_name"),
)
print(repr(metadata))
return metadata
def ffmpeg_read_input_frames(
*,
width: float,
height: float,
input_path: str,
fps: float,
pixel_format: str = "rgb24",
) -> typing.Iterator[np.ndarray]:
cmd_args = [
"ffmpeg", "-hide_banner", "-nostats",
"-i", input_path,
"-f", "rawvideo",
"-pix_fmt", pixel_format,
"-s", f"{width}x{height}",
"-r", str(fps),
"pipe:1",
] # fmt:skip
print("\t$ " + " ".join(cmd_args))
ffproc = subprocess.Popen(cmd_args, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
while True:
retcode = ffproc.poll()
if retcode is not None:
output = ffproc.stderr.read().decode()
err = subprocess.SubprocessError(output)
err.__cause__ = subprocess.CalledProcessError(retcode, ffproc.args, output)
raise UserError(FFMPEG_ERR_MSG) from err
im_bytes = ffproc.stdout.read(height * width * 3)
if not im_bytes:
break
im_cv2 = np.frombuffer(im_bytes, dtype=np.uint8).reshape((height, width, 3))
yield im_cv2
def ffmpeg_get_writer_proc(
*,
width: int,
height: int,
output_path: str,
fps: float,
audio_path: str,
pixel_format: str = "rgb24",
) -> subprocess.Popen:
cmd_args = [
"ffmpeg", "-hide_banner", "-nostats",
# "-thread_queue_size", "128",
"-pixel_format", pixel_format,
"-f", "rawvideo",
# "-vcodec", "rawvideo",
"-s", f"{width}x{height}",
"-r", str(fps),
"-i", "pipe:0", # stdin
"-i", audio_path,
"-map", "0:v", "-map", "1:a", "-shortest",
# "-c:a", "copy",
"-c:v", "libx264",
"-pix_fmt", "yuv420p", # because iphone, see https://trac.ffmpeg.org/wiki/Encode/H.264#Encodingfordumbplayers
# "-preset", "ultrafast",
output_path,
] # fmt:skip
print("\t$ " + " ".join(cmd_args))
return subprocess.Popen(
cmd_args,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
)
def ensure_img_even_dimensions(img: np.ndarray) -> np.ndarray:
"""make sure the image dimensions are divisble by 2 for ffmpeg libx264"""
return img[: img.shape[0] // 2 * 2, : img.shape[1] // 2 * 2]
def ffmpeg_write_output_frame(ffproc: subprocess.Popen, img: np.ndarray):
retcode = ffproc.poll()
if retcode is not None:
output = ffproc.stdout.read().decode()
err = subprocess.SubprocessError(output)
err.__cause__ = subprocess.CalledProcessError(retcode, ffproc.args, output)
raise UserError(FFMPEG_ERR_MSG) from err
ffproc.stdin.write(img.tostring())
def ffmpeg(*args) -> str:
return call_cmd("ffmpeg", "-hide_banner", "-y", *args, err_msg=FFMPEG_ERR_MSG)
def call_cmd(
*args, err_msg: str = "", ok_returncodes: typing.Iterable[int] = ()
) -> str:
print("\t$ " + " ".join(map(str, args)))
try:
return subprocess.check_output(args, stderr=subprocess.STDOUT, text=True)
except subprocess.CalledProcessError as e:
if e.returncode in ok_returncodes:
return e.output
err_msg = err_msg or f"{str(args[0]).capitalize()} Error"
try:
raise subprocess.SubprocessError(e.output) from e
except subprocess.SubprocessError as e:
raise UserError(err_msg) from e