[FFmpeg-user] Rendering video in reverse.

Jeff Sorrentino jeff at soren.com
Wed Dec 7 17:47:57 CET 2011

I have a short project where I would like to reverse video play.

while (av_read_frame(pFormatCtx, &packet) >= 0) {
if (packet.stream_index == videoStream) {
avcodec_decode_video(pCodecCtx, pFrame, &frameFinished, packet.data,
if (frameFinished) {
if (img_convert_ctx == NULL) {
img_convert_ctx =
sws_getContext(pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt,
textureWidth,textureHeight, textureFormat, SWS_FAST_BILINEAR, NULL, NULL,
if(img_convert_ctx == NULL) {
// FREE PACKET? av_free_packet(&packet);
sws_scale(img_convert_ctx, pFrame->data, pFrame->linesize, 0,
pCodecCtx->height, pFrameConverted->data, pFrameConverted->linesize );
av_seek_frame(pFormatCtx, videoStream, 0, AVSEEK_FLAG_ANY);

In the above example, the methodology to do this is pretty simple, *but* I
did not find functions that would allow me to properly gather the data and
manipulate the current frame as need be.

Meaning, av_seek_frame(...) takes a timestamp as an argument for where to
seek in TIME, but I would (or so  i think) need to seek to the last frame
(last line above).

If av_seek_frame can do a SEEK TO LAST FRAME, then in the initial loop i
can call av_read_frame followed by an av_seek_frame ( CurrentFrame - 1 )
(or CurrentFrame - 2 taking into account the read frame).

1. How do I get the total number of frames from the video?
2. How do I incorporate the av_seek_frame to seek based on frame and not
3. OR, is there an av_read_frame_NUMBER() function available?

Thank You!

More information about the ffmpeg-user mailing list