Data corruption when reading realtime H.264 output from AVAssetWriter

前端 未结 2 1691
耶瑟儿~
耶瑟儿~ 2020-12-23 08:12

I\'m using some tricks to try to read the raw output of an AVAssetWriter while it is being written to disk. When I reassemble the individual files by concatenating them, the

相关标签:
2条回答
  • 2020-12-23 08:52

    I ended up abandoning the "read while it's written" approach in favor of a manual chunking approach where I call finishWriting every 5 seconds on a background thread. I was able to drop a negligible number of frames using a method originally described here:

    - (void) segmentRecording:(NSTimer*)timer {
        AVAssetWriter *tempAssetWriter = self.assetWriter;
        AVAssetWriterInput *tempAudioEncoder = self.audioEncoder;
        AVAssetWriterInput *tempVideoEncoder = self.videoEncoder;
        self.assetWriter = queuedAssetWriter;
        self.audioEncoder = queuedAudioEncoder;
        self.videoEncoder = queuedVideoEncoder;
        //NSLog(@"Switching encoders");
    
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
            [tempAudioEncoder markAsFinished];
            [tempVideoEncoder markAsFinished];
            if (tempAssetWriter.status == AVAssetWriterStatusWriting) {
                if(![tempAssetWriter finishWriting]) {
                    [self showError:[tempAssetWriter error]];
                }
            }
            if (self.readyToRecordAudio && self.readyToRecordVideo) {
                NSError *error = nil;
                self.queuedAssetWriter = [[AVAssetWriter alloc] initWithURL:[self newMovieURL] fileType:(NSString *)kUTTypeMPEG4 error:&error];
                if (error) {
                    [self showError:error];
                }
                self.queuedVideoEncoder = [self setupVideoEncoderWithAssetWriter:self.queuedAssetWriter formatDescription:videoFormatDescription bitsPerSecond:videoBPS];
                self.queuedAudioEncoder = [self setupAudioEncoderWithAssetWriter:self.queuedAssetWriter formatDescription:audioFormatDescription bitsPerSecond:audioBPS];
                //NSLog(@"Encoder switch finished");
    
            }
        });
    }
    

    Full source code: https://github.com/chrisballinger/FFmpeg-iOS-Encoder/blob/master/AVSegmentingAppleEncoder.m

    0 讨论(0)
  • 2020-12-23 09:08

    When reading a MOV file that is ACTIVELY recording on iOS, you MUST check the 4 bytes mentioned for changes, and re-write this four bytes, then check for additional data in file, and send additional data. Then when done, truncate the file to the file size written.

    Obviously this depends on where you are sending the file. I use a send (offset,number of bytes) to receiver. So I send "additional data", "more additional data", ... , new data at (24,4), "more additional data".

    Typically iOS only writes the 4 byte (size of data section) record when file is about to be closed (aka after last media write). (see info on "Quicktime atoms"). Unfortunately, this also means the MOV file is not PLAYABLE until recording is completed (and movie descriptors written at END of file).

    0 讨论(0)
提交回复
热议问题