I use AVAssetWriter to record video to file. So I create class for this purpose. link to gist
and then somewhere in project I push record and start record video.
func start() {
assetWriter?.startWriting()
assetWriter?.startSession(atSourceTime: kCMTimeZero)
}
All works fine if I record video only once, but when I stop record
func finish() {
print("before finish")
assetWriter?.finishWriting {
print("finish writing")
}
}
and then try to record another video I get exception
[AVAssetWriter startWriting] Cannot call method when status is 2
How can I configure AVAssertWriter to record multiples videos
It's not clear whether you want to create multiple video files or append multiple video sequences to a single file. If you want multiple files, recreate your AVAssetWriter
and friends (i.e. your AssertController
class).
If you want multiple sequences in your file, then know that AVAssetWriter
doesn't support stopping and starting, so the solution is to not stop. You control what frames to append and when on the session timeline, so simply do not append frames when you are "stopped".
Your gist uses frame numbers to calculate buffer presentation timestamps, so there's not much else to do, however if you were working with image sample buffers, then they already have their own presentation timestamps, you would need to adjust them to work with your session timeline.