2017-10-13 101 views
0

我正在使用相機記錄視頻並將它們保存到我的文檔文件夾中。我面臨的問題是,我從相機獲得的視頻是.avi文件,必須轉換爲.mp4(或任何其他允許的格式)。無法將iOS中的.avi轉換爲.mp4目標-c

我用下面的代碼:
來源:iOS Convert AVI to MP4 Format programatically

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"MyVideo.mp4"]; 
NSURL *outputURL = [NSURL fileURLWithPath:filePath]; 

[self convertVideoToLowQuailtyWithInputURL:localUrl outputURL:outputURL handler:^(AVAssetExportSession *exportSession) 
{ 
    if (exportSession.status == AVAssetExportSessionStatusCompleted) { 
     // Video conversation completed 
    }   
}]; 

- (void)convertVideoToLowQuailtyWithInputURL:(NSURL*)inputURL outputURL:(NSURL*)outputURL handler:(void (^)(AVAssetExportSession*))handler { 
    [[NSFileManager defaultManager] removeItemAtURL:outputURL error:nil]; 
    AVURLAsset *asset = [AVURLAsset URLAssetWithURL:inputURL options:nil]; 
    AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:asset presetName:AVAssetExportPresetPassthrough]; 
    exportSession.outputURL = outputURL; 
    exportSession.outputFileType = AVFileTypeMPEG4; 
    [exportSession exportAsynchronouslyWithCompletionHandler:^(void) { 
     handler(exportSession); 
    }]; 
} 

的exportSession狀態總是失敗。但是,當我嘗試將.mov轉換爲.mp4是行不通的。

我怎樣才能將.avi文件轉換爲.mp4文件?

+0

什麼是AVI視頻裏面的視頻編解碼器?如果您不知道AVI的詳細信息,請使用** MediaInfo **(檢查爲「文本」模式)等工具。 –

+0

所有信息MediaInfo給了我: 275 kb/s,640x480(4:3),在10.000 FPS,AVS(Baseline @ L3)(1 Ref frame)@ VC.One – Milander

+0

_「** AVS ** @ L3)「_ ??是錯字嗎?如果實際上你的意思是** AVC **,那麼你有一個MP4容器兼容的視頻編解碼器......是否有正確的編解碼器(AAC或MP3)的聲音?當你說_「exportSession狀態總是失敗」_沒有其他有用的錯誤信息?如果問題不是視頻編解碼器,不是音頻編解碼器,那麼問題必須是AVI文件本身。相機品牌也可能會製造一些奇怪的自定義AVI容器字節。 –

回答

0

嘗試打印錯誤消息:

switch ([exportSession status]) { 
     case AVAssetExportSessionStatusFailed: 
      NSLog(@"Export failed: %@", [[exportSession error] localizedDescription]); 
      break; 
     case AVAssetExportSessionStatusCancelled: 
      NSLog(@"Export canceled"); 
      break; 
      case AVAssetExportSessionStatusCompleted: 
      NSLog(@"Successfully"); 
      break; 
     default: 
      break; 
     } 

    }]; 
} 
+0

是的,你是對的,這是我的第一個答案,我已經改變了格式 – CharmingQin

+0

問題是,localizedDescription並不真正有用。 輸出爲'導出失敗:操作無法完成'。 – Milander

+0

@Milander thx爲您的建議 – CharmingQin