The iPhone SDK provides a high level movie player, but so far there is no way to decode movies or extract frames. In this post I will show you how to do that using FFmpeg.
First, thanks to the FFmpeg project, Martin Böhme for his tutorial on using libavformat and libavcodec and David Conrad for the gas-preprocessor project that allows us to compile FFmpeg for the iPhone.
Before you link the FFmpeg libraries to your project, make sure that you understand FFmpeg’s licensing. My project uses the LGPL version of FFmpeg, which means that I have to make FFmpeg’s source code available (included in the project).
Before you create a commercial project that decodes H264 video, make sure that you get a license from MPEG LA.
The source code is posted on github: git://github.com/lajos/iFrameExtractor.git.
To compile and run the project on your iPhone:
- open Terminal
- clone the repository: git clone git://github.com/lajos/iFrameExtractor.git
- go to the ffmpeg folder in the project: cd iFrameExtractor/ffmpeg
- build the ffmpeg libraries: ./build_universal
- open the xcode project and run it on your iPhone device
The build_universal shell script creates libraries that work on both armv6 and armv7 processors. If you need libraries for pre-3Gs devices (iPhone 2g, iPhone3G, iPod Touch), use the build_armv6 script. For iPhone 3Gs only libraries, use build_armv7.
iFrameExtractorAppDelegate creates an instance of VideoFrameExtractor (the class that grabs images from the video). When you press the play button, iFrameExtractorAppDelegate sets up a loop and displays all the images from the video until it reaches the end of the movie.
The VideoFrameExtractor class is based on Martin Böhme’s tutorial, upgraded to use swscale. Initialize this class with the full path of the movie:
VideoFrameExtractor *video = [[VideoFrameExtractor alloc] initWithVideo:@"/full/path/to/movie.mov"];
By default, the output size will match the movie’s size. You can set outputWidth and outputHeight to change the dimensions:
video.outputWidth = 426; video.outputHeight = 320;
Use stepFrame to grab the next frame. This method returns false if there are no more frames (reached the end of video):
BOOL result = [video stepFrame];
The processed frame can be accessed as a UIImage on the currentImage property:
UIImage *myImage = video.currentImage;
You can seek to a specific time (in seconds) using the seekTime: method:
The movie’s length can be queried by checking the duration property:
NSLog(@"video duration: %f",video.duration);
This should get you started with extracting frames from iPhone movies If you find any problems, please submit a patch.
- Images are always decoded as landscape.