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
@@ -24,6 +24,7 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
extern NSString * const MP4264BitData;
|
||||
extern NSString * const MP4264BitTime;
|
||||
extern NSString * const MP42GenerateChaptersPreviewTrack;
|
||||
extern NSString * const MP42ChaptersPreviewPosition;
|
||||
extern NSString * const MP42CustomChaptersPreviewTrack;
|
||||
|
||||
typedef void (^MP42FileProgressHandler)(double progress);
|
||||
|
||||
+6
-3
@@ -20,6 +20,7 @@
|
||||
NSString * const MP4264BitData = @"MP4264BitData";
|
||||
NSString * const MP4264BitTime = @"MP4264BitTime";
|
||||
NSString * const MP42GenerateChaptersPreviewTrack = @"MP42ChaptersPreview";
|
||||
NSString * const MP42ChaptersPreviewPosition = @"MP42ChaptersPreviewPosition";
|
||||
NSString * const MP42CustomChaptersPreviewTrack = @"MP42CustomChaptersPreview";
|
||||
|
||||
/**
|
||||
@@ -853,7 +854,7 @@ static void logCallback(MP4LogLevel loglevel, const char *fmt, va_list ap) {
|
||||
|
||||
// Generate previews images for chapters
|
||||
if ([options[MP42GenerateChaptersPreviewTrack] boolValue] && self.itracks.count) {
|
||||
[self createChaptersPreview];
|
||||
[self createChaptersPreviewAtPosition:[options[MP42ChaptersPreviewPosition] floatValue]];
|
||||
} else if ([options[MP42CustomChaptersPreviewTrack] boolValue] && self.itracks.count) {
|
||||
[self customChaptersPreview];
|
||||
}
|
||||
@@ -968,7 +969,7 @@ static void logCallback(MP4LogLevel loglevel, const char *fmt, va_list ap) {
|
||||
return YES;
|
||||
}
|
||||
|
||||
- (BOOL)createChaptersPreview {
|
||||
- (BOOL)createChaptersPreviewAtPosition:(CGFloat)position {
|
||||
NSInteger decodable = 1;
|
||||
MP42ChapterTrack *chapterTrack = nil;
|
||||
MP42VideoTrack *refTrack = nil;
|
||||
@@ -1001,7 +1002,9 @@ static void logCallback(MP4LogLevel loglevel, const char *fmt, va_list ap) {
|
||||
}
|
||||
|
||||
if (chapterTrack && decodable && (!jpegTrack)) {
|
||||
NSArray<NSImage *> *images = [MP42PreviewGenerator generatePreviewImagesFromChapters:chapterTrack.chapters fileURL:self.URL];
|
||||
NSArray<NSImage *> *images = [MP42PreviewGenerator generatePreviewImagesFromChapters:chapterTrack.chapters
|
||||
fileURL:self.URL
|
||||
atPosition:position];
|
||||
|
||||
// If we haven't got any images, return.
|
||||
if (!images || !images.count) {
|
||||
|
||||
@@ -14,7 +14,7 @@ NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface MP42PreviewGenerator : NSObject
|
||||
|
||||
+ (NSArray<NSImage *> *)generatePreviewImagesFromChapters:(NSArray<MP42TextSample *> *)chapters fileURL:(NSURL *)url;
|
||||
+ (NSArray<NSImage *> *)generatePreviewImagesFromChapters:(NSArray<MP42TextSample *> *)chapters fileURL:(NSURL *)url atPosition:(CGFloat)position;
|
||||
|
||||
@end
|
||||
|
||||
|
||||
@@ -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