Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webm to gif format conversion error #79

Open
AH-dark opened this issue Mar 16, 2024 · 1 comment
Open

Webm to gif format conversion error #79

AH-dark opened this issue Mar 16, 2024 · 1 comment

Comments

@AH-dark
Copy link

AH-dark commented Mar 16, 2024

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
Copy link
Author

AH-dark commented Mar 16, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant