forked from marcingrabda/whatsub
-
Notifications
You must be signed in to change notification settings - Fork 1
/
FrameRateCalculator.m
executable file
·54 lines (41 loc) · 1.64 KB
/
FrameRateCalculator.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//
// MGFrameRateCalculator.m
// WhatSub
//
// Created by Marcin Grabda on 5/11/09.
// Copyright 2010 www.burningtomato.com. All rights reserved.
//
#import "FrameRateCalculator.h"
#import "RegexKitLite.h"
@implementation FrameRateCalculator
+ (float)calculateFrameRateForMovie:(QTMovie*)movie
{
NSArray* videoTracks = [movie tracksOfMediaType:QTMediaTypeVideo];
QTTrack* firstVideoTrack = [videoTracks objectAtIndex:0];
QTMedia* media = [firstVideoTrack media];
QTTime QTTime = [[media attributeForKey:QTMediaDurationAttribute] QTTimeValue];
long movieSeconds = [FrameRateCalculator convertQTTimeToSeconds:QTTime];
long sampleCount = [[media attributeForKey:QTMediaSampleCountAttribute] longValue];
float calculatedFrameRate = (float)sampleCount / (float)movieSeconds;
NSLog(@"Movie framerate: %f", calculatedFrameRate);
return calculatedFrameRate;
}
+ (float)calculateFrameRateForMovieInPath:(NSString*)filePath
{
NSError* error;
NSLog(@"Loading movie %@", filePath);
QTMovie* movie = [QTMovie movieWithFile:filePath error:&error];
return [self calculateFrameRateForMovie:movie];
}
+ (long)convertQTTimeToSeconds:(QTTime)QTTime
{
//0:01:45:43.21/25 = sign:days:hours:minutes:seconds:timevalue:timescale
NSString* QTTimeDurationString = QTStringFromTime(QTTime);
NSArray* separated = [QTTimeDurationString componentsSeparatedByRegex:@":"];
long days = [[separated objectAtIndex:0] integerValue];
long hours = [[separated objectAtIndex:1] integerValue];
long minutes = [[separated objectAtIndex:2] integerValue];
long seconds = [[separated objectAtIndex:3] integerValue];
return 86400*days + hours*3600 + minutes*60 + seconds;
}
@end