how to use mp4box in objective-c

来源:互联网 发布:肠道健康 知乎 编辑:程序博客网 时间:2024/05/16 13:43

//

//  ProcessingController.m

//  VF-X264

//

//  Created by John Paul Alcala on 10/30/10.

//  Copyright 2010 __MyCompanyName__. All rights reserved.

//


#import "ProcessingController.h"

#import "SettingsFactory.h"

#import "Constants.h"


@interface ProcessingController()


-(void) startBackgroundProcessing;

-(NSDictionary *) identifyFile:(NSString *)filename;

-(void) mkfifo;

-(void) runX264;

-(void) extractAudioAndVideoFromFile:(NSString *)filename;

-(void) convertAudioToAac;

-(void) createMP4PackageUsingFilename:(NSString *)filename andParams:(NSDictionary *)params;


@end



@implementation ProcessingController


-(IBAction) startProcessing:(id)sender {

[selfperformSelectorInBackground:@selector(startBackgroundProcessing)withObject:nil];

}


-(void) startBackgroundProcessing {

isProcessing=YES;


NSAutoreleasePool *pool=[[NSAutoreleasePoolalloc] init];

NSArray *inputFiles=inputSourceController.inputFiles;

for(NSURL *filein inputFiles) {

NSDictionary *fileParams=[selfidentifyFile:[file path]];

if ([fileParamscount]>1 &&isProcessing) {

[selfmkfifo];

}

if (isProcessing) {

[selfrunX264];

[selfextractAudioAndVideoFromFile:[file path]];

}

if (isProcessing) {

[selfconvertAudioToAac];

}

if (isProcessing) {

[selfcreateMP4PackageUsingFilename:[file lastPathComponent]andParams:fileParams];

}

}

[pooldrain];

}


-(NSDictionary *) identifyFile:(NSString *)filename {

NSBundle *mainBundle=[NSBundlemainBundle];

NSString *midentifyPath=[mainBundlepathForResource:@"midentify"ofType:nil inDirectory:@"Binaries"];

NSMutableArray *midentifyArgs=[NSMutableArrayarray];

[midentifyArgsaddObject:filename];

// identify the file before doing anything.

NSTask *midentifyTask=[[NSTaskalloc] init];

NSPipe *midentifyPipe=[NSPipepipe];

[midentifyTasksetLaunchPath:midentifyPath];

[midentifyTasksetArguments:midentifyArgs];

[midentifyTask setCurrentDirectoryPath:[midentifyPath stringByDeletingLastPathComponent]];

[midentifyTasksetStandardOutput:midentifyPipe];

[midentifyTasksetStandardInput:[NSPipepipe]];


[selfaddRunningTask:midentifyTask];

[midentifyTasklaunch];

[midentifyTaskwaitUntilExit];

[selfclearRunningTasks];

// process video info into a dictionary

NSData *midentifyOutput=[[midentifyPipefileHandleForReading] readDataToEndOfFile];

NSString *videoInfo=[[[NSStringalloc] initWithData:midentifyOutputencoding:NSUTF8StringEncoding]autorelease];

NSArray *videoInfoArray=[videoInfocomponentsSeparatedByString:@"\n"];

NSMutableDictionary *videoInfoDict=[NSMutableDictionarydictionary];

for(NSString *infoSegmentin videoInfoArray) {

NSArray *segment=[infoSegmentcomponentsSeparatedByString:@"="];

if ([segmentcount]>1) {

[videoInfoDictsetObject:[segment objectAtIndex:1] forKey:[segment objectAtIndex:0]];

}

}

return videoInfoDict;

}


-(void) mkfifo {

NSString *mkfifoPath=@"/usr/bin/mkfifo";

NSMutableArray *mkfifoArgs=[NSMutableArrayarray];

//[mkfifoArgs addObject:[NSTemporaryDirectory() stringByAppendingPathComponent:@"video.y4m"]];

[mkfifoArgs addObject:@"/Users/dragonlord/tmp2/video.y4m"];

NSTask *task=[[NSTaskalloc] init];

[tasksetLaunchPath:mkfifoPath];

[tasksetArguments:mkfifoArgs];

[selfaddRunningTask:task];

[tasklaunch];

[task waitUntilExit];

[selfclearRunningTasks];

}


-(void) runX264 {

NSBundle *mainBundle=[NSBundlemainBundle];

NSString *x264Path=[mainBundlepathForResource:@"x264"ofType:nilinDirectory:@"Binaries"];

NSMutableArray *x264Args=[SettingsFactoryx264SettingsForDevice:[targetFormatDropdowntitleOfSelectedItem]];

NSTask *task=[[NSTaskalloc] init];

[tasksetLaunchPath:x264Path];

[tasksetArguments:x264Args];

[selfaddRunningTask:task];

[tasklaunch];

}


-(void) extractAudioAndVideoFromFile:(NSString *)filename {

NSBundle *mainBundle=[NSBundlemainBundle];

NSString *mplayerPath=[mainBundlepathForResource:@"mplayer"ofType:nil inDirectory:@"Binaries"];

NSMutableArray *mplayerArgs=[SettingsFactorymplayerSettingsForDevice:[targetFormatDropdowntitleOfSelectedItem]];

[mplayerArgsaddObject:filename];

NSTask *task=[[NSTaskalloc] init];

[tasksetLaunchPath:mplayerPath];

[tasksetArguments:mplayerArgs];


[selfaddRunningTask:task];

[tasklaunch];

[task waitUntilExit];

[selfclearRunningTasks];

}


-(void) convertAudioToAac {

NSString *afconvertPath=@"/usr/bin/afconvert";

NSMutableArray *afconvertArgs=[SettingsFactoryafconvertSettings];

NSTask *task=[[NSTaskalloc] init];

[tasksetLaunchPath:afconvertPath];

[tasksetArguments:afconvertArgs];


[selfaddRunningTask:task];

[tasklaunch];

[task waitUntilExit];

[selfclearRunningTasks];

}


-(void) createMP4PackageUsingFilename:(NSString *)filename andParams:(NSDictionary *)params {

NSBundle *mainBundle=[NSBundlemainBundle];

NSString *mp4boxPath=[mainBundlepathForResource:@"MP4Box"ofType:nil inDirectory:@"Binaries"];

NSMutableArray *mp4boxArgs=[SettingsFactorymp4boxSettings];

[mp4boxArgsaddObject:@"-fps"];

[mp4boxArgsaddObject:[params valueForKey:@"ID_VIDEO_FPS"]];

[mp4boxArgsaddObject:@"-new"];

NSString *destinationFile=[[[destinationPathURL] path] stringByAppendingPathComponent:filename];

destinationFile=[[destinationFilestringByDeletingPathExtension] stringByAppendingPathExtension:@"mp4"];

[mp4boxArgsaddObject:destinationFile];

NSTask *task=[[NSTaskalloc] init];

[tasksetLaunchPath:mp4boxPath];

[tasksetArguments:mp4boxArgs];

[selfaddRunningTask:task];

[tasklaunch];

[task waitUntilExit];

[selfclearRunningTasks];

}


-(IBAction) stopProcessing:(id)sender {

@synchronized(self) {

isProcessing=NO;

}

@synchronized(self) {

for(NSTask *taskin runningTasks) {

[taskterminate];

}

}

[selfclearRunningTasks];

}


-(void) addRunningTask:(NSTask *)task {

@synchronized(self) {

if (runningTasks==nil) {

runningTasks=[NSMutableArrayarray];

[runningTasksretain];

}

[runningTasksaddObject:task];

}

}


-(void) clearRunningTasks {

@synchronized(self) {

if (runningTasks==nil) {

runningTasks=[NSMutableArrayarray];

[runningTasksretain];

}

for(NSTask *taskin runningTasks) {

[taskrelease];

}

[runningTasksremoveAllObjects];

}

}


-(BOOL) isProcessing {

returnisProcessing;

}


@end


0 0
原创粉丝点击