2

I need to trigger a long ffmpeg process by nicegui button. I have created a code :

from concurrent.futures import ProcessPoolExecutor
import shlex, subprocess
import argparse
import asyncio
from nicegui import ui, app

def run_cmd():
  cmd = "ffmpeg -y -i data/video.mp4 -acodec libmp3lame -ab 128000 data/video.mp3"
  subprocess.call(shlex.split(cmd))

pool = ProcessPoolExecutor()
async def async_run():
  loop = asyncio.get_running_loop()
  await loop.run_in_executor(pool, run_cmd)

args = argparse.ArgumentParser()
args.add_argument("--webui", action="store_true")
args = args.parse_args()

if args.webui:
  ui.button('Translate', on_click=async_run)
  app.on_shutdown(pool.shutdown)
  ui.run()
else:
  run_cmd()

ffmpeg works well if run just from python. But when I run it from nicegui (python3 ./main.py --webui) ffmpeg failed or crashed:

...
Error while decoding stream #0:1: Invalid argument
[aac @ 0x555e54100440] Sample rate index in program config element does not match the sample rate index configured by the container.
[aac @ 0x555e54100440] Inconsistent channel configuration.
[aac @ 0x555e54100440] get_buffer() failed
...
ac @ 0x555e54100440] Error decoding AAC frame header.
Error while decoding stream #0:1: Error number -50531338 occurred
[aac @ 0x555e54100440] channel element 2.8 is not allocated
Error while decoding stream #0:1: Invalid data found when processing input
[aac @ 0x555e54100440] Pulse data corrupt or invalid.
Error while decoding stream #0:1: Invalid data found when processing input
ffmpeg: psymodel.c:576: calc_energy: Assertion `el >= 0' failed.
...

What am I doing wrong? Or maybe there is a more straightforward pattern to start long-term background process with nicegui?

user1113159
  • 645
  • 2
  • 7
  • 14
  • I don't know nicegui, but it looks like the input video file is not valid (FFmpeg can't decode it) . For testing, try using full path of `data/video.mp4` and `data/video.mp3`, and maybe also full path of `ffmpeg` (in case few ffmpeg are installed). – Rotem Mar 23 '23 at 20:34
  • But it works well when I run the same cmd just from python code (without `--webui`) – user1113159 Mar 24 '23 at 08:58
  • Are you sure `--webui` doesn't affect the path? Does it affect permissions? Command "just from Python", I assume you mean from command line: `python3 ./main.py` – Rotem Mar 24 '23 at 09:14
  • Yep provided code works when I run it with `python3 ./main.py` and does not work if I run it with --webui flag, and then press the Translate button on the browser. I was trying to detect what are changes inside the subprocess run, but with no success. `pwd` returns the same result, `env` are also the same. – user1113159 Mar 24 '23 at 11:02
  • Your example code works fine for me with and without the --webui parameter. I'm on macOS, M1 chip – Rodja Mar 25 '23 at 07:52
  • You could try with `ui.run(reload=False)` which will deactivate starting the server in a subprocess. – Rodja Mar 25 '23 at 12:37
  • I've also created an [example to extract frames with ffmpeg](https://github.com/zauberzeug/nicegui/blob/main/examples/ffmpeg_extract_images/main.py) inspired by this question. Please try it. Maybe it's audio-related? – Rodja Mar 25 '23 at 12:47
  • see https://github.com/WolfgangFahl/nicescad/blob/main/nicescad/openscad.py for an example how to call a command line based process which is used in nicegui see render function in https://github.com/WolfgangFahl/nicescad/blob/main/nicescad/webserver.py – Wolfgang Fahl Aug 29 '23 at 07:09

0 Answers0