开发者

QTKit Play Movie in Slow Motion

开发者 https://www.devze.com 2023-03-13 07:10 出处:网络
I\'m trying to play a movie in slow motion and attach it to an existing movie. Here\'s what I\'ve done so far:

I'm trying to play a movie in slow motion and attach it to an existing movie. Here's what I've done so far:

QTMovie *originalMovie = ...;
QTMovie *slowMotionMovie = ...;

[originalMovie insertSegmentOfMovie:slowMotionMovie timeRange:QTMakeTimeRange(QTZeroTime, [slowMotionMovie duration]) atTime:[originalMovie duration]];

This correctly adds the second movie to the firts one, but it doesn't play it in slow motion of course. I know there's another method insertSegmentOfMovie:fromRange:scaledToRange: but I have no idea how to set the time ranges in there.

Any help would be appreciated

UPDATE: I can use the following code to add slow motion:

[slowMotionMovie setAttribute:[NSNumber numberWithBool:YES] forKey:QTMovieEditableAttribute];
QTTime newDuration = QTMakeTime([slowMotionMovie duration].timeValue * 2, [slowMotionMovie duration].timeScale);
[slowMotionMovie scaleSegment:QTMakeTimeRange(QTZeroTime, [slowMotionMovie duration]) newDuration:newDuration];

This correctly plays the video in slow motion, but does not save the audio of the second video in slow motion. I.E. the second video is in slow motion but its audio is not!.

UPDATE 2:

To be clear, if I try to play originalVideo inside a quicktime player right away from my cocoa app, the second video's audio does play in slow motion. It's only the saved file whose audio is out of sync.

I'm posting the complete code just in case it helps:

- (void)captureOutput:(QTCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL forConnections:(NSArray *)connections dueToError:(NSError *)error
{

    QTMovie *originalMovie = [QTMovie movieWithURL:outputFileURL error:nil];
    [originalMovie setAttribute:[NSNumber numberWithBool:YES] forKey:QTMovieEditableAttribute];

    QTMovie *slowMotionMovie = [QTMovie movieWithURL:outputFileURL error:nil];
    [slowMotionMovie setAttribute:[NSNumber numberWithBool:YES] forKey:QTMovieEditableAttribute];

    // Scale the second video
    QTTime newDuration = QTMakeTime([slowMotionMovie duration].timeValue * 2, [slowMotionMovie duration].timeScale);
    [slowMotionMovie scaleSegment:QTMakeTimeRange(QTZeroTime, [slowMotionMovie duration]) newDurat开发者_StackOverflow社区ion:newDuration];

    // Attach Slow motion video
    [originalMovie insertSegmentOfMovie:slowMotionMovie timeRange:QTMakeTimeRange(QTZeroTime, [slowMotionMovie duration]) atTime:[originalMovie duration]];

    // If I play originalMovie, both video and audio of slowMotionMovie are correct (i.e. in slow motion)

    [originalMovie updateMovieFile];
    // However, the video that's being saved here has its audio out of sync (i.e. not in slow motion)
}

Update 3:

If I open the final file with a program other than quicktime, say vlc, the video stops playing at the end of the first video (where the motion should have to start), but the audio continues to play till the end.

Update 4:

If I use [originalMovie writeToFile:withAttributes] and specify a new location, everything works as expected. It's only when I try to overwrite a file that I run into every sort of problems.


Ok, so to add a second movie in slow motion one needs to do the following:

- (void)captureOutput:(QTCaptureFileOutput *)captureOutput didFinishRecordingToOutputFileAtURL:(NSURL *)outputFileURL forConnections:(NSArray *)connections dueToError:(NSError *)error
{

    QTMovie *originalMovie = [QTMovie movieWithURL:outputFileURL error:nil];
    [originalMovie setAttribute:[NSNumber numberWithBool:YES] forKey:QTMovieEditableAttribute];

    QTMovie *slowMotionMovie = [QTMovie movieWithURL:outputFileURL error:nil];
    [slowMotionMovie setAttribute:[NSNumber numberWithBool:YES] forKey:QTMovieEditableAttribute];

    // Scale the second video
    QTTime newDuration = QTMakeTime([slowMotionMovie duration].timeValue * 2, [slowMotionMovie duration].timeScale);
    [slowMotionMovie scaleSegment:QTMakeTimeRange(QTZeroTime, [slowMotionMovie duration]) newDuration:newDuration];

    // Attach Slow motion video
    [originalMovie insertSegmentOfMovie:slowMotionMovie timeRange:QTMakeTimeRange(QTZeroTime, [slowMotionMovie duration]) atTime:[originalMovie duration]];

}

But then, if you try to overwrite originalMovie you may get all kind of errors, so instead save it in a new location with [originalMovie writeToFile].

0

精彩评论

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