Skip to content

Instantly share code, notes, and snippets.

@sergiou87
Last active November 26, 2015 11:00
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sergiou87/b3c438d49ae7bb695ba7 to your computer and use it in GitHub Desktop.
Save sergiou87/b3c438d49ae7bb695ba7 to your computer and use it in GitHub Desktop.
Add custom chapter thumbnails to video
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