QTKit,捕获视频以进行直播

发布于 2024-12-09 09:45:03 字数 1915 浏览 0 评论 0原文

我正在尝试为 Mac 创建一个可以创建实时视频流的应用程序。我知道 VLC 和其他解决方案,但仍然如此。

为此,我尝试使用 QTKit 从 iSight 录制视频,并将其连续保存为一系列小视频文件。然而,录音结果并不完全连续,文件之间存在间隙。

基本上,我只是设置一个计时器,以特定的时间间隔开始录制到新文件,从而停止旧的录制。我还尝试设置最大记录长度,并使用委托方法 ...didFinishRecording... 和 ...willFinishRecording...,但结果相同(我无法真正估计这些情况下间隙之间的差异)。

如果您知道应该如何完成这些事情,请帮助我。

这是我当前的代码:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    QTCaptureSession *session = [[QTCaptureSession alloc] init];
    QTCaptureDevice *iSight = [QTCaptureDevice defaultInputDeviceWithMediaType:QTMediaTypeVideo];
    [iSight open:nil];
    QTCaptureDeviceInput *myInput = [QTCaptureDeviceInput deviceInputWithDevice:iSight];
    output = [[QTCaptureMovieFileOutput alloc] init] ; //ivar, QTCaptureFileOutput
    [output setDelegate:self];
    a = 0; //ivar, int
    fileName = @"/Users/dtv/filerecording_"; //ivar, NSString
    [session addOutput:output error:nil];
    [session addInput:myInput error:nil];
    [capview setCaptureSession:session]; //IBOutlet
    [session startRunning]; 
    [output setCompressionOptions:[QTCompressionOptions compressionOptionsWithIdentifier:@"QTCompressionOptionsSD480SizeH264Video"] forConnection:[[output connections] objectAtIndex:0]];
    [output recordToOutputFileURL:[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@%i.mov", fileName, a]] bufferDestination:QTCaptureFileOutputBufferDestinationOldFile];
    NSTimer *tmr = [NSTimer timerWithTimeInterval:5 target:self selector:@selector(getMovieLength:) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:tmr forMode:NSDefaultRunLoopMode];
}

‐ (void) getMovieLength:(NSTimer *) t { 一个++; [输出 recordToOutputFileURL:[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@%i.mov", fileName, a]] bufferDestination:QTCaptureFileOutputBufferDestinationOldFile]; }

I am trying to create an application for the Mac that would create live video streaming. I know about VLC and other solutions, but still.

To that end i am trying to record video from iSight using QTKit, and save it continuously as a series of tiny video files. However, the recording turns out not quite continuous, with gaps between the files.

Basically, I am just setting up a timer, that starts recording to a new file at certain time intervals, thus stopping the old recording. I also tried setting the max recorded length, and using a delegate method ...didFinishRecording... and ...willFinishRecording..., but with the same result (i can't really estimate the difference between the gaps in these cases).

Please, help me, if you know how these things should be done.

Here is my current code:

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    QTCaptureSession *session = [[QTCaptureSession alloc] init];
    QTCaptureDevice *iSight = [QTCaptureDevice defaultInputDeviceWithMediaType:QTMediaTypeVideo];
    [iSight open:nil];
    QTCaptureDeviceInput *myInput = [QTCaptureDeviceInput deviceInputWithDevice:iSight];
    output = [[QTCaptureMovieFileOutput alloc] init] ; //ivar, QTCaptureFileOutput
    [output setDelegate:self];
    a = 0; //ivar, int
    fileName = @"/Users/dtv/filerecording_"; //ivar, NSString
    [session addOutput:output error:nil];
    [session addInput:myInput error:nil];
    [capview setCaptureSession:session]; //IBOutlet
    [session startRunning]; 
    [output setCompressionOptions:[QTCompressionOptions compressionOptionsWithIdentifier:@"QTCompressionOptionsSD480SizeH264Video"] forConnection:[[output connections] objectAtIndex:0]];
    [output recordToOutputFileURL:[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@%i.mov", fileName, a]] bufferDestination:QTCaptureFileOutputBufferDestinationOldFile];
    NSTimer *tmr = [NSTimer timerWithTimeInterval:5 target:self selector:@selector(getMovieLength:) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:tmr forMode:NSDefaultRunLoopMode];
}

‐ (void) getMovieLength:(NSTimer *) t { a++; [output recordToOutputFileURL:[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@%i.mov", fileName, a]] bufferDestination:QTCaptureFileOutputBufferDestinationOldFile]; }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

欲拥i 2024-12-16 09:45:03

有一种本机机制可以将捕获的电影分成碎片。用于

[QTCaptureFileOutput setMaximumRecordedDuration:]

指定片段的持续时间或

[QTCaptureFileOutput setMaximumRecordedFileSize:]

指定文件大小限制。

当达到限制时,将调用委托方法:

[QTCaptureFileOutput_Delegate captureOutput: shouldChangeOutputFileAtURL: forConnections: dueToError:]

在该方法中,您可以设置新文件名:

[QTCaptureFileOutput recordToOutputFileURL:]

这将允许您非常精确地剪切录制的电影的片段。

请注意,[QTCaptureFileOutput_Delegate captureOutput: didFinishRecordingToOutputFileAtURL: forConnections: dueToError:] 将在重新编码到文件中实际完成后稍后调用。如果您使用此方法设置新文件,最终视频中将会出现间隙。但这并不意味着您不需要使用此方法。此方法将指示电影片段何时可​​供使用。

如果您需要更精确的剪切,您可以指定

[QTCaptureFileOutput captureOutput: didOutputSampleBuffer: fromConnection:]

何时开始录制到新片段中的确切电影帧。但是,您需要更具体的知识才能使用该方法。

There is a native mechanism to brake the captured movie into pieces. Use

[QTCaptureFileOutput setMaximumRecordedDuration:]

to specify the duration of the piece or

[QTCaptureFileOutput setMaximumRecordedFileSize:]

to specify the file size limit.

When the limit is reached the delegate method will be called:

[QTCaptureFileOutput_Delegate captureOutput: shouldChangeOutputFileAtURL: forConnections: dueToError:]

In the this method you can set the new file name:

[QTCaptureFileOutput recordToOutputFileURL:]

This will allow you to cut the pieces of the recorded movie pretty precisely.

Note, that [QTCaptureFileOutput_Delegate captureOutput: didFinishRecordingToOutputFileAtURL: forConnections: dueToError:] will be called a bit later after the recoding into the file has been actually finished. If you use this method to set the new file you will have gaps in the final video. It does not mean you do not need to use this method though. This method will indicated when the piece of the movie is ready to be used.

If you need even more precise cutting you can use

[QTCaptureFileOutput captureOutput: didOutputSampleBuffer: fromConnection:]

to specify the exact movie frame when to start recording into a new piece. However, you will need more specific knowledge to work with the method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文