I've scoured the Google/SO/Zeranoe results and tried to integrate everything I found into making my program generate videos correctly but I still can't get it to work right.
I needed a good video to use as a reference so I ran:
ffmpeg -t 5 -f dshow -i "video=Logitech HD Pro WebcamC910" CLI.mpg
Which generated a five-second video which can be played in VLC and shows the duration and plays correctly. You can get the video here: https://drive.google.com/file/d/0B1VGi1Ts7D7TR3c4VUlyM2dIcFk/edit?usp=sharing
I then generated a five-second video with my own code. I've tried to pare it down to only what I think is needed to record. You can find the code here: https://drive.google.com/file/d/0B1VGi1Ts7D7TMnFxSE1HX2FKbEU/edit?usp=sharing
It generated a video also, with similar video quality, but VLC doesn't show the video duration and the video seems to play too quickly. I mean that what I see on the screen looks like it's moving slightly too fast. You can find the video my code generated here: https://drive.google.com/file/d/0B1VGi1Ts7D7TSzFGUFZEMHJwQ0U/edit?usp=sharing
Even when you click on the links to the videos, you can see that Google is having trouble with mine but no problem with the one generated by the FFmpeg CLI. Sorry about posting everything off-site but I didn't want to spam my code into the post and I wanted to provide as much information as I could.
I've seen a number of posts about this but I can't seem to find a definitive solution.
EDIT: So I implemented your suggestions and I'm pretty sure that your answer fixed the timing issue but now I consistently get 20 non-strictly-monotonic errors before the first successful call encode_video. This happens whether I use
gFrame->pts = gFrameIndex;
or
gFrame->pts = av_rescale_q(gFrameIndex, gCodecContext->time_base, gStream->codec->time_base);
before
ret = avcodec_encode_video2(gCodecContext, &pkt, gFrame, &got_output);
if (ret < 0) {
fprintf(stderr, "Error encoding frame\n");
return false;
}
This seems to coincide with video artifacts at the start of the video file during playback. gFrameIndex starts at 1.
I believe my webcam auto-focuses at the start of recordings, is it possible this is related?
I've uploaded the generated .h264 file at https://drive.google.com/file/d/0B1VGi1Ts7D7TRjRzbzZZemRaRTA/edit?usp=sharing and my most recent code to https://drive.google.com/file/d/0B1VGi1Ts7D7TbmlyYncxYzRQUTA/edit?usp=sharing.
I really appreciate the help. Sadly, I can't use the FFmpeg CLI directly in my software, so I have to use the library. I'm going to have to keep trucking along with it unless you can suggest a better alternative.