AVFoundation – AVMetadataItem get media attribute metadata

catalogue

Zero basis Object-C learning route recommendation: Object-C learning directory >> Object-C Foundation

Zero basis Object-C learning route recommendation: Object-C learning directory >> Object-C thread

Zero basis Object-C learning route recommendation: Object-C learning directory >> OpenGL ES

Zero basis Object-C learning route recommendation: Object-C learning directory >> GPUImage

Zero basis Object-C learning route recommendation: Object-C learning directory >> AVFoundation

Zero basis Object-C learning route recommendation: Object-C learning directory >> CocoaPods

1, Foreword

1.AVAsset

Assets It can come from a file or a user's album, which can be understood as a multimedia resource. The URL is used as the identification of an asset object. This URL can be a local file path or network stream;

2.AVAssetTrack

AVAsset contains many tracks** AVAssetTrack **For example, audio, video, text, closed captions, subtitles

3.AVComposition / AVMutableComposition

**Use AVMutableComposition Class can add or delete an AVAsset to gather a single or multiple avassets together to synthesize a new video** In addition, AVMutableAudioMix and AVMutableVideoComposition Class to coordinate and manage the resources;

4.AVMutableVideoComposition

AVFoundation The core class in the class API is avvideoposition / avmutablevideoposition.

Avvideoposition / avmutablevideoposition gives an overall description of the method of combining two or more video tracks. It consists of a set of time frames and introductions that describe the combination behavior. This information appears at any point in time within the combined resources.

AVVideoComposition /Avmutablevideoposition manages all video tracks and can determine the final video size. Clipping needs to be done here;

5.AVMutableCompositionTrack

Gather multiple avassets together to synthesize the track information in the new video, including audio track, video track, etc., in which various corresponding materials (picture in picture, watermark, etc.) can be inserted;

6.AVMutableVideoCompositionLayerInstruction

AVMutableVideoCompositionLayerInstruction is mainly used to process scaling, blurring, clipping, rotation, etc. of a video in the video track;

7.AVMutableVideoCompositionInstruction

Represents an instruction that determines the state of each track in a timeRange. Each instruction contains multiple avmutablevideocompositionlayerinstructions; Avvideoposition is composed of multiple avvideoposition instructions;

The key data provided by avvideoposition instruction is the time range information in the time axis of the composite object. This time range is the time range when a combination appears. The set of attributes to execute is defined through its AVMutableVideoCompositionLayerInstruction collection.

8.AVAssetExportSession

AVAssetExportSession It is mainly used to export video;

2, Introduction to AVMetadataItem

AVAsset and AVAssetTrack The function of querying related metadata can be realized. Generally, the metadata provided by AVAsset is used. When it involves obtaining track level metadata, AVAssetTrack will be used. The related metadata obtained by AVAsset is as follows:

/*  Contains metadata of current video common format types     */
@property (nonatomic, readonly) NSArray<AVMetadataItem *> *commonMetadata;

//  Contains metadata for all format types of the current video
@property (nonatomic, readonly) NSArray<AVMetadataItem *> *metadata API_AVAILABLE(macos(10.10), ios(8.0), tvos(9.0), watchos(1.0));

//  Contains the format types of all available metadata for the current video. The format types of metadata are defined in AVMetadataFormat, including title, creator, subject, publisher, etc
@property (nonatomic, readonly) NSArray<AVMetadataFormat> *availableMetadataFormats;

AVMetadataFormat is as follows:

// Metadata common keys

AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyTitle ;

AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyCreator;

AVF_EXPORT AVMetadataKey const AVMetadataCommonKeySubject;

AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyDescription;

AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyPublisher;

AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyContributor;

AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyCreationDate;

AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyLastModifiedDate;

AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyType;

AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyFormat;

AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyIdentifier;

AVF_EXPORT AVMetadataKey const AVMetadataCommonKeySource;

AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyLanguage;

AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyRelation;

AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyLocation;

AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyCopyrights;

AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyAlbumName;

AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyAuthor;

AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyArtist;

AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyArtwork;

AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyMake;

AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyModel ;

AVF_EXPORT AVMetadataKey const AVMetadataCommonKeySoftware ;

AVF_EXPORT AVMetadataKey const AVMetadataCommonKeyAccessibilityDescription;

3, Get metadata using availableMetadataFormats

/******************************************************************************************/
//@Author: ape programming
//@Blog (personal blog address): www.codersrc.com
//@File:C language tutorial - AVMetadataItem get media attribute metadata
//@Time:2021/07/29 07:30
//@Motto: no small steps can lead to thousands of miles. No small streams can lead to rivers and seas. The highlights of program life need to be accumulated unremittingly!
/******************************************************************************************/


NSURL* url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"456.mp3" ofType:nil]];
    
AVURLAsset* asset = [[AVURLAsset alloc] initWithURL:url options:nil];
    
for (AVMetadataFormat item in [asset availableMetadataFormats]) {
    NSArray *medata = [asset metadataForFormat:item];
    for (AVMetadataItem *mitem in medata) {
        NSLog(@"%@:%@",mitem.key,mitem.value);
    }
}


/*
2021-07-24 23:22:28.863338+0800 LearnAVFoundation[7405:497502] TXXX:3923013_396752
2021-07-24 23:22:28.863393+0800 LearnAVFoundation[7405:497502] TCON:3923013_396752
2021-07-24 23:22:28.863429+0800 LearnAVFoundation[7405:497502] TSSE:Lavf56.4.101
2021-07-24 23:22:28.863464+0800 LearnAVFoundation[7405:497502] TPOS:1
2021-07-24 23:22:28.863499+0800 LearnAVFoundation[7405:497502] TRCK:12
2021-07-24 23:22:28.863529+0800 LearnAVFoundation[7405:497502] TPE1:Thia Megia
2021-07-24 23:22:28.863574+0800 LearnAVFoundation[7405:497502] APIC:{length = 665140, bytes = 0x89504e47 0d0a1a0a 0000000d 49484452 ... 49454e44 ae426082 }
2021-07-24 23:22:28.863606+0800 LearnAVFoundation[7405:497502] COMM:163 key(Don't modify):L64FU3W4YxX3ZFTmbZ+8/cmlcJixcHCmHiX1THVPt/hhwCr8IK4+AdU6ba2JAq6yLF2az6pftnczEio2AkbRn47o8pfzLEbtKa3vNxCKaBpQujCz48iUDx0+FASY522oYNHmaeewFYBc+xLPSfsyaUgHaQaa/ojLxzzWUVv50Mg/I2IoOKyxPTUquetcGKXGgEofzvAcSlem6yEo7nkM9Nkmbhv7/ne7NlU5GvpG8nOvoBYZiG5otuvxV8pc7ti7ATYNINAs3qHiA2LTuJ9l6CxYtCboFlRr29BVyJSH1eMjIA9m04AEobEgVsuiyG+VXTNeh8imOV8WG268fh5dRxD0EEYDscUALQfptmS3TWSoyM7LF6ASZYn7aBZNkheSYNjgijkkEAt31UpVbNbLjW+HyJwvJyJ/Tls9A9h0PFfmIbeSTrjenxpMz22eRkw/WM3snECea8IhyNFe71F0VXmiZFi5/A6aiBv2EJrHFDKQWD1ktANXSTiTcjmLIEIb7b8cQdI8quSJAZ02VcS4zA==
2021-07-24 23:22:28.863647+0800 LearnAVFoundation[7405:497502] TIT2:Colors Of The Wind
2021-07-24 23:22:28.863676+0800 LearnAVFoundation[7405:497502] TALB:American Idol Top 12 Season 10
*/

4, Get metadata using metadata

/******************************************************************************************/
//@Author: ape programming
//@Blog (personal blog address): www.codersrc.com
//@File:C language tutorial - AVMetadataItem get media attribute metadata
//@Time:2021/07/29 07:30
//@Motto: no small steps can lead to thousands of miles. No small streams can lead to rivers and seas. The highlights of program life need to be accumulated unremittingly!
/******************************************************************************************/


NSURL* url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"456.mp3" ofType:nil]];
    
AVURLAsset* asset = [[AVURLAsset alloc] initWithURL:url options:nil];

[asset loadValuesAsynchronouslyForKeys:@[@"metadata"] completionHandler:^{

    AVKeyValueStatus commonStatus = [asset statusOfValueForKey:@"metadata" error:nil];
    if (commonStatus == AVKeyValueStatusLoaded) {

        // Song name
        AVMetadataItem *titleItem  = [AVMetadataItem metadataItemsFromArray:asset.metadata filteredByIdentifier:AVMetadataCommonIdentifierTitle].firstObject;
        // Singer
        AVMetadataItem *artistItem = [AVMetadataItem metadataItemsFromArray:asset.metadata filteredByIdentifier:AVMetadataCommonIdentifierArtist].firstObject;
        // Album name
        AVMetadataItem *albumItem  = [AVMetadataItem metadataItemsFromArray:asset.metadata filteredByIdentifier:AVMetadataCommonIdentifierAlbumName].firstObject;

            
        NSLog(@"%@ : %@", titleItem.key,  titleItem.value);
        NSLog(@"%@ : %@", artistItem.key, artistItem.value);
        NSLog(@"%@ : %@", albumItem.key,  albumItem.value);
    }
}];
    

/*
2021-07-24 23:25:12.442039+0800 LearnAVFoundation[7441:500002] TIT2 : Colors Of The Wind
2021-07-24 23:25:12.442086+0800 LearnAVFoundation[7441:500002] TPE1 : Thia Megia
2021-07-24 23:25:12.442113+0800 LearnAVFoundation[7441:500002] TALB : American Idol Top 12 Season 10
*/

5, Edit metadata

AVAsset It is an immutable class. If you want to save metadata changes, use AVAssetExportSession to export a new resource copy and metadata changes.

AVAssetExportSession is used to transcode the AVAsset content according to the export preset conditions, and write the exported assets to disk. It provides multiple functions to convert one format to another, revise the content of resources, modify the audio and video behavior of resources, and write new metadata.

To create an AVAssetExportSession instance, you need to provide resources and export presets. Export presets are used to determine the quality, size, and other properties of exported content. After you create the export session, you also specify the export content address outputURL, and finally call exportAsynchronouslyWithCompletionHandler to start exporting.


Jump to read the full text


**Note: * * avassetexportpreset passthrough can modify existing metadata information, but it cannot add new metadata. The only way to add metadata is to use transcoding preset values.

6, Guess you like it

No reprint without permission: Ape programming » AVFoundation – AVMetadataItem get media attribute metadata

Posted by allydm on Fri, 12 Nov 2021 01:00:59 -0800