Cocoa在视频结束时添加图像

时间:2011-06-21 11:48:01

标签: objective-c cocoa macos quicktime qtkit

我正在使用QTCaptureSession从iSight摄像头录制视频。

我想在视频的末尾添加一个图像,所以我实现了didFinishRecordingToOutputFileAtURL委托方法。这是我到目前为止所做的:

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

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

    NSImage *splashScreen = [NSImage imageNamed:@"video-ending.jpg"];

    NSImage *tiffImage = [[NSImage alloc] initWithData:[splashScreen TIFFRepresentation]];

    id attr = [NSDictionary dictionaryWithObjectsAndKeys:@"tiff",
               QTAddImageCodecType,
               [NSNumber numberWithLong:codecHighQuality], QTAddImageCodecQuality,
               nil];

    [originalMovie addImage:tiffImage forDuration:QTMakeTime(2, 1) withAttributes:attr];

    [tiffImage release];

    [originalMovie updateMovieFile];
}

这段代码的问题在于,虽然quicktime播放得很好,但其他播放器却没有。我确定我错过了一些基本的东西。

在保存之前将图像添加到视频中也是很酷的(在此期间避免两次)。这就是我现在停止录制的方式:

- (void)stopRecording
{   
    // It would be cool to add an image here
    [mCaptureMovieFileOutput recordToOutputFileURL:nil];
}

1 个答案:

答案 0 :(得分:1)

虽然我使用Cocoa touch,但这仍然适用。根据我将图像写入电影的经验,我有两个提示。首先,虽然我敢打赌addImage:forDuration负责AVAssetExportSessions没有做的很多事情,但我必须确保图像的定期添加次数比每秒几次,否则它们对所有玩家都不会很好。其次,如果有一个网络流媒体选项,例如AVAssetExportSession shouldOptimizeForNetworkUse,可以在电影中向前移动尽可能多的元数据和标题,我发现它使视频也与更多播放器兼容。