3

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://i.stack.imgur.com/OtxNY.jpg

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!!

NewbieCoder
  • 105
  • 9
  • I don't see any output files here. Are the output files empty? Partially correct (say, the first frame)? Utter garbage? Have you tried stepping through it with a debugger to see if it is taking the code paths you expect? If you run your executable through [dependencies](https://github.com/lucasg/Dependencies/), does it find the right libraries at the right places? – metal May 29 '20 at 13:17
  • Should I add an output file here? The output files are generated by the program but the player does not even play it. It played only when the format was .mp4 and codec was libx26., I saw some colored waves which is what should've appeared for other formats and codecs also. And also since it worked for that alone I am assuming its finding the libraries correctly. – NewbieCoder May 29 '20 at 14:08
  • It just looked like you were trying to embed some code or picture or something. What happens when you do those other debug steps in the failure cases vs. the success cases? – metal May 29 '20 at 14:10
  • Oh sorry I was just trying to add a separation to the paragraphs with those empty code blocks. Edited it now. – NewbieCoder May 29 '20 at 14:12
  • Hey I have included the necessary details. Can you please check it now? I used the dependencygui and also went over the debugger for this program and posted its output. @metal – NewbieCoder May 30 '20 at 06:14
  • My understanding is that you have a working example and a failing example. What is different when you step through each of these in the debugger? Where does one fail, while the other succeeds? Which of those two error codes is causing the failure? What does the documentation of `avcodec_receive_packet()` say could cause that to happen? – metal Jun 01 '20 at 14:38
  • In the answers to this question (https://stackoverflow.com/questions/53328946/cannot-play-video-output-of-libavcodec-ffmpeg-encoding-example) they suggest that "muxing" is required to produce the final file – Olivier Sohn Jan 02 '21 at 00:51
  • Does this answer your question? [Cannot Play Video Output of Libavcodec (ffmpeg) Encoding Example](https://stackoverflow.com/questions/53328946/cannot-play-video-output-of-libavcodec-ffmpeg-encoding-example) – Olivier Sohn Jan 02 '21 at 01:00

3 Answers3

1

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.

Vlad Feinstein
  • 10,960
  • 1
  • 12
  • 27
0

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.

0

You want to use the muxing.c example instead.

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

RusArtM
  • 1,116
  • 3
  • 15
  • 22