我正在尝试以慢动作播放一部电影,并将其附加到现有的电影中。以下是我迄今为止所做的:
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:
需要明确的是,如果我试图在可可应用程序的快速播放器中播放原始视频,第二个视频的音频确实会以慢动作播放。只有保存的文件的音频不同步。
我发布了完整的代码,以防有帮助:
- (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]
并指定一个新位置,那么一切都会按预期进行。只有当我试图覆盖一个文件时,我才会遇到各种各样的问题。
好的,所以要添加第二部慢动作电影,需要执行以下操作:
- (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]];
}
但是,如果您试图覆盖originalMovie,则可能会出现各种错误,因此请使用[originalMovie writeToFile]
将其保存在新位置。