Отсутствует звук при получении примеров буферов через AVAssetReaderTrackOutput в iOS?

Вы можете использовать jQuery и что-то вроде этого:

$("#id").html($("#id option").sort(function (a, b) {
    return a.text == b.text ? 0 : a.text < b.text ? -1 : 1
}))

Но, вероятно, лучше спросить, почему и чего вы пытаетесь выполнить.

1
задан G. Hazarath Reddy 19 January 2019 в 09:37
поделиться

1 ответ

Пропущено добавление аудиофайлов в audioAssetWriterInput. Я исправил эту проблему, добавив буферы аудиосэмплов.

Здесь я добавляю код для получения аудио, буферы семплов видео из существующего видео, затем пишу и сохраняю в локальные документы. Вы можете применять фильтры и визуализировать изображения поверх изображения для требуемых кадров и общего количества кадров в определенной области кадра.

    NSError *error;

    AVURLAsset *videoAsset = [AVURLAsset URLAssetWithURL:videoURL options:nil];;
    AVAssetReader *assetReader = [[AVAssetReader alloc] initWithAsset:videoAsset error:nil];
    AVAssetTrack *videoTrack = [[videoAsset tracksWithMediaType:AVMediaTypeVideo] firstObject];
    AVAssetTrack *audioTrack = [[videoAsset tracksWithMediaType:AVMediaTypeAudio] firstObject];

    NSDictionary *videoReaderOutputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                               [NSNumber numberWithInt:kCVPixelFormatType_420YpCbCr8BiPlanarFullRange], kCVPixelBufferPixelFormatTypeKey, nil];

    AVAssetReaderTrackOutput* assetReaderVideoTrackOutput = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:videoTrack outputSettings:videoReaderOutputSettings];

    AudioChannelLayout acl;
    bzero( &acl, sizeof(acl));
    acl.mChannelLayoutTag = kAudioChannelLayoutTag_Mono;

    NSDictionary* audioOutputSettings  = [NSDictionary dictionaryWithObjectsAndKeys:
                                          [ NSNumber numberWithInt: kAudioFormatMPEG4AAC], AVFormatIDKey,
                                          [ NSNumber numberWithInt: 1 ], AVNumberOfChannelsKey,
                                          [ NSNumber numberWithFloat: 44100.0 ], AVSampleRateKey,
                                          [ NSData dataWithBytes: &acl length: sizeof( acl ) ], AVChannelLayoutKey,
                                          [ NSNumber numberWithInt: 64000 ], AVEncoderBitRateKey,
                                          nil];

    NSDictionary  *audioDecodesettings = @{ AVFormatIDKey : [NSNumber numberWithInt:kAudioFormatLinearPCM] };

    AVAssetReaderTrackOutput *assetReaderAudioTrackOutput  = [AVAssetReaderTrackOutput assetReaderTrackOutputWithTrack:audioTrack outputSettings:audioDecodesettings];

    [assetReader addOutput:assetReaderVideoTrackOutput];
    [assetReader addOutput:assetReaderAudioTrackOutput];
    [assetReader startReading];


    NSMutableArray *samples = [[NSMutableArray alloc] init];

    CMSampleBufferRef sample;
    while((sample = [assetReaderVideoTrackOutput copyNextSampleBuffer])) {
        [samples addObject:(__bridge id)sample];
        CFRelease(sample);
    }

    NSString *outputPath = [self getDocumentsUrlForFilterMovie];
    NSURL *outputURL = [NSURL fileURLWithPath:outputPath];

    AVAssetWriter *assetWriter = [[AVAssetWriter alloc] initWithURL:outputURL
                                                           fileType:AVFileTypeQuickTimeMovie
                                                              error:&error];


    NSDictionary *videoCompressionProps = [NSDictionary dictionaryWithObjectsAndKeys:
                                           @(videoTrack.estimatedDataRate), AVVideoAverageBitRateKey,
                                           nil];

    NSDictionary *writerOutputSettings = [NSDictionary dictionaryWithObjectsAndKeys:
                                          AVVideoCodecH264, AVVideoCodecKey,
                                          [NSNumber numberWithInt:videoTrack.naturalSize.width], AVVideoWidthKey,
                                          [NSNumber numberWithInt:videoTrack.naturalSize.height], AVVideoHeightKey,
                                          videoCompressionProps, AVVideoCompressionPropertiesKey,
                                          nil];

    AVAssetWriterInput *videoWriterInput = [[AVAssetWriterInput alloc] initWithMediaType:AVMediaTypeVideo
                                                                          outputSettings:writerOutputSettings
                                                                        sourceFormatHint:(__bridge CMFormatDescriptionRef)[videoTrack.formatDescriptions lastObject]];

    [videoWriterInput setExpectsMediaDataInRealTime:NO];
    [assetWriter addInput:videoWriterInput];

    AVAssetWriterInput *audioWriterInput  = [AVAssetWriterInput assetWriterInputWithMediaType:AVMediaTypeAudio outputSettings:audioOutputSettings];
    audioWriterInput.expectsMediaDataInRealTime = YES;
    if([assetWriter canAddInput:audioWriterInput]) {
        [assetWriter addInput:audioWriterInput];
    }

    AVAssetWriterInputPixelBufferAdaptor *pixelBufferAdaptor = [[AVAssetWriterInputPixelBufferAdaptor alloc] initWithAssetWriterInput:videoWriterInput sourcePixelBufferAttributes:nil];

    [assetWriter startWriting];
    [assetWriter startSessionAtSourceTime:CMSampleBufferGetPresentationTimeStamp((__bridge CMSampleBufferRef)samples[0])];

    while((sample = [assetReaderAudioTrackOutput copyNextSampleBuffer])) {
        [audioWriterInput appendSampleBuffer:sample];
        while (!audioWriterInput.readyForMoreMediaData) {
            [NSThread sleepForTimeInterval:0.1];
        }
        CFRelease(sample);
    }


    for(NSInteger i = 0; i < samples.count; i++) {

        CMTime presentationTime = CMSampleBufferGetPresentationTimeStamp((__bridge CMSampleBufferRef)samples[i]);

        CVPixelBufferRef videoFrameBuffer = nil;

        if(frameRenderType == KVideoNormal) {
            videoFrameBuffer = CMSampleBufferGetImageBuffer((__bridge CMSampleBufferRef)samples[i]);
        } else if (frameRenderType == KVideoReverse) {
            videoFrameBuffer = CMSampleBufferGetImageBuffer((__bridge CMSampleBufferRef)samples[samples.count - i - 1]);
        }
        if(self.filters.count > 0) {
            CIImage *frameImage = [CIImage imageWithCVPixelBuffer:videoFrameBuffer];

            for(CIFilter *filter in self.filters) {

                [filter setValue:frameImage forKey:kCIInputImageKey];
                frameImage = filter.outputImage;

            }

            [self->ciContext render:frameImage toCVPixelBuffer:videoFrameBuffer bounds:frameImage.extent colorSpace:self->colorSpace];
        }
        while (!videoWriterInput.readyForMoreMediaData) {
            [NSThread sleepForTimeInterval:0.1];
        }

        [pixelBufferAdaptor appendPixelBuffer:videoFrameBuffer withPresentationTime:presentationTime];

    }

    [videoWriterInput markAsFinished];
    [assetWriter finishWritingWithCompletionHandler:^(){
        dispatch_async(dispatch_get_main_queue(), ^{
            NSLog(@"Finished video processing");
        });
    }];
});
0
ответ дан G. Hazarath Reddy 19 January 2019 в 09:37
поделиться
Другие вопросы по тегам:

Похожие вопросы: