Skip to content

Commit 01461e9

Browse files
committed
init_media_write_outputs() - fixed merge_audio_streams clause
1 parent 9de5772 commit 01461e9

1 file changed

Lines changed: 79 additions & 2 deletions

File tree

src/ffmpegio/configure.py

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1890,9 +1890,13 @@ def init_media_write_outputs(
18901890
do_merge = bool(merge_audio_streams)
18911891
if do_merge:
18921892
try:
1893-
a_ids = [i for i, info in enumerate(input_info) if info["media_type"] == "audio"]
1893+
a_ids = [
1894+
i for i, info in enumerate(input_info) if info["media_type"] == "audio"
1895+
]
18941896
except KeyError as e:
1895-
raise NotImplementedError('audio merging mode is not currently implemented. Please use the `complex_filtergraph=ffmpegio.filtergraph.presets.merge_audio(...)` to assign a custom filtergraph.')
1897+
raise NotImplementedError(
1898+
"audio merging mode is not currently implemented. Please use the `complex_filtergraph=ffmpegio.filtergraph.presets.merge_audio(...)` to assign a custom filtergraph."
1899+
)
18961900
do_merge = len(a_ids) > 1
18971901
if do_merge:
18981902
if merge_audio_streams is True:
@@ -2324,3 +2328,76 @@ def assign_std_pipes(
23242328

23252329
return stdin, stdout, pinput
23262330

2331+
2332+
def init_std_pipes(
2333+
stdin: IO | None,
2334+
stdout: IO | None,
2335+
input_info: list[InputSourceDict],
2336+
output_info: list[OutputDestinationDict],
2337+
update_rate: float | None = None,
2338+
blocksize: int | None = None,
2339+
queue_size: int | None = None,
2340+
) -> ExitStack | None:
2341+
"""initialize named pipes for read & write operations with FFmpeg
2342+
2343+
:param args: FFmpeg option arguments (modified)
2344+
:param input_info: FFmpeg input information, its length matches that of `args['inputs']`
2345+
:param output_info: FFmpeg output information, its length matches that of `args['outputs']` (modified)
2346+
:param update_rate: target rate at which queue transactions will occur for raw data output,
2347+
defaults to None (1 video frame or 1024 audio sample at a time)
2348+
:param blocksize: encoded data output block size in bytes, defaults to None (2**20 bytes)
2349+
:returns: a list of indices of the FFmpeg outputs that are raw data streams
2350+
2351+
In addition to the retured list, this function modifies the dicts in its arguements.
2352+
2353+
- The pipe names are assigned to the URLs of FFmpeg input and output (`args['inputs'][][0]`
2354+
and `args['outputs'][][0]`)
2355+
- The reader threads for FFmpeg outputs that are written to buffers (i.e.,
2356+
`output_info[]['dst_type']=='buffer'`) are saved as `output_info[]['reader']`
2357+
so the reader object can be used to retrieve the data.
2358+
2359+
2360+
if any output is a piped, overwrite flag (-y) is automatically inserted
2361+
"""
2362+
2363+
stack = ExitStack()
2364+
in_use = False
2365+
wr_kws = {"queuesize": queue_size} if queue_size else {}
2366+
2367+
# configure output pipes
2368+
for info in output_info:
2369+
dst_type = info["dst_type"]
2370+
if dst_type == "buffer":
2371+
kws = {**wr_kws}
2372+
if "raw_info" in info:
2373+
dtype, shape, rate = info["raw_info"]
2374+
kws["itemsize"] = utils.get_samplesize(shape, dtype)
2375+
if update_rate is not None:
2376+
kws["nmin"] = round(rate / update_rate) or 1
2377+
else:
2378+
# assume encoded output
2379+
kws["itemsize"] = 1
2380+
kws["nmin"] = blocksize or 2**16
2381+
info["reader"] = reader = ReaderThread(stdout, **kws)
2382+
stack.enter_context(reader) # starts thread & wait for pipe connection
2383+
in_use = True
2384+
break
2385+
2386+
# configure input pipes (if needed)
2387+
for info in input_info:
2388+
src_type = info["src_type"]
2389+
if src_type == "buffer":
2390+
writer = WriterThread(stdin, **wr_kws)
2391+
# starts thread & wait for pipe connection
2392+
stack.enter_context(writer)
2393+
in_use = True
2394+
if "buffer" in info:
2395+
# data buffer given, feed the data and terminate
2396+
writer.write(info["buffer"])
2397+
writer.write(None) # close the writer immediately
2398+
else:
2399+
# if no data given, provide the access to the writer
2400+
info["writer"] = writer
2401+
break
2402+
2403+
return stack if in_use else None

0 commit comments

Comments
 (0)