h4tr3d / avcpp

C++ wrapper for FFmpeg
Other
429 stars 78 forks source link

Can you provide an example of encoding image sequence into video file #139

Open zjd1988 opened 3 weeks ago

zjd1988 commented 3 weeks ago

wonderful work!! Now I need to encode image sequence into file,I modify code from https://github.com/h4tr3d/avcpp/blob/master/example/api2-samples/api2-scale-video.cpp

but get errors:

....
[avi @ 0x5555558d0600] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 12 >= 12
[avi @ 0x5555558d0600] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 13 >= 13
[avi @ 0x5555558d0600] Application provided invalid, non monotonically increasing dts to muxer in stream 0: 14 >= 14
....

my codes

            // use opencv read image files
            cv::Mat frame = cv::imread(file_name);
            std::string av_format = "bgr24";
            int frame_h = frame.rows;
            int frame_w = frame.cols;
            int frame_ch = frame.channels();
            int frame_size = frame_h * frame_w * frame_ch;
            const uint8_t* frame_data = frame.data;
            av::VideoFrame video_frame(frame_data, frame_size, av::PixelFormat(av_format), frame_w, frame_h);
            // convert bgr24 frame to yuv420p
            av::VideoFrame convert_frame = m_rescaler->rescale(video_frame, ec);

           // encode frame to packet
           opkt = m_venc->encode(convert_frame, ec);

           opkt.setStreamIndex(0);

           // error occur
           m_octx->writePacket(opkt, ec);
h4tr3d commented 2 weeks ago

Just point Timebase and PTS.

Simplest way, set Timebase to the value inverted to required FPS, for example 1/60 and use sequential counter for the frames: 1, 2, 3, 4, ..., n. So, each frame time can be simple calculated in seconds: seq timebase = n 1 / 60.

Set timebase and PTS for the video_frame.

Refer to:

Set same timebase for the Encoder too.