1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] No audio in the final video when converting webm blobs to mp4 using ffmpeg

Discussão em 'Python' iniciado por Stack, Setembro 28, 2024 às 18:02.

  1. Stack

    Stack Membro Participativo

    I trying to record user camera and microphone and using MediaRecorder to convert the stream to blobs and sending the blobs every 2 second to the backend using websocket. Everything is working fine, but when I checked the final mp4 video in the backend, it doesn't have any audio to it, I try specifying the audio codec, but still no help.

    My frontend code :-

    const micStream = await navigator.mediaDevices.getUserMedia({ audio: true });

    const recorder = new MediaRecorder(stream, {
    mimeType: 'video/webm;codecs=H264',
    videoBitsPerSecond: 8000000,
    audioBitsPerSecond : 8000000
    });

    recorder.ondataavailable = (e: BlobEvent) => {
    websocket.send(e.data)
    }
    recorder.start(2000);


    And here is the backend code :-

    @router.websocket("/streamaudio")
    async def websocket_endpoint(websocket: WebSocket):
    await manager.connect(websocket)

    recordingFile = os.path.join(os.getcwd(), f"recording_.mp4")

    command = [
    'ffmpeg',
    '-y',
    '-i',
    '-',
    '-codec:v',
    'copy',
    '-c:a', 'aac',
    '-y',
    '-f', 'mp4',
    recordingFile,
    # "-"
    # f'output{queueNumber}.mp4',
    ]


    try:
    while True:
    try:

    data = await websocket.receive_bytes()

    process.stdin.send(data)

    except RuntimeError:
    break
    except WebSocketDisconnect:
    print(f"Client disconnected: {websocket.client.host}")
    finally:
    manager.disconnect(websocket)
    await process.stdin.aclose()
    await process.wait()

    Continue reading...

Compartilhe esta Página