-
Notifications
You must be signed in to change notification settings - Fork 0
/
MP42File.m
121 lines (100 loc) · 3.23 KB
/
MP42File.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//
// MP42File.m
// Subler
//
// Created by Damiano Galassi on 31/01/09.
// Copyright 2009 Damiano Galassi. All rights reserved.
//
#import "MP42File.h"
@implementation MP42File
- (id) initWithExistingFile:(NSString *)path
{
if (self = [super init])
{
fileHandle = MP4Read([path UTF8String], 0);
filePath = path;
if (!fileHandle) {
[self release];
return nil;
}
metadata = [[MP42Metadata alloc] initWithSourcePath:filePath fileHandle:fileHandle];
MP4Close(fileHandle);
}
return self;
}
- (void) optimize
{
BOOL noErr;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString * tempPath = [NSString stringWithFormat:@"%@%@", filePath, @".tmp"];
noErr = MP4Optimize([filePath UTF8String], [tempPath UTF8String], MP4_DETAILS_ERROR);
if (noErr) {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
[fileManager removeItemAtPath:filePath error:&error];
[fileManager moveItemAtPath:tempPath toPath:filePath error:&error];
}
[pool release];
}
- (BOOL) writeToFilePath:(NSString *)absoulteFilePath flags:(uint64_t)flags
error:(NSError **)outError removeAllTags:(BOOL)performRemove
{
BOOL success = NO;
filePath = absoulteFilePath;
NSString *fileExtension = [filePath pathExtension];
char* majorBrand = "mp42";
char* supportedBrands[4];
u_int32_t supportedBrandsCount = 0;
if ([fileExtension isEqualToString:@"m4v"]) {
majorBrand = "M4V ";
supportedBrands[0] = majorBrand;
supportedBrands[1] = "M4A ";
supportedBrands[2] = "mp42";
supportedBrandsCount = 3;
}
else if ([fileExtension isEqualToString:@"m4a"]) {
majorBrand = "M4A ";
supportedBrands[0] = majorBrand;
supportedBrands[1] = "mp42";
supportedBrandsCount = 2;
}
else {
supportedBrands[0] = majorBrand;
supportedBrandsCount = 1;
}
fileHandle = MP4CreateEx([filePath UTF8String], MP4_DETAILS_ERROR,
flags, 1, 1,
majorBrand, 0,
supportedBrands, supportedBrandsCount);
if (fileHandle) {
MP4SetTimeScale(fileHandle, 600);
MP4Close(fileHandle);
success = [self updateMP4File:outError removeAllTags:performRemove];
}
return success;
}
- (BOOL) updateMP4File:(NSError **)outError removeAllTags:(BOOL)performRemove
{
fileHandle = MP4Modify([filePath UTF8String], MP4_DETAILS_ERROR, 0);
if (fileHandle == MP4_INVALID_FILE_HANDLE) {
if ( outError != NULL) {
NSMutableDictionary *errorDetail = [NSMutableDictionary dictionary];
[errorDetail setValue:@"Failed to open mp4 file" forKey:NSLocalizedDescriptionKey];
*outError = [NSError errorWithDomain:@"MP42Error"
code:100
userInfo:errorDetail];
}
return NO;
}
if (metadata.isEdited || performRemove)
[metadata writeMetadataWithFileHandle:fileHandle removeAll:performRemove];
MP4Close(fileHandle);
return YES;
}
- (void) dealloc
{
[metadata release];
[super dealloc];
}
@synthesize metadata;
@end