AVFoundation - Reverse an AVAsset and output video file

早过忘川 提交于 2019-12-03 04:16:14

问题


I've seen this question asked a few times, but none of them seem to have any working answers.

The requirement is to reverse and output a video file (not just play it in reverse) keeping the same compression, format, and frame rate as the source video.

Ideally, the solution would be able to do this all in memory or buffer and avoid generating the frames into image files (for ex: using AVAssetImageGenerator) and then recompiling it (resource intensive, unreliable timing results, changes in frame/image quality from original, etc.).

--

My contribution: This is still not working, but the best I've tried so far:

  • Read in the sample frames into an array of CMSampleBufferRef[] using AVAssetReader.
  • Write it back in reverse order using AVAssetWriter.
  • Problem: Seems like timing for each frame is saved in the CMSampleBufferRef so even appending them backwards will not work.
  • Next, I tried swapping the timing information of each frame with reverse/mirror frame.
  • Problem: This causes an unknown error with AVAssetWriter.
  • Next Step: I'm going to look into AVAssetWriterInputPixelBufferAdaptor

    - (AVAsset *)assetByReversingAsset:(AVAsset *)asset {
        NSURL *tmpFileURL = [NSURL URLWithString:@"/tmp/test.mp4"];    
        NSError *error;
    
        // initialize the AVAssetReader that will read the input asset track
        AVAssetReader *reader = [[AVAssetReader alloc] initWithAsset:asset error:&error];
        AVAssetTrack *videoTrack = [[asset tracksWithMediaType:AVMediaTypeVideo] lastObject];
    
        AVAssetReaderTrackOutput* readerOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:videoTrack outputSettings:nil];
        [reader addOutput:readerOutput];
        [reader startReading];
    
        // Read in the samples into an array
        NSMutableArray *samples = [[NSMutableArray alloc] init];
    
        while(1) {
            CMSampleBufferRef sample = [readerOutput copyNextSampleBuffer];
    
            if (sample == NULL) {
                break;
            }
    
            [samples addObject:(__bridge id)sample];
            CFRelease(sample);
        }
    
        // initialize the the writer that will save to our temporary file.
        CMFormatDescriptionRef formatDescription = CFBridgingRetain([videoTrack.formatDescriptions lastObject]);
        AVAssetWriterInput *writerInput = [[AVAssetWriterInput alloc] initWithMediaType:AVMediaTypeVideo outputSettings:nil sourceFormatHint:formatDescription];
        CFRelease(formatDescription);
    
        AVAssetWriter *writer = [[AVAssetWriter alloc] initWithURL:tmpFileURL
                                                          fileType:AVFileTypeMPEG4
                                                             error:&error];
        [writerInput setExpectsMediaDataInRealTime:NO];
        [writer addInput:writerInput];
        [writer startSessionAtSourceTime:CMSampleBufferGetPresentationTimeStamp((__bridge CMSampleBufferRef)samples[0])];
        [writer startWriting];
    
    
        // Traverse the sample frames in reverse order
        for(NSInteger i = samples.count-1; i >= 0; i--) {
            CMSampleBufferRef sample = (__bridge CMSampleBufferRef)samples[i];
    
            // Since the timing information is built into the CMSampleBufferRef 
            // We will need to make a copy of it with new timing info. Will copy
            // the timing data from the mirror frame at samples[samples.count - i -1]
    
            CMItemCount numSampleTimingEntries;
            CMSampleBufferGetSampleTimingInfoArray((__bridge CMSampleBufferRef)samples[samples.count - i -1], 0, nil, &numSampleTimingEntries);
            CMSampleTimingInfo *timingInfo = malloc(sizeof(CMSampleTimingInfo) * numSampleTimingEntries);
            CMSampleBufferGetSampleTimingInfoArray((__bridge CMSampleBufferRef)sample, numSampleTimingEntries, timingInfo, &numSampleTimingEntries);
    
            CMSampleBufferRef sampleWithCorrectTiming;
            CMSampleBufferCreateCopyWithNewTiming(
                                                  kCFAllocatorDefault,
                                                  sample,
                                                  numSampleTimingEntries,
                                                  timingInfo,
                                                  &sampleWithCorrectTiming);
    
            if (writerInput.readyForMoreMediaData)  {
                [writerInput appendSampleBuffer:sampleWithCorrectTiming];
            }
    
            CFRelease(sampleWithCorrectTiming);
            free(timingInfo);
        }
    
        [writer finishWriting];
    
        return [AVAsset assetWithURL:tmpFileURL];
    }
    

回答1:


Worked on this over the last few days and was able to get it working.

Source code here: http://www.andyhin.com/post/5/reverse-video-avfoundation

Uses AVAssetReader to read out the samples/frames, extracts the image/pixel buffer, and then appends it with the presentation time of the mirror frame.



来源:https://stackoverflow.com/questions/30169120/avfoundation-reverse-an-avasset-and-output-video-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!