How to change video metadata using AVAssetWriter?

How to change meta information of video (.mp4) using API AVAssetWriter?

I do not want to recode. just wanted to change the meta information of the video.

how to write the following code?

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

if I am wrong, give me some hint.

Thank!!

+3
source share
1 answer

enter the 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;
             }
         }];
+3
source

Source: https://habr.com/ru/post/1679128/


All Articles