2

我正在尝试以慢动作播放电影并将其附加到现有电影中。这是我到目前为止所做的:

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

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

这正确地将第二部电影添加到第一部电影中,但它当然不会以慢动作播放。我知道还有另一种方法insertSegmentOfMovie:fromRange:scaledToRange:,但我不知道如何在那里设置时间范围。

任何帮助,将不胜感激

更新:我可以使用以下代码添加慢动作:

[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];

这会以慢动作正确播放视频,但不会以慢动作保存第二个视频的音频。即第二个视频是慢动作,但它的音频不是!

更新 2:

需要明确的是,如果我尝试立即从我的可可应用程序在快速播放器中播放 originalVideo,则第二个视频的音频确实以慢动作播放。只有保存的文件的音频不同步。

我发布完整的代码以防万一:

- (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]];

    // 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)
}

更新 3:

如果我用 quicktime 以外的程序打开最终文件,比如 vlc,视频会在第一个视频的结尾停止播放(动作应该开始的地方),但音频会继续播放到最后。

更新 4:

如果我使用[originalMovie writeToFile:withAttributes]并指定一个新位置,一切都会按预期工作。只有当我尝试覆盖一个文件时,我才会遇到各种问题。

4

1 回答 1

0

好的,所以要以慢动作添加第二部电影,需要执行以下操作:

- (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].

于 2011-06-15T13:00:01.137 回答