larksuite / rsmpeg

A Rust crate that exposes FFmpeg's power as much as possible.
https://docs.rs/rsmpeg/latest/rsmpeg/
MIT License
677 stars 41 forks source link

Error Decoding the .Mp4 video using Rsmpeg #192

Closed Codewithteju closed 2 months ago

Codewithteju commented 2 months ago

Hello everyone,

I am trying to decode a input video to get individual frames for further modification and encoding again back into a video using rsmpeg, But unfortunately I am not able to decode if the video is in .mp4 format, I am getting this error and stops the process-

[h264 @ 00000000xyz] No start code is found [h264 @ 00000000xyz] Error splitting the input into NAL units.

I am able to use the same code for decoding the video in .264 format and it is doing its work pretty good, Don't know what is causing the error. Can you address it or provide some insights in which way can rsmpeg be used to decode .mp4 videos too ?

Also I am using this rsmpeg version : rsmpeg = { version = "0.15.0", default-features = false, features = [ "ffmpeg6", "link_system_ffmpeg", ] } image

Codewithteju commented 2 months ago

I think I have solved it, the problem was about not setting input context with codecparameters, so after I setting up, I can decode any type of video using rsmpeg. Sorry for the inconvenience!

Codewithteju commented 2 months ago

Hey @ldm0, I have a doubt, I am trying to decode frames and I am getting them in YUV format, so now I wanted to convert them in RGB format and encode back into a video file. Is this possible? because somewhere I read this codec H.264 cannot encode frames in RGB format? Do you have any idea ?

ldm0 commented 2 months ago

Hey @ldm0, I have a doubt, I am trying to decode frames and I am getting them in YUV format, so now I wanted to convert them in RGB format and encode back into a video file. Is this possible? because somewhere I read this codec H.264 cannot encode frames in RGB format? Do you have any idea ?

Yeah, h264 doesn't accept RGB frames. You can run ffmpeg -h encoder=h264 to get the list of supported pixel formats: yuv420p yuvj420p yuv422p yuvj422p yuv444p yuvj444p nv12 nv16 nv21 yuv420p10le yuv422p10le yuv444p10le nv20le gray gray10le. You need to convert it back.

Codewithteju commented 2 months ago

Hoo okay, got it. Thanks @ldm0 for the quick reply. And one more thing, which is the best way to convert YUV frame into rgb format?

ldm0 commented 2 months ago

Hoo okay, got it. Thanks @ldm0 for the quick reply. And one more thing, which is the best way to convert YUV frame into rgb format?

Run two for loops and convert it pixel by pixel. :-)

fn yuv_to_rgb(y: u8, u: u8, v: u8) -> (u8, u8, u8) {
    let y = y as f32;
    let u = u as f32 - 128.0;
    let v = v as f32 - 128.0;

    let r = (y + 1.402 * v).round().clamp(0.0, 255.0) as u8;
    let g = (y - 0.344136 * u - 0.714136 * v).round().clamp(0.0, 255.0) as u8;
    let b = (y + 1.772 * u).round().clamp(0.0, 255.0) as u8;

    (r, g, b)
}
Codewithteju commented 2 months ago

Thanks for the code @ldm0 , I will do it 👏🤝