• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

iphone - 如何在 iphone sdk 中合并实时流媒体视频和音频

[复制链接]
菜鸟教程小白 发表于 2022-12-12 09:26:04 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在 IOS 中开发基于视频的应用程序。在我的应用程序中,我需要合并视频和音频。我已经合并了本地视频和音频文件,但是如果我尝试合并它们,我无法合并实时流视频和音频,这意味着应用程序由于持续时间而崩溃。对于合并,我使用以下代码

    -(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 optionsptions];
    AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL: audiopath optionsptions];
    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 performSelectorselector(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);
    }

请一些人帮助我



Best Answer-推荐答案


您没有提供正确的实时视频 URL 来播放。

在您的代码中,您在文档目录中传递了本地视频 URL。 NSString *fileNamePath1 = @"Egg_break.mov"; 而不是这个,你应该从服务器传递一些 URLString。

例如

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

关于iphone - 如何在 iphone sdk 中合并实时流媒体视频和音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15244041/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap