'macOS modify the cover art (ArtWork) of a video file, I found some API code, but it fails to modify any metadata

As the title.
I am writing an app for macOS.
Used to modify the cover (ArtWork) and title of the video file.
I found some API code, it doesn't work, any metadata of the video file has not been modified, no error is reported, I don't know what the problem is.
Instead of exporting a new copy of the video file, I want to modify the metadata directly on the original video file.

Code:

NSString * path = [NSString stringWithFormat:@"%@/en.m4v",[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]];
NSURL* url = [NSURL fileURLWithPath:path];

NSString * imgpath = [NSString stringWithFormat:@"%@/en.png",[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]];
NSURL* imgurl = [NSURL fileURLWithPath:imgpath];

AVMutableMetadataItem *item = [[AVMutableMetadataItem alloc] init];
item.keySpace = AVMetadataKeySpaceCommon;

//title
item.key = AVMetadataCommonKeyTitle;
item.value = @"MyTitle";

AVMutableMetadataItem *item1 = [[AVMutableMetadataItem alloc] init];

//image
item1.key = AVMetadataCommonKeyArtwork;
item1.value = [[NSImage alloc] initWithContentsOfURL:imgurl];

NSArray * newMetadataArray = [NSArray arrayWithObjects:item,item1, nil];

NSError * err;
AVAssetWriter * assetWrtr = [[AVAssetWriter alloc] initWithURL:url fileType:AVFileTypeAppleM4V error:&err];

assetWrtr.metadata = newMetadataArray;

[assetWrtr startWriting];
[assetWrtr startSessionAtSourceTime:kCMTimeZero];


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source