如何在 Apple TV 中编辑视频信息?

How to edit video's info in Apple TV?

我尝试在信息框下添加视频的一些基本信息,但不知道如何添加或更新?

有人知道怎么做吗?

TVJS

您可以直接在实例化的 MediaItem 对象上设置元数据属性。

var mediaItem = new MediaItem("video", videoURL);
mediaItem.title = "My Title";
mediaItem.subtitle = "My Subtitle";
mediaItem.artworkImageURL = someURL;
mediaItem.description = "My description";

有关详细信息,请参阅 here and here

AVKit

Swift

您应该将 externalMetadata 添加到您的 AVPlayerItem。为此,您可以添加 Apple 在其 WWDC 会话中提供的这两个辅助函数:

    func metadataItem(identifier : String, value : protocol<NSCopying,
        NSObjectProtocol>?) -> AVMetadataItem? {
        if let actualValue = value {
            let item = AVMutableMetadataItem()
            item.value = actualValue
            item.identifier = identifier
            item.extendedLanguageTag = "und" // undefined (wildcard) language
            return item.copy() as? AVMetadataItem
        }
        return nil
    }
    
    func metadataArtworkItem(image: UIImage) -> AVMetadataItem? {
        let item = AVMutableMetadataItem()
        // Choose PNG or JPEG
        item.value = UIImagePNGRepresentation(image)
        item.dataType = kCMMetadataBaseDataType_PNG as String
        item.identifier = AVMetadataCommonIdentifierArtwork
        item.extendedLanguageTag = "und"
        return item.copy() as? AVMetadataItem
    }

第一个将采用标识符、您要添加的元数据类型以及该类型的值,return 一个可选的 AVMetadataItem 另一个采用 UIImage 和 return 是一回事。

现在您可以在更新 AVPlayerItem 时说类似下面的话:

let videoURL = NSURL(string: "http://devstreaming.apple.com/videos/wwdc/2016/506ms2tv71tcduwp3dm/506/hls_vod_mvp.m3u8")
self.player = AVPlayer(URL: videoURL!)
let playerItem = self.player?.currentItem
var allItems: [AVMetadataItem] = [AVMetadataItem]()
allItems.append(self.metadataItem(AVMetadataCommonIdentifierTitle, value: "AVKit on tvOS")!)
allItems.append(self.metadataItem(AVMetadataCommonIdentifierDescription, value: "2016 WWDC session AVKit on tvOS where adding metadata to AVPlayerItem is covered")!)
if let image = UIImage(named: "Poster"), let artworkItem = metadataArtworkItem(image)
{
    allItems.append(artworkItem)
}
playerItem?.externalMetadata = allItems

这给你类似的东西:

您可以找到更多信息here

Objective-C

- (AVMetadataItem *)metadataItemWithIdentifier:(NSString *)identifier value:(id<NSObject, NSCopying>) value
{
    AVMutableMetadataItem *item = [[AVMutableMetadataItem alloc]init];
    item.value = value;
    item.identifier = identifier;
    item.extendedLanguageTag = @"und";
    return [item copy];
}

- (AVMetadataItem *)metadataArtworkItemWithImage:(UIImage *)image
{
    AVMutableMetadataItem *item = [[AVMutableMetadataItem alloc]init];
    item.value = UIImagePNGRepresentation(image);
    item.dataType = (__bridge NSString * _Nullable)(kCMMetadataBaseDataType_PNG);
    item.identifier = AVMetadataCommonIdentifierArtwork;
    item.extendedLanguageTag = @"und";
    return item.copy;
}

// Add this code where you update the AVPlayerItem
NSURL *videoURL = [NSURL URLWithString:self.detailItem.urlString];
playerViewController.player = [AVPlayer playerWithURL:videoURL];
self.player = playerViewController.player;
AVPlayerItem *playerItem = self.player.currentItem;
NSMutableArray <AVMetadataItem *> *allItems = [NSMutableArray new];
[allItems addObject:[self metadataItemWithIdentifier:AVMetadataCommonIdentifierTitle value:@"AVKit on tvOS"]];
[allItems addObject:[self metadataItemWithIdentifier:AVMetadataCommonIdentifierDescription value:@"2016 WWDC session AVKit on tvOS where adding metadata to AVPlayerItem is covered"]];
[allItems addObject:[self metadataArtworkItemWithImage:[UIImage imageNamed:@"Poster"]]];
playerItem.externalMetadata = allItems;

如果您需要下载图像 异步 - 就像我的情况一样 - 我正在使用它:

Objective-C

#import <AFNetworking/UIKit+AFNetworking.h>

@property (strong, nonatomic) NSMutableArray<AVMetadataItem*>* metaData;


// Build title item
AVMutableMetadataItem* titleMetadataItem = [[AVMutableMetadataItem alloc] init];
titleMetadataItem.locale = NSLocale.currentLocale;
titleMetadataItem.key = AVMetadataCommonKeyTitle;
titleMetadataItem.keySpace = AVMetadataKeySpaceCommon;
titleMetadataItem.value = self.currentVideoData.Title;
[_metaData addObject:titleMetadataItem];

// Build description item
AVMutableMetadataItem* descriptionMetadataItem = [[AVMutableMetadataItem alloc] init];
descriptionMetadataItem.locale = NSLocale.currentLocale;
descriptionMetadataItem.key = AVMetadataCommonKeyDescription;
descriptionMetadataItem.keySpace = AVMetadataKeySpaceCommon;
descriptionMetadataItem.value = description;
[_metaData addObject:descriptionMetadataItem];

self.player.currentItem.externalMetadata = _metaData;


NSString* artwork = @"https://Your_image_path"
__weak typeof(self) weakSelf = self;
    if (![artwork isEqualToString:@""]) {
        // Build artwork item
        UIImageView* imgArtwork = [UIImageView new];
        imgArtwork.contentMode = UIViewContentModeScaleAspectFit;

        [imgArtwork
            setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:artwork]]
                  placeholderImage:nil
                           success:^(NSURLRequest* _Nonnull request,
                                     NSHTTPURLResponse* _Nullable response,
                                     UIImage* _Nonnull image) {

                               [weakSelf.metaData
                                   addObject:[weakSelf metadataArtworkItemWithImage:image]];
                               weakSelf.player.currentItem.externalMetadata = _metaData; // re-setting meta data here...
                           }
                           failure:nil];
    }

//...


// same method of @beyowulf
- (AVMetadataItem*)metadataArtworkItemWithImage:(UIImage*)image {
    AVMutableMetadataItem* item = [[AVMutableMetadataItem alloc] init];
    item.value = UIImagePNGRepresentation(image);
    item.dataType = (__bridge NSString * _Nullable)(kCMMetadataBaseDataType_PNG);
    item.identifier = AVMetadataCommonIdentifierArtwork;
    item.extendedLanguageTag = @"und";
    return item.copy;
}

它就像一个魅力。