Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wrap an h.264 file as mp4 on iOS

I have a bare h.264 file (from a raspberry pi camera), and I'd like to wrap it as an mp4. I don't need to play it, edit it, add or remove anything, or access the pixels.

Lots of people have asked about compiling ffmpeg for iOS, or streaming live data. But given the lack of easy translation between the ffmpeg command line and its iOS build, it's very difficult for me to figure out how to implement this simple command:

ffmpeg -i input.h264 -vcodec copy out.mp4

I don't specifically care whether this happens via ffmpeg, avconv, or AVFoundation (or something else). It just seems like it should be not-this-hard to do on a device.

like image 797
buildsucceeded Avatar asked Oct 31 '22 05:10

buildsucceeded


1 Answers

It is not hard but requires some work and attention to detail. Here is my best guess:

  1. read PPS/SPS from your input.h264
  2. extract height & width from SPS
  3. generate avcC header from PPS/SPS
  4. create an AVAssetWriter with file type AVFileTypeQuickTimeMovie
  5. create an AVAssetWriterInput
  6. add the AVAssetWriterInput as AVMediaTypeVideo with your height & width to the AVAssetWriter
  7. read from your input.h264 (likely in Annex B format) one NALs at a time
  8. convert your NALs from your input.h264 from start code prefixed (0 0 1; Annex B) to size prefixed (mp4 format)
  9. drop NALs of type AU, PPS, SPS
  10. create a CMSampleBuffer for each NAL and add a CMFormatDescription with the avcC header
  11. regenerate timestamps starting a zero using the known frame rate (watch out if your frames are reordered)
  12. append your CMSampleBuffer to your AVAssetWriterInput
  13. goto 7 until EOF
like image 129
Markus Schumann Avatar answered Nov 14 '22 19:11

Markus Schumann