视频时长是应有的两倍

时间:2018-07-17 15:32:01

标签: c# ffmpeg accord.net

我正在尝试记录计算机屏幕,因为我正在使用Accord。

我使用Accord 3.8.0.0的版本一切正常,但是当我升级到3.8.2.0-alpha版本时,我的录制视频变慢了,大约需要翻倍才能完成。

所以10秒的视频实际上需要20秒才能播放(在视频播放器中,视频的持续时间也是10秒)。

我在Google中搜索,但没有找到有用的东西。可能是由于该发行版的Alpha版本所致。

我设法从使用此3.8.2.0-alpha版本https://github.com/cesarsouza/screencast-capture

的Accord项目的维护者中找到了一个项目。

我下载了zip文件,解压缩了项目,构建了项目,修复了编译错误(将Dispose()更改为Stop()),然后运行了该应用程序,但是问题仍然存在...生成的视频文件仍然很慢...

我的3.8.2.0-alpha版本的代码的主要方法是这些(它们取自我下载的上一个项目):

public void StartRecording()
{
    if (IsRecording || !IsPlaying)
        return;

    int height = area.Height;
    int width = area.Width;
    Rational framerate = new Rational(1000, screenStream.FrameInterval);
    int videoBitRate = 1200 * 1000;

    OutputPath = Path.Combine(main.CurrentDirectory, fileName);
    RecordingStartTime = DateTime.MinValue;
    videoWriter = new VideoFileWriter();
    videoWriter.BitRate = videoBitRate;
    videoWriter.FrameRate = framerate;
    videoWriter.Width = width;
    videoWriter.Height = height;
    videoWriter.VideoCodec = VideoCodec.H264;
    videoWriter.VideoOptions["crf"] = "18";
    videoWriter.VideoOptions["preset"] = "veryfast";
    videoWriter.VideoOptions["tune"] = "zerolatency";
    videoWriter.VideoOptions["x264opts"] = "no-mbtree:sliced-threads:sync-lookahead=0";

    videoWriter.Open(OutputPath);

    HasRecorded = false;
    IsRecording = true;
}

void VideoPlayer_NewFrameReceived(object sender, Accord.Video.NewFrameEventArgs eventArgs)
{
    DateTime currentFrameTime = eventArgs.CaptureFinished;

    // Encode the last frame at the same time we prepare the new one
    Task.WaitAll(
        Task.Run(() =>
        {
            lock (syncObj) // Save the frame to the video file.
            {
                if (IsRecording)
                {
                    if (RecordingStartTime == DateTime.MinValue)
                        RecordingStartTime = DateTime.Now;

                    TimeSpan timestamp = currentFrameTime - RecordingStartTime;
                    if (timestamp > TimeSpan.Zero)
                        videoWriter.WriteVideoFrame(this.lastFrame, timestamp, this.lastFrameRegion);
                }
            }
        }),

        Task.Run(() =>
        {
            // Adjust the window according to the current capture
            // mode. Also adjusts to keep even widths and heights.
            CaptureRegion = AdjustWindow();

            // Crop the image if the mode requires it
            if (CaptureMode == CaptureRegionOption.Fixed ||
                CaptureMode == CaptureRegionOption.Window)
            {
                crop.Rectangle = CaptureRegion;

                eventArgs.Frame = croppedImage = crop.Apply(eventArgs.Frame, croppedImage);
                eventArgs.FrameSize = crop.Rectangle.Size;
            }

            //// Draw extra information on the screen
            bool captureMouse = Settings.Default.CaptureMouse;
            bool captureClick = Settings.Default.CaptureClick;
            bool captureKeys = Settings.Default.CaptureKeys;

            if (captureMouse || captureClick || captureKeys)
            {
                cursorCapture.CaptureRegion = CaptureRegion;
                clickCapture.CaptureRegion = CaptureRegion;
                keyCapture.Font = Settings.Default.KeyboardFont;

                using (Graphics g = Graphics.FromImage(eventArgs.Frame))
                {
                    g.CompositingQuality = CompositingQuality.HighSpeed;
                    g.SmoothingMode = SmoothingMode.HighSpeed;

                    float invWidth = 1; // / widthScale;
                    float invHeight = 1; // / heightScale;

                    if (captureMouse)
                        cursorCapture.Draw(g, invWidth, invHeight);

                    if (captureClick)
                        clickCapture.Draw(g, invWidth, invHeight);

                    if (captureKeys)
                        keyCapture.Draw(g, invWidth, invHeight);
                }
            }
        })
    );

    // Save the just processed frame and mark it to be encoded in the next iteration:
    lastFrame = eventArgs.Frame.Copy(lastFrame);

    lastFrameRegion = new Rectangle(0, 0, eventArgs.FrameSize.Width, eventArgs.Frame.Height);
}

任何人都知道导致这种速度下降的可能是什么问题吗?

编辑:

我想我找到了自己的问题:

pts:4.032000e+003   pts_time:0.252  dts:2.016000e+003   dts_time:0.126  duration:6.720000e+002  duration_time:0.042 stream_index:0
pts:6.720000e+003   pts_time:0.42   dts:3.360000e+003   dts_time:0.21   duration:6.720000e+002  duration_time:0.042 stream_index:0
pts:1.075200e+004   pts_time:0.672  dts:5.376000e+003   dts_time:0.336  duration:6.720000e+002  duration_time:0.042 stream_index:0
pts:1.344000e+004   pts_time:0.84   dts:6.720000e+003   dts_time:0.42   duration:6.720000e+002  duration_time:0.042 stream_index:0
pts:1.612800e+004   pts_time:1.008  dts:8.064000e+003   dts_time:0.504  duration:6.720000e+002  duration_time:0.042 stream_index:0
pts:1.881600e+004   pts_time:1.176  dts:9.408000e+003   dts_time:0.588  duration:6.720000e+002  duration_time:0.042 stream_index:0
pts:2.150400e+004   pts_time:1.344  dts:1.075200e+004   dts_time:0.672  duration:6.720000e+002  duration_time:0.042 stream_index:0
pts:2.553600e+004   pts_time:1.596  dts:1.276800e+004   dts_time:0.798  duration:6.720000e+002  duration_time:0.042 stream_index:0
pts:2.822400e+004   pts_time:1.764  dts:1.411200e+004   dts_time:0.882  duration:6.720000e+002  duration_time:0.042 stream_index:0
pts:3.091200e+004   pts_time:1.932  dts:1.545600e+004   dts_time:0.966  duration:6.720000e+002  duration_time:0.042 stream_index:0
pts:3.494400e+004   pts_time:2.184  dts:1.747200e+004   dts_time:1.092  duration:6.720000e+002  duration_time:0.042 stream_index:0
pts:3.897600e+004   pts_time:2.436  dts:1.948800e+004   dts_time:1.218  duration:6.720000e+002  duration_time:0.042 stream_index:0
pts:4.166400e+004   pts_time:2.604  dts:2.083200e+004   dts_time:1.302  duration:6.720000e+002  duration_time:0.042 stream_index:0
pts:4.704000e+004   pts_time:2.94   dts:2.352000e+004   dts_time:1.47   duration:6.720000e+002  duration_time:0.042 stream_index:0
pts:5.107200e+004   pts_time:3.192  dts:2.553600e+004   dts_time:1.596  duration:6.720000e+002  duration_time:0.042 stream_index:0

PTS始终是DTS的两倍,这就是为什么视频以慢速播放的原因。

不幸的是,我不知道为什么会这样...有人有任何线索吗?

1 个答案:

答案 0 :(得分:0)

这实际上是alpha发行版中的错误。 See here for more information.

您始终可以编辑源代码,并使用添加的更正内容构建项目,但是,如果我正确理解许可证,则可能需要释放用作LGPL的任何代码。但是请不要相信我,因为我不是律师。

可以避免法律问题的解决方法是仅将其降级为最新的正式版本,因为该错误不存在。