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
- how to use mp4box in objective-c
- How to use custom delegates in Objective-C
- How to use custom delegates in Objective-C
- How to use Objective-C on Window
- How to convert from int to string in objective c
- How to convert from int to string in objective c
- How to split a string to array in objective-c?
- w to Use a Dictionary in Objective-C
- How to Use the restrict Qualifier in C
- How to use epoll? A complete example in C
- How to use epoll? A complete example in C
- How to use epoll? A complete example in C
- How to use epoll? A complete example in C
- How to use epoll? A complete example in C
- How to use epoll? A complete example in C
- How to use epoll? A complete example in C
- How to use epoll? A complete example in C
- How to use epoll? A complete example in C
- C++私有成员访问
- linux 查看系统信息命令
- Chrome浏览器通过切换user agent模拟其他浏览器方法
- 获取BMP像素转16进制RGB565格式
- SSL简介
- how to use mp4box in objective-c
- hello csdn blog :)
- Hibernate 所有缓存机制详解
- Oracle计算时间差
- 有些数字
- 通过在拦截器配置request对象,在前台页面显示返回的消息(2)
- C++设计模式——单例模式
- windows应用开发由浅入深(一)AfxMessageBox消息框
- Spring MVC学习笔记 1:入门篇