@@ -2240,3 +2240,83 @@ def init_named_pipes(
22402240 args ["global_options" ]["y" ] = None
22412241
22422242 return stack if len (input_info ) or len (output_info ) else None
2243+
2244+
2245+ def assign_std_pipes (
2246+ args : FFmpegArgs ,
2247+ input_info : list [InputSourceDict ],
2248+ output_info : list [OutputDestinationDict ],
2249+ use_sp_run : bool = False ,
2250+ ) -> tuple [int | IO | None , int | IO | None , bytes | None ]:
2251+ """initialize named pipes for read & write operations with FFmpeg
2252+
2253+ :param args: FFmpeg option arguments (modified)
2254+ :param input_info: list of input information
2255+ :param output_info: list of output information
2256+ :param use_sp_run: True to set `stdin` output to `None` even if input
2257+ data is given (so it's compatible with `subprocess.run()`)
2258+ :returns stdin: stdin argument of subsequent ffmpegprocess.Popen call
2259+ :returns stdout: stdout argument of subsequent ffmpegprocess.Popen call
2260+ :returns input: input argument of subsequent ffmpegprocess.Popen call
2261+
2262+ In addition to the retured list, this function modifies the dicts in its arguements.
2263+
2264+ - The pipe names are assigned to the URLs of FFmpeg input and output (`args['inputs'][][0]`
2265+ and `args['outputs'][][0]`)
2266+ - The reader threads for FFmpeg outputs that are written to buffers (i.e.,
2267+ `output_info[]['dst_type']=='buffer'`) are saved as `output_info[]['reader']`
2268+ so the reader object can be used to retrieve the data.
2269+
2270+
2271+ if any output is a piped, overwrite flag (-y) is automatically inserted
2272+ """
2273+
2274+ # configure output pipes
2275+ use_stdin = use_stdout = False
2276+ stdin = stdout = pinput = None
2277+ for i , (output , info ) in enumerate (zip (args ["outputs" ], output_info )):
2278+ if output [0 ] is None or utils .is_pipe (output [0 ]):
2279+ if use_stdout :
2280+ raise FFmpegioError (
2281+ "More than 1 pipe to output found. Cannot use standard pipes."
2282+ )
2283+ use_stdout = True
2284+ assign_output_url (args , i , "pipe:1" )
2285+
2286+ dst_type = info ["dst_type" ]
2287+ if dst_type == "fileobj" :
2288+ stdout = info ["fileobj" ]
2289+ elif dst_type == "buffer" :
2290+ stdout = fp .PIPE
2291+ else :
2292+ raise FFmpegioError (f"{ dst_type = } is an unknown output data type." )
2293+
2294+ # configure input pipes (if needed)
2295+ for i , (input , info ) in enumerate (zip (args ["inputs" ], input_info )):
2296+ if input [0 ] is None or utils .is_pipe (input [0 ]):
2297+ if use_stdin :
2298+ raise FFmpegioError (
2299+ "More than 1 pipe to input found. Cannot use standard pipes."
2300+ )
2301+ use_stdin = True
2302+ assign_input_url (args , i , "pipe:0" )
2303+ src_type = info ["src_type" ]
2304+ if src_type == "fileobj" :
2305+ stdin = info ["fileobj" ]
2306+ elif src_type == "buffer" :
2307+ if "buffer" in info :
2308+ pinput = info ["buffer" ]
2309+ if not use_sp_run :
2310+ stdin = fp .PIPE
2311+ else :
2312+ stdin = fp .PIPE
2313+ else :
2314+ raise FFmpegioError (f"{ src_type = } is an unknown input data type." )
2315+
2316+ if use_stdout :
2317+ # if any output is piped, must run in the overwrite mode
2318+ args ["global_options" ].pop ("n" , None )
2319+ args ["global_options" ]["y" ] = None
2320+
2321+ return stdin , stdout , pinput
2322+
0 commit comments