Transcoding in C with FFmpeg

Google doesn’t provide much information on transcoding video in C with FFmpeg – there are no examples anywhere of how to specifically do this. However, with a significant amount of determination and perseverance, I have successfully produced a system capable of transcoding a video file. It might have taken a day and a half to have it perfected, but I now feel that I fully understand aspects of video encoding and FFmpeg that I did not understand before.

In my tests, I have successfully transcoded an MP4 file using H.264/AAC to another MP4 file using the same codecs. The emphasis for this transcoding system was to produce something capable of parsing the individual audio and video frames, thus allowing manipulation of this data at a later stage. The work I have done thus far has hopefully set the foundations for data manipulation to follow shortly. Given this progress, I remain cautiously optimistic that I will be able to adapt what I have done to embed data into video.

Over the last couple of days, I have had to deal with the issues associated with processing multiplexed audio and video data. At first, this didn’t seem too challenging. All I had to do was process a packet correctly as either audio or video and then successfully interleave it in the output stream. There were a couple of nuances that did crop up, but most significantly was the problem of the PTS (Presentation Time Stamp) of each video frame. When an incorrect PTS was defined, the video frame would not interleave, or the playback of the video would be hideously affected. In essence, PTS is used to synchronise separate streams. In the instances that the video would playback, the PTS would affect the stream to the point where the image was static or playing at a rate that was significantly more rapid than the audio playback.

After correcting the PTS, dealing with frame noise produced by the decoder and managing frame rate issues, I ended up with my current solution, which thankfully works!