【问题标题】:How to repeat audio track if video duration in longer than audio duration如果视频持续时间长于音频持续时间,如何重复音轨
【发布时间】:2013-04-26 05:42:22
【问题描述】:

我正在使用 AVMutableComposition 混合音频和视频文件。下面是我的代码:

enter code here
AVMutableComposition* mixComposition = [AVMutableComposition composition];

 NSString *bundleDirectory = [[NSBundle mainBundle] bundlePath];

 NSString *audio_inputFilePath = [bundleDirectory stringByAppendingPathComponent:@"xyz.mp3"];//audio of 35 seconds

 NSURL    *audio_inputFileUrl = [NSURL fileURLWithPath:audio_inputFilePath];

  NSURL    *video_inputFileUrl = [NSURL fileURLWithPath:videoOutputPath];

  NSString *outputFilePath = [documentsDirectory stringByAppendingPathComponent:@"video.mp4"];//video of 60 seconds

 NSURL    *outputFileUrl = [NSURL fileURLWithPath:outputFilePath];

 if ([[NSFileManager defaultManager] fileExistsAtPath:outputFilePath])
        [[NSFileManager defaultManager] removeItemAtPath:outputFilePath error:nil];

   CMTime nextClipStartTime = kCMTimeZero;

  AVURLAsset* videoAsset = [[AVURLAsset alloc]initWithURL:video_inputFileUrl options:nil];

 CMTimeRange video_timeRange = CMTimeRangeMake(kCMTimeZero,videoAsset.duration);

  AVMutableCompositionTrack *a_compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];

 [a_compositionVideoTrack insertTimeRange:video_timeRange ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:nextClipStartTime error:nil];

    AVURLAsset* audioAsset = [[AVURLAsset alloc]initWithURL:audio_inputFileUrl options:nil];
    enter code here
     CMTimeRange audio_timeRange = CMTimeRangeMake(kCMTimeZero, audioAsset.duration);
    enter code here
    AVMutableCompositionTrack *b_compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio preferredTrackID:kCMPersistentTrackID_Invalid];
    [b_compositionAudioTrack insertTimeRange:audio_timeRange ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:nextClipStartTime error:nil];
    enter code here
    AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPreset640x480];

我面临的问题是我的音频文件持续时间比视频持续时间短。所以我想做的是循环播放音频文件直到视频结束。就像我的视频是 60 秒,音频是 35 秒,所以音频应该重复 25 秒。

谁能帮助我如何做到这一点。

【问题讨论】:

标签: iphone avmutablecomposition avasset


【解决方案1】:

解决方案一:在AVMutableCompositionTrack中新建CMTimeRangeinsertTimeRange

if (videoAsset.duration>audioAsset.duration)
{ 
    //new time range  
    CMTime duration = CMTimeMakeWithSeconds(CMTimeGetSeconds(videoAsset.duration)-CMTimeGetSeconds(audioAsset.duration), audioAsset.duration.timescale);
   if (CMTIME_IS_VALID(duration))
   {
    CMTimeRange video_timeRange2 = CMTimeRangeMake(audioAsset.duration,duration);
    //start from where left
    CMTime nextClipStartTime2 = audioAsset.duration;
    //add in AVMutableCompositionTrack
    [b_compositionVideoTrack insertTimeRange:video_timeRange2 ofTrack:[[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0] atTime:nextClipStartTime2 error:nil];
   }
   else
       NSLog(@"time is invalid");
}

注意:未经测试,但应该可以工作。

编辑

解决方案 2:试试这个。不要使用我的代码并将下面的这一行替换为您的代码

[b_compositionAudioTrack insertTimeRange:audio_timeRange ofTrack:[[audioAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0] atTime:kCMTimeInvalid error:nil];

【讨论】:

  • 不工作。您的逻辑似乎正确,但 35 秒后音频仍然停止
  • 我已经尝试过使用 b_composition。我用了你的逻辑。不一样的线。是否可以在 mixcomposition 中添加两个音轨??
猜你喜欢
  • 2016-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-27
  • 2021-12-14
  • 2015-07-25
  • 1970-01-01
相关资源
最近更新 更多