2

I have a class to record video and audio into .mp4 file. I use AVAssetWriter, AVAssetWriterInput to do such task

How can I get duration of a video while it's being recorded with such method (AVAssetWriter)?

user924
  • 8,146
  • 7
  • 57
  • 139

1 Answers1

4

You can save start time when you begin startSession

let startTimeStamp = CMSampleBufferGetPresentationTimeStamp(sample)
fileWriter.startSession(atSourceTime: startTimeStamp)
startTime = Double(startTimeStamp.value) / Double(startTimeStamp.timescale)

than every buffer you will get do this:

let currentTimeStamp = CMSampleBufferGetPresentationTimeStamp(sample)
let currentTime = Double(currentTimeStamp.value) / Double(currentTimeStamp.timescale)

print("Duration - \(currentTime - startTime)")
Jabson
  • 1,585
  • 1
  • 12
  • 16
  • what if I need to pause recording for example for 30 seconds? then it should not take into account these 30 seconds – user924 Mar 30 '18 at 08:41
  • Can you provide some sample code how are you going to pause AVAssetWriter? – Jabson Mar 30 '18 at 08:46
  • I don't need to pause this writer (what do you mean?) I just won't send any buffers (audio or video) while some variable isPaused (bool) is set as true – user924 Mar 30 '18 at 09:00
  • You can't pause writing such way. i think you will get black video while your boolean is false (isPaused = false) – Jabson Mar 30 '18 at 09:06
  • I don't think so, anyway I already limit how fast I append buffers because I guess `didOutputSampleBuffer` sends them at very high fps (>= 30) – user924 Mar 30 '18 at 09:11
  • Ok, try it. Anyway. You can save every last buffer you appended. when you hit pause you can detect duration from startTime to saved Buffer time. resume continue same algorithm from first appended buffer. I have not implemented this... it's only idea... – Jabson Mar 30 '18 at 09:18