-
Notifications
You must be signed in to change notification settings - Fork 0
/
FFmpegGui.m
191 lines (156 loc) · 6.48 KB
/
FFmpegGui.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//
// ffmpegGui.m
// ffmpegGui
//
// Created by Sebastian on 17.09.10.
// Copyright 2010 ICQ: 171680864. All rights reserved.
//
#import "ffmpegGui.h"
@implementation FFmpegGui
@synthesize videoWidth, videoHeight;
@synthesize videoWidthNew, videoHeightNew;
@synthesize maxRate, audioBitRate, audioSRate, threads, audioChannel, videoBitRate;
@synthesize videoWidthStd, videoHeightStd;
@synthesize aspectRatioStd, aspectRatio;
@synthesize outDirectory, tmpDirectory, ffmpegApp, videoparApp, logfilePath;
@synthesize inVFile;
@synthesize outVFile;
@synthesize controller;
-(id)init {
self = [super init];
// Set default parameters
self->videoWidthStd = 480;
self->videoHeightStd = 320;
self->aspectRatioStd = (float)self->videoWidthStd/self->videoHeightStd;
self->videoWidthNew = self->videoWidthStd;
self->videoHeightNew = self->videoHeightStd;
return self;
}
- (void)checkATaskStatus:(NSNotification *)aNotification {
const int ATASK_SUCCESS_VALUE = 0;
int status = [[aNotification object] terminationStatus];
if (status == ATASK_SUCCESS_VALUE)
NSLog(@"Task succeeded.");
else
NSLog(@"Task failed.");
[controller taskFinished];
}
- (void) getVideoPar {
videoparTask = [[NSTask alloc] init];
NSString *videoparPath = [[NSBundle mainBundle] pathForResource:@"videopar" ofType:@"pl"];
[videoparTask setLaunchPath: videoparPath];
NSArray* videoparArgs;
videoparArgs = [NSArray arrayWithObjects:
[self inVFile],
nil];
[videoparTask setArguments: videoparArgs];
NSPipe *pipe;
pipe = [NSPipe pipe];
[videoparTask setStandardOutput: pipe];
[videoparTask setStandardError: pipe];
NSFileHandle *file;
file = [pipe fileHandleForReading];
NSLog(@"Calling videopar with arguments: %@\n",videoparArgs);
// Redirect output to stdout
[videoparTask setStandardInput:[NSPipe pipe]];
[videoparTask launch];
[videoparTask waitUntilExit];
NSData *data;
data = [file readDataToEndOfFile];
NSString *string;
string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog (@"%@", string);
[controller textViewPrint:[NSString stringWithFormat:@"%@\n", string]];
NSArray* videoPar = [string componentsSeparatedByString:@" "];
NSArray* videoRes = [[videoPar objectAtIndex:0] componentsSeparatedByString:@"x"];
videoWidth=[[videoRes objectAtIndex:0] intValue];
videoHeight=[[videoRes objectAtIndex:1] intValue];
assert(videoHeight > 0);
assert(videoWidth > 0);
if (videoHeight != 0) {
aspectRatio = (float)self->videoWidth/self->videoHeight;
}
audioSRate = [[videoPar objectAtIndex:1] intValue];
audioChannel = [[videoPar objectAtIndex:2] intValue];
audioBitRate = [[videoPar objectAtIndex:3] intValue];
// Notify Controller about format infos
[controller setFormatProperties:(NSArray*)[[NSArray alloc] initWithObjects:
@"Resolution, audio sampling, audio channels, audio bitrate: ",
string,
nil ]];
}
- (void) startTranscode {
// [controllerCB processStarted];
transcodeTask1 = [[NSTask alloc] init];
NSString *transcodePath = [[NSBundle mainBundle] pathForResource:@"transcode" ofType:@"sh"];
[transcodeTask1 setLaunchPath: transcodePath];
NSArray *transcodeArguments;
//TODO also video bitrate
transcodeArguments = [NSArray arrayWithObjects:
[self inVFile],
[NSString stringWithFormat:@"%d",[self videoWidthNew]],
[NSString stringWithFormat:@"%d",[self videoHeightNew]],
[NSString stringWithFormat:@"%d",[self audioBitRate]],
[NSString stringWithFormat:@"%d",[self audioChannel]],
nil];
[transcodeTask1 setArguments: transcodeArguments];
NSPipe *pipe;
pipe = [NSPipe pipe];
[transcodeTask1 setStandardOutput: pipe];
[transcodeTask1 setStandardError: pipe];
// Register as task termination observer
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(checkATaskStatus:)
name:NSTaskDidTerminateNotification
object:nil];
// Register to get notified on new data from pipe
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(getData:)
name: NSFileHandleReadCompletionNotification
object: [[transcodeTask1 standardOutput] fileHandleForReading]];
// We tell the file handle to go ahead and read in the background asynchronously, and notify
// us via the callback registered above when we signed up as an observer. The file handle will
// send a NSFileHandleReadCompletionNotification when it has data that is available.
[[[transcodeTask1 standardOutput] fileHandleForReading] readInBackgroundAndNotify];
NSLog(@"Calling transcode with arguments: %@\n",transcodeArguments);
[controller taskStarted];
// Redirect output to stdout
//[transcodeTask1 setStandardInput:[NSPipe pipe]];
// launch the task asynchronously
[transcodeTask1 launch];
}
- (void) terminateTransTask {
NSData *data;
[[NSNotificationCenter defaultCenter] removeObserver:self
name:NSFileHandleReadCompletionNotification
object: [[transcodeTask1 standardOutput] fileHandleForReading]];
// Make sure the task has actually stopped!
[transcodeTask1 terminate];
while ((data = [[[transcodeTask1 standardOutput] fileHandleForReading] availableData]) && [data length])
{
[controller textViewPrint: [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]];
}
NSLog(@"Terminating task: %@\n", self->transcodeTask1);
[controller taskFinished];
}
// This method is called asynchronously when data is available from the task's file handle.
// We just pass the data along to the controller as an NSString.
- (void) getData: (NSNotification *)aNotification
{
NSData *data = [[aNotification userInfo] objectForKey:NSFileHandleNotificationDataItem];
// If the length of the data is zero, then the task is basically over - there is nothing
// more to get from the handle so we may as well shut down.
if ([data length])
{
// Send the data on to the controller; we can't just use +stringWithUTF8String: here
// because -[data bytes] is not necessarily a properly terminated string.
// -initWithData:encoding: on the other hand checks -[data length]
[controller textViewPrint: [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]];
} else {
// We're finished here
[self terminateTransTask];
}
// we need to schedule the file handle go read more data in the background again.
[[aNotification object] readInBackgroundAndNotify];
}
@end