Search code examples
iphoneavassetwriter

How to change video metadata using AVAssetWriter?


How to change a video(.mp4) meta-info using a AVAssetWriter API?

I want to not re-encode. only wanted to modify the video meta-info.

how to write a next code?

AVAssetWriter *writer = [AVAssetWriter assetWriterWithURL:[NSURL URLWithString:myPath] fileType:AVFileTypeQuickTimeMovie error:nil];

if I mistaken, give me some hint.

Thanks!!


Solution

  • refer a following code.

    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:[NSURL fileURLWithPath:"your path"] options:nil];
    NSMutableArray *metadata = [NSMutableArray array];
    AVMutableMetadataItem *metaItem = [AVMutableMetadataItem metadataItem];
    metaItem.key = AVMetadataCommonKeyPublisher;
    metaItem.keySpace = AVMetadataKeySpaceCommon;
    metaItem.value = @"your_value";
    [metadata addObject:metaItem];
    
    
    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetPassthrough];
    exportSession.outputURL = [NSURL fileURLWithPath:"your output path"];
    CMTime start = CMTimeMakeWithSeconds(0.0, BASIC_TIMESCALE);
    CMTimeRange range = CMTimeRangeMake(start, [asset duration]);
    exportSession.timeRange = range;
    exportSession.outputFileType = AVFileTypeAppleM4V // AVFileTypeMPEG4 or AVFileTypeQuickTimeMovie (video format);
    exportSession.metadata = metadata;
    exportSession.shouldOptimizeForNetworkUse = YES;
    [exportSession exportAsynchronouslyWithCompletionHandler:^{
         switch ([exportSession status]) 
         {
             case AVAssetExportSessionStatusCompleted:
                 NSLog(@"Export sucess");
             case AVAssetExportSessionStatusFailed:
                 NSLog(@"Export failed: %@", [[exportSession error] localizedDescription]);                
             case AVAssetExportSessionStatusCancelled:
                 NSLog(@"Export canceled");
                    default:
                        break;
                 }
             }];