Thursday, May 14, 2015

FFMPEG and 60 FPS

I have been using FFMPEG in my script to encode Playblast files from Maya without problem for a few years. The first time my studio used 60FPS however it started producing some weird results. Broken frames were added to the end of our compressed files.

Let's say a Playblast was 80 frames long. Frame 1-80 every thing was normal, then weird frames would be added to frame 81 onwards all the way to frame 157 or so.

Broken frames that are being added.
After a bit of confusion, I realized the solution was very simple. FFMPEG already offers a flag to specify the output frame amount, -vframes. The amount of frame can be generated with some Maya queries.
ffmpeg -y -i "uncompressed.avi" -vcodec libx264 -vframes 80 -crf 22 -bf 0 "output.mp4"
For the entire playback range:
startframe = pm.playbackOptions(ast=1, q=1)
endframe = pm.playbackOptions(aet=1, q=1)
If you have hilighted the time line, its frame can be queried like this.
slider = pm.mel.eval('$tmpVar=$gPlayBackSlider')
startframe, endframe = pm.timeControl(slider,q=True,rangeArray=True) 

Simply subtrace startframe from endframe and bam, we get the correct frame number.