angelcam / rust-ac-ffmpeg

Simple and safe Rust interface for FFmpeg libraries.
MIT License
197 stars 33 forks source link

Webm to gif format conversion error #79

Open AH-dark opened 7 months ago

AH-dark commented 7 months ago

I was trying to convert a sticker in webm format to gif format, but some problems occurred.

An error occurs when running the following code:

use ac_ffmpeg::format::demuxer::{Demuxer, InputFormat};
use ac_ffmpeg::format::io::IO;
use ac_ffmpeg::format::muxer::{Muxer, OutputFormat};
use anyhow::Context;

pub(crate) fn convert_webm_to_gif(input_buffer: Vec<u8>) -> anyhow::Result<Vec<u8>> {
    let ictx = IO::from_read_stream(input_buffer.as_slice());
    let mut demuxer = Demuxer::builder()
        .input_format(Some(
            InputFormat::find_by_name("webm").context("WebM format not found")?,
        ))
        .build(ictx)?
        .find_stream_info(None)
        .map_err(|err| err.1)?;

    let video_stream_index = demuxer
        .streams()
        .iter()
        .position(|stream| {
            stream
                .codec_parameters()
                .as_video_codec_parameters()
                .is_some()
        })
        .ok_or(anyhow::anyhow!("No video stream found"))?;

    let mut output_buffer = Vec::new();
    let mut muxer = {
        let mut builder = Muxer::builder();
        builder
            .add_stream(&demuxer.streams()[video_stream_index].codec_parameters())
            .map_err(|err| anyhow::anyhow!("Failed to add stream to muxer: {}", err))?;
        builder
            .build(
                IO::from_write_stream(&mut output_buffer),
                OutputFormat::find_by_name("gif").context("GIF format not found")?,
            )
            .map_err(|err| anyhow::anyhow!("Failed to create muxer: {}", err))?
    };

    while let Some(packet) = demuxer.take()? {
        if packet.stream_index() == video_stream_index {
            muxer.push(packet.with_stream_index(0))?;
        }
    }

    // flush the muxer
    muxer.flush()?;
    muxer.close()?;

    Ok(output_buffer)
}

Error: [gif @ 0x128f08300] GIF muxer supports only a single video GIF stream.

AH-dark commented 7 months ago

I'm a noob developer, so please excuse me if I have some stupid questions. 😢