Temp file used in AVAssetWriter/AVAssetWriterInput

I use AVAssetWriter and AVAssetWriterInput to capture the input (CMSampleBuffer) to a target local file. It seems that it always writes to a temporary file first and write to the actual target file after finishWriting() is called. Is there a way to write to target file directly instead of to a temporary file. It is not using multi-pass now.


Now, it causes a problem if the app crashes, and the recorded media can't be recovered. We like to at least recover the media.


Thanks

Accepted Reply

I recently noticed that (on macOS) setting

shouldOptimizeForNetworkUse = YES
on the
AVAssetWriter
instance triggers the behaviour you are describing: Media sample data is written to temporary file (in the same location, with some random characters added to the file name), and only in
finishWriting
the final file is written, presumably by first writing the movie header and then appending the sample data from the temporary file (which would explain why the run time of
finishWriting
depends on the size of the video data).

Without

shouldOptimizeForNetworkUse
, no temporary file is created and
finishWriting
returns in constant time.


Even if

shouldOptimizeForNetworkUse
is not involved in your case, there may be other properties that trigger this behaviour. Judging from the documentation,
movieFragmentInterval
may be a candidate that is relevant for your setup.

Replies

finishWriting() will trigger the header of your file to be written so your file isn't readable before that anyway... (unless you're using segmented mp4)

It seems what trigger by finishWriting() is more than the header. The header generally shall be small. However, the real situation is, the larger the video being recorded, the longer the wait time to the callback is necessary.

I recently noticed that (on macOS) setting

shouldOptimizeForNetworkUse = YES
on the
AVAssetWriter
instance triggers the behaviour you are describing: Media sample data is written to temporary file (in the same location, with some random characters added to the file name), and only in
finishWriting
the final file is written, presumably by first writing the movie header and then appending the sample data from the temporary file (which would explain why the run time of
finishWriting
depends on the size of the video data).

Without

shouldOptimizeForNetworkUse
, no temporary file is created and
finishWriting
returns in constant time.


Even if

shouldOptimizeForNetworkUse
is not involved in your case, there may be other properties that trigger this behaviour. Judging from the documentation,
movieFragmentInterval
may be a candidate that is relevant for your setup.