简体   繁体   中英

FFmpeg API example (encode_video.c) does not work correctly

I am using the official encode_video.c example to test if FFmpeg works correctly for me. I got the pre-built windows edition from ffmpeg.zeranoe.com/builds . It is built already with libx264 and other external libraries. I got both dev and shared editions and added the DLLs, header files and libs accordingly in Visual Studio.

Now the encode_video.c example does not work correctly.


What I tried:

I compiled the example and run it on many different file formats and codecs such as the following.

First I tried all of these file formats (.mp4, .m4v, .h264, .x264, .avi, .flv) with codec name as libx264. The code executed without errors but the output video file did not play in VLC or Windows 10 default player.

Next, I tried all of those above file formats but with codec name as mpeg4. The code executed without errors but the output video file played only for .m4v in VLC.


What is expected:

All of those combinations should have produced a video file which could be played in VLC. None of them worked except for .m4v as file format and mpeg4 as codec name.


Please tell me how to make this work for h264. I mainly want it to work for h264 as that is only important for now.

I am running the code like ./encode_video.exe test.mp4 libx264 where first argument is output filename and second argument is codec name.

This is the output for test.mp4 and libx264 as command line arguments https://imgur.com/a/AHLQwuK

It seems that in the encode function, it goes over the below code and returns because of AVERROR(EAGAIN) or AVERROR_EOF. Please tell me what is happening.

while (ret >= 0) {
        ret = avcodec_receive_packet(enc_ctx, pkt);
        if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
            return;
        else if (ret < 0) {
            fprintf(stderr, "Error during encoding\n");
            exit(1);
        }

        printf("Write packet %3"PRId64" (size=%5d)\n", pkt->pts, pkt->size);
        fwrite(pkt->data, 1, pkt->size, outfile);
        av_packet_unref(pkt);
    }

I used DepenciesGUI to find out the DLLs linked and it shows that the DLLs are correctly linked. Please help me figure out what the problem is now!!

I found this question because I had the same issue (couldn't watch video generated by that example). Regarding your concern:

It seems that in the encode function, it goes over the below code and returns because of AVERROR(EAGAIN) or AVERROR_EOF. Please tell me what is happening.

While stepping through the code in debugger, I too noticed those occasional "errors". However, those frames are eventually processed either in subsequent loop iteration or when the encoder is flashed via:

/* flush the encoder */
encode(c, NULL, pkt, f);

I too tried to use VLC, and also QuickTime, with no luck.

Then I noticed ffplay tool in the ffmpeg's bin folder, that would play all videos produced by that example, with different codecs.

My point is - the issue might be with the viewer, not with the video file.

just change:

./encode_video.exe test.mp4 libx264 

to:

./encode_video.exe test.264 libx264 

and the result file "test.264" can be played by vlc player.

I'm running ffmpeg's encode_video.c demo on my Mac and Apple's QuickTime Player seems not supporting this format.

You want to use the muxing.c example instead.

The encode_video example doesn't produce an MPEG compliant file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM