【问题标题】:Video duration is the double of what it should be视频时长是应有的两倍
【发布时间】:2018-12-25 09:15:21
【问题描述】:

我正在尝试记录我的电脑屏幕,因为我正在使用 Accord。

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

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

我在谷歌搜索并没有找到任何有用的东西。可能是由于发布的 alpha 版本。

我设法从 Accord 项目的一位维护者那里找到了一个使用此 3.8.2.0-alpha 版本的项目:https://github.com/cesarsouza/screencast-capture

我下载了 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 的两倍,这就是视频以慢速呈现的原因。

不幸的是,我不知道为什么会发生这种情况......有人有任何线索吗?

【问题讨论】:

    标签: c# ffmpeg accord.net


    【解决方案1】:

    这实际上是 alpha 版本中的一个错误。 See here for more information.

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

    避免法律问题的可能解决方法是仅降级到最后一个正式版本,因为那里不存在错误。

    【讨论】:

    • 您的第一条评论是错误的,我仍在从事这个项目(非常大):D 那篇文章实际上是我的。当时降级不是一个选项,因为我需要以 H264 格式录制。我最终使用 FFmpeg 实现了我自己的录像机,并使用管道传递了视频源。
    • 哦,好吧。如果您不介意,您会为我们指出您使用一些源代码的方向吗?大约两个星期以来,我们一直在努力寻找解决方案。我们正在尝试使用非 GPL 代码。
    • 当时我从这里得到了一些想法:github.com/MathewSachin/Captura 我记得它启动了一个 ffmpeg 进程并设置来自具有特定名称的管道的输入,然后在应用程序中,一个管道与创建相同的名称,然后将每个框架提供给它。创建 ffmpeg 进程时,您可以指定有关记录格式的所有内容。我认为该链接可以帮助您。
    • 好的,非常感谢!我想我们也会走这条路。
    猜你喜欢
    • 1970-01-01
    • 2018-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-04
    • 2017-07-16
    相关资源
    最近更新 更多