6

I am attempting to use AVFoundation to record video on OS X but it waits till then end of the recording to save the file. I want to be able to have it save whatever it has captured every 5/10/X seconds.

I need to do this so that as it saves off the video files I can stream the segments to a server while the video is still recording so I can server up "almost live" video off the server.

Thanks for any help you may be able to provide!

JoshStrange
  • 1,121
  • 1
  • 7
  • 22
  • It is actually saving it to a temporary directory. It needs to do AV sync on the file. If you only have one media track then it will save the file immediately. Also keep in mind that with a MOV/MP4 the header to the file is written last. – Steve McFarlin Jul 13 '12 at 19:02
  • @SteveMcFarlin So there is no simple way to tell AVFoundation when recording to split the video into smaller segments as it goes? – JoshStrange Jul 16 '12 at 14:02
  • 1
    No there is not. AVFoundation will only record to a MP4/MOV. There are two types of MOV files. A standard MOV which is for the most part identical to a MP4 file. Then there is a 'streaming' quicktime format. This essentially creates a series of MOV files within a single MOV file. It does this by using 'fragment' atoms. For instance moof which is a movie fragment. There will also be a series of mdat atoms containing the media. You may be able to parse this file in realtime and create a segmented HLS stream. Take a look at movieFragmentInterval in AVAssetWriter. – Steve McFarlin Jul 16 '12 at 17:56
  • Also, if you will only support OS 10.8 then take a look at VideoToolkit which is now public. Combining this with CoreAudio and AudioToolkit you could create H.264/AAC streams to be used with some chunking code. Additionally this will be hardware based if supported by the system. – Steve McFarlin Jul 16 '12 at 18:13
  • @SteveMcFarlin I'm trying to get the file to save immidiatly but it remains on zero bytes. It has a single video track, no audio & movieFragmentInterval = CMTimeMake(1.0, 1000000000). Am i missing something? Thanks! – Avishay Cohen Jul 25 '12 at 16:45
  • Try not setting movieFragmentInterval. I know with just a single video track it will write to the file immediately. – Steve McFarlin Jul 27 '12 at 18:09
  • @SteveMcFarlin i want to create multiple segment files while its recording through camera , is it possible to do this using "movieFragmentInterval" in avassetwritter ? and to read time from each framented data – – Mr.G Apr 21 '15 at 09:49
  • @Mr.G You may want to post a new question explaining exactly what you want to do. – Steve McFarlin May 12 '15 at 21:10
  • @SteveMcFarlin i did that using ffmpeg, thank you – Mr.G May 18 '15 at 06:13

1 Answers1

2

You can ask AVFoundation to vend the frames to you as it is recording, then just pass a set number of frames to the server. But what you are asking about is extremely complicated to get right, since bandwidth or temporary network blips can cause delays/skips, etc.

We had a discussion about doing this exact thing over here, by using AVCaptureSession with AVCaptureVideoDataOutput to vend the MPEG frames: Near Real Time Video Upload from iPhone

If you want the individual chunks to be playable, you'll have to wrap them in a MOV or MPEG container, but it doesn't sound like you need that in your scenario.

Community
  • 1
  • 1
russbishop
  • 16,587
  • 7
  • 61
  • 74
  • how can i create individual chunks ??? does it automatically created using avassetwritter's movieFragmentInterval method ?? – Mr.G Apr 21 '15 at 05:33
  • i want to read timestamp from each fragment . how can i do that ? – Mr.G Apr 21 '15 at 09:51