从iphone app上传到youtube的视频没有声音

时间:2012-03-29 07:53:00

标签: iphone objective-c youtube-api gdata-api

我正在开发一款iphone应用程序,用于记录其活动(屏幕录制)并上传到youtube。首先,在上传应用程序与视频混合一些声音之前,视频没有声音。输出视频在我的iphone和ipad中播放没有任何问题,但上传的视频没有声音播放(仅在启动时播放声音)。我的视频格式是.mov。 我的混合声音和视频的代码是

as in purplelilgirl's tutorial

-(NSString*) processVideo: (NSURL*) videoUrl{    NSLog(@"started processing %@",videoUrl);
    AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL: videoUrl options:nil];

    AVMutableComposition* mixComposition = [AVMutableComposition composition];


    NSError * error = nil;

    for (NSMutableDictionary * audioInfo in audioInfoArray){
        // NSString *pathString = [[NSHomeDirectory() stringByAppendingString:@"/Documents/"] stringByAppendingString: [audioInfo objectForKey: @"fileName"]];
       // NSString *pathString =  [audioInfo objectForKey: @"filePath"];
        NSURL *audioUrl=[audioInfo objectForKey: @"filePath"];
       // NSLog(@"audioUrl %@",audioUrl);
        AVURLAsset * urlAsset = [AVURLAsset URLAssetWithURL:audioUrl options:nil];

        AVAssetTrack * audioAssetTrack = [[urlAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
        AVMutableCompositionTrack *compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio 
                                                                                       preferredTrackID: kCMPersistentTrackID_Invalid];

       // NSLog(@"%lf", [[audioInfo objectForKey: @"startTime"] doubleValue]);

        CMTime audioStartTime = CMTimeMake(([[audioInfo objectForKey: @"startTime"] doubleValue]*TIME_SCALE), TIME_SCALE);

        [compositionAudioTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero,urlAsset.duration) ofTrack:audioAssetTrack atTime:audioStartTime error:&error];      
    }


    AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo 
                                                                                   preferredTrackID:kCMPersistentTrackID_Invalid];
    [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) 
                                   ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] 
                                    atTime:kCMTimeZero error:&error];

    AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition 
                                                                          presetName:AVAssetExportPresetPassthrough];   

    NSString* videoName = @"export.mov";

    NSString *exportPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:videoName];
    NSURL    *exportUrl = [NSURL fileURLWithPath:exportPath];

    if ([[NSFileManager defaultManager] fileExistsAtPath:exportPath]) 
    {
        [[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil];
    }

    _assetExport.outputFileType = @"com.apple.quicktime-movie";
   // _assetExport.outputFileType=AVFileTypeMPEG4;
    NSLog(@"file type %@",_assetExport.outputFileType);
    _assetExport.outputURL = exportUrl;
    _assetExport.shouldOptimizeForNetworkUse = YES;

    [_assetExport exportAsynchronouslyWithCompletionHandler:
     ^(void ) {
         switch (_assetExport.status) 
         {
             case AVAssetExportSessionStatusCompleted:
                 //export complete 
                 NSLog(@"Export Complete");
                 //[self uploadToYouTube];

                 break;
             case AVAssetExportSessionStatusFailed:
                 NSLog(@"Export Failed");
                 NSLog(@"ExportSessionError: %@", [_assetExport.error localizedDescription]);
                 //export error (see exportSession.error)  
                 break;
             case AVAssetExportSessionStatusCancelled:
                 NSLog(@"Export cancelled");
                 NSLog(@"ExportSessionError: %@", [_assetExport.error localizedDescription]);
                 //export cancelled  
                 break;
         }
     }];  
    NSLog(@"completed processing exportPath %@ ",exportPath);
    return exportPath;
}

我的代码有什么问题可以帮助我

1 个答案:

答案 0 :(得分:2)

编码的音频编解码器是什么?据我了解,如果你使用某种形式的苹果专有编解码器,可能会导致你的问题。