NSTask and FFMpeg losing output

2019-08-01 02:35发布

I'm trying to call ffmpeg from NSTask in objective-c. I execute the ffmpeg command in terminal and it works flawlessly every time. I make the same command using NSTask, and it never gives me the whole output. It cuts it off half way through the output, at a seemingly random spot every time. Here is my code.

    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    NSString* ffmpegPath = [[NSBundle mainBundle] pathForResource:@"ffmpeg" ofType:@""];
    NSString* path = @"test.mov";

    NSTask *task = [[NSTask alloc] init];
    NSArray *arguments = [NSArray arrayWithObjects: @"-i", path, nil];
    NSPipe *pipe = [NSPipe pipe];
    NSFileHandle * read = [pipe fileHandleForReading];

    [task setLaunchPath: ffmpegPath];
    [task setArguments: arguments];
    [task setStandardOutput: pipe];
    [task launch];
    [task waitUntilExit];

    NSData* data = [read readDataToEndOfFile];
    NSString* stringOutput = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];


    NSLog(@"%@", stringOutput);
    NSLog(@"%i", [task terminationStatus]);
    NSLog(@"DONE");
}

1条回答
Bombasti
2楼-- · 2019-08-01 03:01

And just like that I figured it out. Apparently the output had non UTF8Characters in it. Switched it over to NSASCIIStringEncoding and voila. Magic.

查看更多
登录 后发表回答