-
-
Save sergiou87/b3c438d49ae7bb695ba7 to your computer and use it in GitHub Desktop.
Add custom chapter thumbnails to video
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://images.apple.com/v/tv/c/images/overview/buy_tv_large.jpg"]]; | |
AVPlayerItem *playerItem = [[AVPlayerItem alloc] initWithURL:[NSURL URLWithString:@"http://download.wavetlan.com/SVV/Media/HTTP/H264/Other_Media/H264_test8_voiceclip_mp4_480x320.mp4"]]; | |
{ | |
AVMutableMetadataItem *title = [[AVMutableMetadataItem alloc] init]; | |
title.key = AVMetadataCommonKeyTitle; | |
title.keySpace = AVMetadataKeySpaceCommon; | |
title.locale = NSLocale.currentLocale; | |
title.value = @"Sample File"; | |
AVMutableMetadataItem *description = [[AVMutableMetadataItem alloc] init]; | |
description.key = AVMetadataCommonKeyDescription; | |
description.keySpace = AVMetadataKeySpaceCommon; | |
description.locale = NSLocale.currentLocale; | |
description.value = @"Lorem fistrum pupita me cago en tus muelas torpedo la caidita de la pradera la caidita no te digo trigo por no llamarte Rodrigor ese pedazo de. Ahorarr ese hombree te va a hasé pupitaa fistro te va a hasé pupitaa sexuarl pupita torpedo ese pedazo de quietooor. Ahorarr papaar papaar condemor apetecan está la cosa muy malar pecador mamaar hasta luego Lucas. Ese pedazo de pupita tiene musho peligro fistro diodenoo por la gloria de mi madre apetecan qué dise usteer. No puedor a wan pupita jarl va usté muy cargadoo diodenoo."; | |
AVMutableMetadataItem *artwork = [[AVMutableMetadataItem alloc] init]; | |
artwork.key = AVMetadataCommonKeyArtwork; | |
artwork.keySpace = AVMetadataKeySpaceCommon; | |
artwork.value = imageData; | |
artwork.locale = NSLocale.currentLocale; | |
playerItem.externalMetadata = @[title, artwork, description]; | |
} | |
{ | |
NSMutableArray *chapterMetadataGroups = [NSMutableArray array]; | |
for (NSUInteger idx = 0; idx < 10; idx++) | |
{ | |
CMTime time = CMTimeMakeWithSeconds(idx * 10000, 1000); | |
CMTime duration = CMTimeMakeWithSeconds(10000, 1000); | |
AVMutableMetadataItem *title = [[AVMutableMetadataItem alloc] init]; | |
title.key = AVMetadataCommonKeyTitle; | |
title.keySpace = AVMetadataKeySpaceCommon; | |
title.value = [$$:@"Sample Chapter %@", @(idx)];; | |
title.locale = NSLocale.currentLocale; | |
title.time = time; | |
title.duration = duration; | |
AVMutableMetadataItem *thumbnail = [[AVMutableMetadataItem alloc] init]; | |
thumbnail.key = AVMetadataCommonKeyArtwork; | |
thumbnail.keySpace = AVMetadataKeySpaceCommon; | |
thumbnail.locale = [NSLocale currentLocale]; | |
thumbnail.time = time; | |
thumbnail.duration = duration; | |
thumbnail.value = imageData; | |
[chapterMetadataGroups addObject:[[AVTimedMetadataGroup alloc] | |
initWithItems:@[title, thumbnail] | |
timeRange:CMTimeRangeMake(time, duration)]]; | |
} | |
AVNavigationMarkersGroup *chapterGroup = [[AVNavigationMarkersGroup alloc] | |
initWithTitle:@"Chapters" | |
timedNavigationMarkers:chapterMetadataGroups]; | |
playerItem.navigationMarkerGroups = @[chapterGroup]; | |
} | |
AVPlayer *player = [[AVPlayer alloc] initWithPlayerItem:playerItem]; | |
AVPlayerViewController *playerVC = [[AVPlayerViewController alloc] init]; | |
playerVC.player = player; | |
[self.window.rootViewController presentViewController:playerVC animated:YES completion:nil]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment