如何使用AVAssetWriter更改视频元数据?

时间:2012-07-16 13:33:30

标签: iphone avassetwriter

如何使用AVAssetWriter API更改视频(.mp4)元信息?

我想不重新编码。只想修改视频元信息。

如何编写下一个代码?

AVAssetWriter *writer = [AVAssetWriter assetWriterWithURL:[NSURL URLWithString:myPath] fileType:AVFileTypeQuickTimeMovie error:nil];
如果我弄错了,请给我一些提示。

谢谢!

1 个答案:

答案 0 :(得分:4)

参考以下代码。

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;
             }
         }];