-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathffmpeg.py
More file actions
234 lines (215 loc) · 7.26 KB
/
ffmpeg.py
File metadata and controls
234 lines (215 loc) · 7.26 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import numpy as np
import subprocess as sp
import os
import time
import ffmpeg as ff
DEVNULL = open(os.devnull, 'w')
# attempts to handle all float/integer conversions with and without normalizing
def convert_bit_depth(y, in_type, out_type, normalize=False):
in_type = np.dtype(in_type).type
out_type = np.dtype(out_type).type
if normalize:
peak = np.abs(y).max()
if peak == 0:
normalize = False
if issubclass(in_type, np.floating):
if normalize:
y /= peak
if issubclass(out_type, np.integer):
y *= np.iinfo(out_type).max
y = y.astype(out_type)
elif issubclass(in_type, np.integer):
if issubclass(out_type, np.floating):
y = y.astype(out_type)
if normalize:
y /= peak
elif issubclass(out_type, np.integer):
in_max = peak if normalize else np.iinfo(in_type).max
out_max = np.iinfo(out_type).max
if out_max > in_max:
y = y.astype(out_type)
y *= (out_max / in_max)
elif out_max < in_max:
y /= (in_max / out_max)
y = y.astype(out_type)
return y
def aureadmeta(fn):
if not os.path.exists(fn):
raise FileNotFoundError
probe = ffmpeg.probe(fn)
for stream in probe['streams']:
if stream['codec_type'] == 'audio':
meta = {
'channels': stream['channels'],
'sample_rate': int(stream['sample_rate']),
'duration': float(probe['format']['duration'])
}
return meta
return None
# auread should be combined with aureadmeta to not force the samplerate or input type if they are None
def auread(filename, sr=44100, mono=False, normalize=True, in_type=np.int16, out_type=np.float32):
in_type = np.dtype(in_type).type
out_type = np.dtype(out_type).type
channels = 1 if mono else 2
format_strings = {
np.float64: 'f64le',
np.float32: 'f32le',
np.int16: 's16le',
np.int32: 's32le',
np.uint32: 'u32le'
}
format_string = format_strings[in_type]
command = [
'ffmpeg',
'-i', filename,
'-f', format_string,
'-acodec', 'pcm_' + format_string,
'-ar', str(sr),
'-ac', str(channels),
'-']
p = sp.Popen(command, stdout=sp.PIPE, stderr=DEVNULL)
raw, err = p.communicate()
audio = np.frombuffer(raw, dtype=in_type)
if channels > 1:
audio = audio.reshape((-1, channels)).transpose()
if audio.size == 0:
return audio.astype(out_type), sr
audio = convert_bit_depth(audio, in_type, out_type, normalize)
return audio, sr
def auwrite(fn, audio, sr):
if len(audio.shape) > 1:
channels = np.min(audio.shape)
else:
channels = 1
format_strings = {
'float64': 'f64le',
'float32': 'f32le',
'int16': 's16le',
'int32': 's32le',
'uint32': 'u32le'
}
format_strings = {np.dtype(key): value for key,value in format_strings.items()}
format_string = format_strings[audio.dtype]
command = [
'ffmpeg',
'-y',
'-ac', str(channels),
'-ar', str(sr),
'-f', format_string,
'-i', 'pipe:',
fn]
p = sp.Popen(command, stdin=sp.PIPE, stdout=None, stderr=None)
if channels > 1 and audio.shape[0] == channels:
raw, err = p.communicate(audio.T.tobytes())
else:
raw, err = p.communicate(audio.tobytes())
def auchannels(y):
if len(y.shape) > 1:
return y.shape[0]
return 1
def aulen(y):
if len(y.shape) > 1:
return y.shape[1]
return len(y)
def convert_fraction_to_real(framerate):
if '/' in framerate:
num,div = framerate.split('/')
return float(num) / float(div)
return float(framerate)
import json
def vidreadmeta(fn):
if not os.path.exists(fn):
raise FileNotFoundError
probe = ff.probe(fn)
for stream in probe['streams']:
if stream['codec_type'] == 'video':
meta = {
'width': int(stream['width']),
'height': int(stream['height']),
'duration': float(probe['format']['duration']),
'framerate': convert_fraction_to_real(stream['avg_frame_rate'])
}
return meta
return None
def vidread(fn, samples=None, rate=None, hwaccel=None, rotate_90=False):
if not os.path.exists(fn):
raise FileNotFoundError
probe = ff.probe(fn)
out_params = {}
for stream in probe['streams']:
if stream['codec_type'] == 'video':
width, height = stream['width'], stream['height']
try:
if rotate_90 or stream['tags']['rotate'] in ['90','270','-90']: # not sure if -90 ever happens
width, height = height, width
except KeyError:
pass
if samples is not None:
duration = float(stream['duration'])
interval = duration / samples
out_params['r'] = 1 / interval
out_params['ss'] = interval / 2
elif rate is not None:
out_params['r'] = rate
out_params['ss'] = 1 / (2 * rate)
in_params = {}
if hwaccel is not None:
in_params['hwaccel'] = hwaccel
channels = 3
frame_number = -1
try:
proc = (
ff
.input(fn, **in_params)
.output('pipe:', format='rawvideo', pix_fmt='rgb24', **out_params)
.run_async(pipe_stdout=True)
)
while True:
in_bytes = proc.stdout.read(width*height*channels)
frame_number += 1
if not in_bytes:
break
in_frame = (
np
.frombuffer(in_bytes, np.uint8)
.reshape([height, width, channels])
)
yield in_frame
finally:
proc.stdout.close()
proc.wait()
class VideoWriter:
def __init__(self, fn, vcodec='libx264', fps=60, in_pix_fmt='rgb24', out_pix_fmt='yuv420p', input_args=None, output_args=None):
self.fn = fn
self.process = None
self.input_args = {} if input_args is None else input_args
self.output_args = {} if output_args is None else output_args
self.input_args['framerate'] = fps
self.input_args['pix_fmt'] = in_pix_fmt
self.output_args['pix_fmt'] = out_pix_fmt
self.output_args['vcodec'] = vcodec
def add(self, frame):
if self.process is None:
h,w = frame.shape[:2]
self.process = (
ff
.input('pipe:', format='rawvideo', s='{}x{}'.format(w, h), **self.input_args)
.output(self.fn, **self.output_args)
.overwrite_output()
.run_async(pipe_stdin=True)
)
self.process.stdin.write(
frame
.astype(np.uint8)
.tobytes()
)
def close(self):
if self.process is None:
return
self.process.stdin.close()
self.process.wait()
def vidwrite(fn, images, **kwargs):
writer = VideoWriter(fn, **kwargs)
for image in images:
writer.add(image)
writer.close()