2013-03-06 265 views
2

我正在開發基於視頻的應用程序在IOS中。在我的應用程序中,我需要合併視頻和音頻。我合併了本地視頻和音頻文件,但如果我嘗試合併它們,則無法合併實時流式視頻和音頻,這意味着應用程序因持續時間而崩潰。對於合併我使用下面的代碼如何合併iPhone中的實時流視頻和音頻sdk

-(void) playerFunction 
    { 
          NSURL *url = [NSURL URLWithString:@"http://www.digdang.com/media/VideoFolde/017141.mp4"]; 
    
//    NSURL *url = [NSURL URLWithString:@"http://www.educator.com:1935/mobile/mp4:testVideo.mp4/playlist.m3u8"];// these is totally not working 
    
    
    
    NSString* audio_inputFileName = @"audio.mp3"; 
    NSString* audio_inputFilePath = [NSString stringWithFormat:@"%@/%@",[[NSBundle mainBundle] resourcePath],audio_inputFileName]; 
    NSURL*    audiopath = [NSURL fileURLWithPath:audio_inputFilePath]; 

    NSString* videoName = @"output.mov";//outputdata 
    NSString *savepath = [NSTemporaryDirectory() stringByAppendingPathComponent:videoName]; 
    
    NSFileManager *filemgr = [NSFileManager defaultManager]; 
    
    
    if ([filemgr fileExistsAtPath:savepath ] == YES){ 
        [[NSFileManager defaultManager] removeItemAtPath:savepath error:nil]; 
    } 
    else 
    { 
        NSLog (@"File not found"); 
    } 
    CMTime nextClipStartTime = kCMTimeZero; 
    
    AVMutableComposition* mixComposition = [AVMutableComposition composition]; 
    NSDictionary *options = @{ AVURLAssetPreferPreciseDurationAndTimingKey : @YES }; 
    AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:url options:options]; 
    AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL: audiopath options:options]; 
    CMTimeRange video_timeRange = CMTimeRangeMake(kCMTimeZero,videoAsset.duration); 
    AVMutableCompositionTrack *a_compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid]; 
    NSLog(@"%@",[videoAsset tracksWithMediaType:AVMediaTypeVideo]); 
    NSLog(@"%@",[audioAsset tracksWithMediaType:AVMediaTypeAudio]); 
    NSLog(@"%f", CMTimeGetSeconds(videoAsset.duration)); 
    [a_compositionVideoTrack insertTimeRange:video_timeRange ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:kCMTimeZero error:nil]; 
    [a_compositionVideoTrack scaleTimeRange:video_timeRange toDuration:audioAsset.duration]; 
    
    CMTimeRange audio_timeRange = CMTimeRangeMake(kCMTimeZero, audioAsset.duration); 
    AVMutableCompositionTrack *b_compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid]; 
    [b_compositionAudioTrack insertTimeRange:audio_timeRange ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:nextClipStartTime error:nil]; 
    
    
    
    AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetLowQuality]; 
    _assetExport.shouldOptimizeForNetworkUse = YES; 
    _assetExport.outputFileType = @"com.apple.quicktime-movie"; 
    
  
    NSURL    *savetUrl = [NSURL fileURLWithPath:savepath]; 

    _assetExport.outputURL = savetUrl; 
    _assetExport.timeRange = CMTimeRangeMake(kCMTimeZero, audioAsset.duration); 
    
    [_assetExport exportAsynchronouslyWithCompletionHandler: 
     ^(void) { 
         switch (_assetExport.status) 
         { 
             case AVAssetExportSessionStatusCompleted: 
                 //   export complete 
                 
                 NSLog(@"Export Complete"); 
                 //------>>> // From Here I want play movie using MPMoviePlayerController.<<<--------- 
                 [self play]; 
//                 [self performSelector:@selector(play) withObject:nil afterDelay:2.0]; 
                 break; 
             case AVAssetExportSessionStatusFailed: 
                 NSLog(@"Export Failed"); 
                 NSLog(@"ExportSessionError: %@", [_assetExport.error localizedDescription]); 
                 
                 //                export error (see exportSession.error) 
                 break; 
             case AVAssetExportSessionStatusCancelled: 
                 NSLog(@"Export Failed"); 
                 NSLog(@"ExportSessionError: %@", [_assetExport.error localizedDescription]); 
                 
                 //                export cancelled 
                 break; 
                 
         } 
         
         
     } 
     
     ]; 

    
    NSLog(@"savepath :%@",savepath); 
    } 

請一些身體幫我

+0

嘗試通過CMTimeRangeMake(kCMTimeZero,videoAsset.duration)在insetTimeRange compositionCommentaryTrack – BhushanVU 2013-03-06 10:05:13

+0

的應用程序,因爲該行只有爵士崩潰的。該應用程序崩潰在下面的行[compositionCommentaryTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,audioAsset.duration)ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeZero error:nil]; – btmanikandan 2013-03-06 11:22:52

+0

原因雖然直播可能是我們沒有得到正確的videoasset持續時間... – BhushanVU 2013-03-06 11:24:51

回答

1

你是不是給正確的直播視頻網址玩。

在您的代碼中,您已通過文檔目錄中的本地視頻URL。 NSString *fileNamePath1 = @"Egg_break.mov";而不是你應該從服務器傳遞一些URLString。

例如

AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL URLWithString:@"http://qtdevseed.apple.com/addemo/ad.m3u8"] options:nil]; 
+0

[NSURL URLWithString:@「http://www.digdang.com/media/VideoFolde/017141.mp4」] AVURLAsset * videoAsset = [AVURLAsset URLAssetWithURL:url options:nil];雖然通過這也是我有同樣的錯誤 – btmanikandan 2013-03-07 07:35:03

+0

有問題在網址你給...檢查其中一個播放直播視頻和檢查你的鏈接的IOS教程... – BhushanVU 2013-03-07 07:47:23

+0

它在玩mpmovie球員和avplayer – btmanikandan 2013-03-07 08:16:21