question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

quiet mode for run_async method might cause ffmpeg process to stick.

See original GitHub issue

What I experienced

Recently, I was using this module in my project for decoding video into pipe as well as encoding target frames into a result video. But I found that setting the parameter “quiet” of the encoder process to True (which set both stdout and stderr to PIPE) will cause the ffmpeg process to stick at some stage ( and in this case, I cannot write anything into its stdin). If I leave “quiet” to default or set both pipe_stdout and pipe_stderr to False, everything goes well.

What I tried

I tried to edit the run_async function, changing it from

args = compile(stream_spec, cmd, overwrite_output=overwrite_output)
    stdin_stream = subprocess.PIPE if pipe_stdin else None
    stdout_stream = subprocess.PIPE if pipe_stdout or quiet else None
    stderr_stream = subprocess.PIPE if pipe_stderr or quiet else None
    return subprocess.Popen(
        args, stdin=stdin_stream, stdout=stdout_stream, stderr=stderr_stream)

to

 args = compile(stream_spec, cmd, overwrite_output=overwrite_output)
    stdin_stream = subprocess.PIPE if pipe_stdin else None
    stdout_stream = subprocess.PIPE if pipe_stdout else None
    stderr_stream = subprocess.PIPE if pipe_stderr else None
    if quiet:
        stdout_stream = stderr_stream = subprocess.DEVNULL
    return subprocess.Popen(
        args, stdin=stdin_stream, stdout=stdout_stream, stderr=stderr_stream)

Then, using this modified version of ffmpeg-python in my project, it works without any problems.

After all, I don’t think this issue is solved, for it will stick again if setting either pipe_stdout or pipe_stderr (I confirmed).

My forked version works. Should I make a pull request? Or do it only when we find the real reason for this issue.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:7
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
RZinmancommented, Aug 9, 2020

The issue seems to be that pipe buffers are finite in size, so they fill up and eventually block. One workaround when running asynchronously is to just read() from the output pipes periodically. E.g., if you have a loop where you’re feeding frames to stdin:

process = (
    ffmpeg
    .input('pipe:', format='rawvideo', pix_fmt='rgb24', s='{}x{}'.format(width, height))
    .output(video_file_out, pix_fmt='yuv420p')
    .run_async(pipe_stdin=True, quiet=True)
)

# Set non-blocking in case we don't get output for a frame
fcntl.fcntl(process.stderr, fcntl.F_SETFL, os.O_NONBLOCK)

for frame in my_frames:
    process.stdin.write(frame)
    
    # Do a read to clear the output pipe
    process.stderr.read()

But this is a bit hacky and relies on ffmpeg not producing too much output to stderr in one frame. I’d like it if the pipe_stdout and pipe_stderr arguments could take subprocess pipes directly. E.g., run_async(pipe_stdout=subprocess.PIPE, pipe_stderr=subprocess.DEVNULL).

0reactions
ArchieMengcommented, Oct 18, 2022

Ahhh… Is that published to pypi?

No. It is still a forked ffmpeg-python repo. And since I have been away from such stuff for a long time, it is out of date with the repo now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I make ffmpeg be quieter/less verbose? - Super User
Show nothing at all; be silent. 'panic, 0'. Only show fatal errors which could lead the process to crash, such as and assert...
Read more >
Discord.Net sending audio with ffmpeg only works once (the ...
Open ffmpeg -> encode .mp3 -> create PCM stream -> pump the encoded .mp3 ... But the bot remains silent, even though the...
Read more >
2012-February.txt - FFmpeg
And I need a way to track down what is wrong. So how can I find the keyframe information for a mp4 video...
Read more >
ffmpeg-python documentation - GitHub Pages
Build command-line arguments to be passed to ffmpeg. Invoke ffmpeg for the supplied node graph. capture_stdout – if True, capture stdout (to be...
Read more >
Diff - 4ddaa6f836..53ecf9341f - chromium/src - Git at Google
-81,8 +81,10 @@ // The task (if one exists) would have run already, we just need to make // sure it was completed....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found