[Python] Why is my FastAPI process being suspended, and how can I avoid this?

Discussão em 'Python' iniciado por Stack, Janeiro 19, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working on a web app using FastAPI that uses ffmpeg to overlay audio onto video for the user. I'm running into an issue where, when I use subprocess.run(cmd), it automatically suspends the process running my FastAPI app. I can't figure out how to get the error logs to help deduce why this is, and I haven't found anything online talking about this.

    @app.get("/overlay-audio/")
    async def get_video(audio_file: str, forged_name: Annotated[str, Query()] = "default"):
    video_path = os.path.join(output_path, "sample.mp4")
    audio_path = os.path.join(output_path, audio_file)
    forged_path = os.path.join(output_path, forged_name + ".mp4")
    print("Video path: " + video_path)
    print("Audio path: " + audio_path)
    print("Output path: " + forged_path)

    # command to recreate
    # ffmpeg -i input.mp4 -i input.wav -c:v copy -map 0:v:0 -map 1:a:0 -c:a aac -b:a 192k output.mp4

    cmd = ["/opt/homebrew/bin/ffmpeg",
    "-i", video_path,
    "-i", audio_path,
    "-c:v", "copy",
    "-map", "0:v:0",
    "-map", "1:a:0",
    "-c:a", "aac",
    "-b:a", "192k",
    forged_path]

    subprocess.run(cmd)

    return {"forged_vid": f"forged_{forged_name}"}


    if __name__ == "__main__":
    uvicorn.run("main:app", host="127.0.0.1", port=8000, reload=True)


    I've tried not writing output to the terminal, as I've read that could be a reason why it suspends using result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE), and I've also tried running it asynchronously to avoid blocking the event loop using

    result = await asyncio.create_subprocess_exec(
    *cmd,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE
    )
    ```,
    but nothing works. Any help or possible other ways to go about this would be greatly appreciated. Terminal output about the suspension: [1] + 12526 suspended (tty output) "/Users/<user>/Tech Projects/project/tts/videnv/bin/python"

    Continue reading...

Compartilhe esta Página