开发者

QTKit isn't properly mixing audio tracks

开发者 https://www.devze.com 2023-02-17 22:21 出处:网络
I\'m trying to import a single movie file twice (2 separate QTMovie ins开发者_如何学Gotances) offset the audio track of the second instance by a second and then mix it with the audio track of the firs

I'm trying to import a single movie file twice (2 separate QTMovie ins开发者_如何学Gotances) offset the audio track of the second instance by a second and then mix it with the audio track of the first instance. It doesn't mix. The first track starts to play and then it stops and the second track starts to play. This doesn't happen when I use 2 movie files with different content but it still happens when I copy the file to a different name! I created a simple example project that illustrates the problem: http://cl.ly/0s2U2s3S2F0Y052D2v0O/InsertTrack.zip

I'm also happy to pay for the solution to this problem!


The solution isn't quite straight forward, as there are a few things going on here:

  1. -[QTMovie insertSegmentOf(Movie|Track):timeRange:atTime:] does not add tracks to a movie, if the encoding of the track(s) match that of the track(s) that are already present. Instead, it inserts the segment into the existing movie, as QuickTime Player Pro's "Add Selection" does (i.e. if your movie was AAAAA and you insert bbb then you get AAbbbAAA). (Admittedly, the class-reference isn't very concise here…)
  2. Unfortunately -[QTMovie insertSegmentOfTrack:fromRange:scaledToRange:] does not add tracks either. Instead, it behaves like insertSegmentOfTrack:timeRange:atTime: with the added ability to change the duration of the inserted segment.

The solution is to use -[QTMovie insertSegmentOfMovie:fromRange:scaledToRange:].

For your example this would become

-(QTMovie *) composedMovie
{
    NSDictionary *editableAttribute = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:QTMovieEditableAttribute];
    // load and prepare the base movie:
    QTMovie *composition = [QTMovie movieWithFile:fileName error:nil];
    [composition setMovieAttributes:editableAttribute];
    // load and prepare the insertion
    QTMovie *insertion = [QTMovie movieWithFile:insertionFileName error:nil]:
    [insertion setMovieAttributes:editableAttribute];
    // as you'll probably want to avoid adding the video tracks...
    NSArray *videoTracks = [insertion tracksOfType:QTMediaTypeVideo];
    for (QTTrack *track in videoTracks) {
        [insertion removeTrack:track];
    }
    // figure out the time-range
    QTTimeRange sourceRange = QTMakeTimeRange( QTZeroTime, [insertion duration] );
    QTTimeRange destinationRange = sourceRange;
    destinationRange.time = QTMakeTime( 1ll, 1l ); // use an offset of 1 second
    [composition insertSegmentOfMovie:insertion fromRange:sourceRange scaledToRange:destinationRange];
    return composition;
}

I know, it's quite a bootload of code for something that's actually pretty straight-forward…but that's the way it works :-(

0

精彩评论

暂无评论...
验证码 换一张
取 消