Added setting to set the position of the rendered chapter previews. Default is the middle of the chapter.
This commit is contained in:
1 parent
414b5e559a
commit
0af9487aa5
4 files changed
+27
-13
No files matched your search
@@ -10,21 +10,23 @@
|
||||
#import "MP42TextSample.h"
|
||||
#import <AVFoundation/AVFoundation.h>
|
||||
|
||||
#define MINIMUM_OFFSET 1800
|
||||
|
||||
@implementation MP42PreviewGenerator
|
||||
|
||||
+ (NSArray<NSImage *> *)generatePreviewImagesFromChapters:(NSArray<MP42TextSample *> *)chapters fileURL:(NSURL *)url {
|
||||
+ (NSArray<NSImage *> *)generatePreviewImagesFromChapters:(NSArray<MP42TextSample *> *)chapters fileURL:(NSURL *)url atPosition:(CGFloat)position {
|
||||
NSArray<NSImage *> *images = nil;
|
||||
|
||||
images = [MP42PreviewGenerator generatePreviewImagesAVFoundationFromChapters:chapters andFile:url];
|
||||
images = [MP42PreviewGenerator generatePreviewImagesAVFoundationFromChapters:chapters andFile:url atPosition:position];
|
||||
|
||||
return images;
|
||||
}
|
||||
|
||||
+ (NSArray *)generatePreviewImagesAVFoundationFromChapters:(NSArray *)chapters andFile:(NSURL *)file {
|
||||
+ (NSArray *)generatePreviewImagesAVFoundationFromChapters:(NSArray<MP42TextSample *> *)chapters andFile:(NSURL *)file atPosition:(CGFloat)position {
|
||||
NSMutableArray *images = [[NSMutableArray alloc] initWithCapacity:[chapters count]];
|
||||
|
||||
|
||||
AVAsset *asset = [AVAsset assetWithURL:file];
|
||||
|
||||
|
||||
if ([asset tracksWithMediaCharacteristic:AVMediaCharacteristicVisual]) {
|
||||
AVAssetImageGenerator *generator = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset];
|
||||
generator.appliesPreferredTrackTransform = YES;
|
||||
@@ -32,16 +34,24 @@
|
||||
generator.requestedTimeToleranceBefore = kCMTimeZero;
|
||||
generator.requestedTimeToleranceAfter = kCMTimeZero;
|
||||
|
||||
for (MP42TextSample *chapter in chapters) {
|
||||
CMTime time = CMTimeMake([chapter timestamp] + 1800, 1000);
|
||||
CGFloat offset;
|
||||
|
||||
for (NSInteger idx = 0; idx < chapters.count; idx++)
|
||||
{
|
||||
MP42TextSample *chapter = chapters[idx];
|
||||
MP42Duration nextChapterTimestamp = (idx < chapters.count - 1) ? [chapters[idx+1] timestamp] : CMTimeGetSeconds(asset.duration) * 1000;
|
||||
if (position <= 0.0) offset = MINIMUM_OFFSET;
|
||||
else if (position >= 1.0) offset = ((nextChapterTimestamp - [chapter timestamp]) * position) - MINIMUM_OFFSET;
|
||||
else offset = (nextChapterTimestamp - [chapter timestamp]) * position;
|
||||
CMTime time = CMTimeMake([chapter timestamp] + MAX(MINIMUM_OFFSET, offset), 1000);
|
||||
CGImageRef imgRef = [generator copyCGImageAtTime:time actualTime:NULL error:NULL];
|
||||
if (imgRef) {
|
||||
NSSize size = NSMakeSize(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));
|
||||
NSImage *frame = [[NSImage alloc] initWithCGImage:imgRef size:size];
|
||||
|
||||
|
||||
[images addObject:frame];
|
||||
}
|
||||
|
||||
|
||||
CGImageRelease(imgRef);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user