- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 libav
的经验几乎为零/FFMPEG
.我编写了这段代码来捕获屏幕并将其写入文件,我正面临一些问题。我正在使用 AV_CODEC_ID_MPEG4
起初编解码器,它工作得很好,但是非常快速的应用程序开始发送这样的垃圾邮件[dshow @ 02da1c80] real-time buffer [screen-capture-recorder] [video input] too full or near too full (64% of size: 128000000 [rtbufsize parameter])! frame dropped!
所以我google了一段时间,发现可能编码器太慢了,我需要把它改成更快的。所以我把它改成了AV_CODEC_ID_H264
.突然写入的文件变得不可读,并且应用程序开始发送垃圾邮件[libx264 @ 0455ff40] non-strictly-monotonic PTS
我到处找,我发现的只是一个建议把这两行
if(outPacket.pts != AV_NOPTS_VALUE) outPacket.pts = av_rescale_q(outPacket.pts, videoStream->codec->time_base, videoStream->time_base);
if(outPacket.dts != AV_NOPTS_VALUE) outPacket.dts = av_rescale_q(outPacket.dts, videoStream->codec->time_base, videoStream->time_base);
#include "MainWindow.h"
#include <QGuiApplication>
#include <QLabel>
#include <QScreen>
#include <QTimer>
#include <QLayout>
#include <QImage>
#include <QtConcurrent/QtConcurrent>
#include <QThreadPool>
#include <QVideoFrame>
#include "ScreenCapture.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
{
resize(800, 600);
label = new QLabel();
label->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
auto layout = new QHBoxLayout();
layout->addWidget(label);
auto widget = new QWidget();
widget->setLayout(layout);
setCentralWidget(widget);
connect(this, &MainWindow::imageReady, [=](QImage image) {label->setPixmap(QPixmap::fromImage(image).scaled(label->size(), Qt::KeepAspectRatio));});
init();
initOutFile();
collectFrames();
}
MainWindow::~MainWindow()
{
isRunning = false;
QThreadPool::globalInstance()->waitForDone();
avformat_close_input(&inputFormatContext);
avformat_free_context(inputFormatContext);
}
void MainWindow::init()
{
av_register_all();
avcodec_register_all();
avdevice_register_all();
auto screen = QGuiApplication::screens()[0];
QRect geometry = screen->geometry();
inputFormatContext = avformat_alloc_context();
// AVDictionary* options = NULL;
// av_dict_set(&options, "framerate", "30", NULL);
// av_dict_set(&options, "offset_x", QString::number(geometry.x()).toLatin1().data(), NULL);
// av_dict_set(&options, "offset_y", QString::number(geometry.y()).toLatin1().data(), NULL);
// av_dict_set(&options, "preset", "ultrafast", NULL);
// av_dict_set(&options, "probesize", "10MB", NULL);
// av_dict_set(&options, "pix_fmt", "yuv420p", NULL);
// av_dict_set(&options, "video_size", QString(QString::number(geometry.width()) + "x" + QString::number(geometry.height())).toLatin1().data(), NULL);
// AVInputFormat* inputFormat = av_find_input_format("gdigrab");
// avformat_open_input(&inputFormatContext, "desktop", inputFormat, &options);
QSettings settings("HKEY_CURRENT_USER\\Software\\screen-capture-recorder", QSettings::NativeFormat);
settings.setValue("start_x", geometry.x());
settings.setValue("start_y", geometry.y());
settings.setValue("capture_width", geometry.width());
settings.setValue("capture_height", geometry.height());
AVDictionary* options = NULL;
av_dict_set(&options, "preset", "ultrafast", NULL);
av_dict_set(&options, "vcodec", "h264", NULL);
av_dict_set(&options, "video_size", "1920x1080", NULL);
av_dict_set(&options, "crf", "0", NULL);
av_dict_set(&options, "tune", "zerolatency", NULL);
av_dict_set(&options, "rtbufsize", "128M", NULL);
AVInputFormat *format = av_find_input_format("dshow");
avformat_open_input(&inputFormatContext, "video=screen-capture-recorder", format, &options);
av_dict_free(&options);
avformat_find_stream_info(inputFormatContext, NULL);
videoStreamIndex = av_find_best_stream(inputFormatContext, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
AVStream* inStream = inputFormatContext->streams[videoStreamIndex];
inputCodec = avcodec_find_decoder(inStream->codecpar->codec_id);
if(!inputCodec) qDebug() << "Can't find input codec!";
inputCodecContext = avcodec_alloc_context3(inputCodec);
qDebug() << "IN_FORMAT" << av_get_pix_fmt_name(inStream->codec->pix_fmt);
avcodec_parameters_to_context(inputCodecContext, inStream->codecpar);
if(avcodec_open2(inputCodecContext, inputCodec, NULL)) qDebug() << "Can't open input codec!";
}
void MainWindow::initOutFile()
{
const char* filename = "C:/Temp/output.mp4";
if(avformat_alloc_output_context2(&outFormatContext, NULL, NULL, filename) < 0) qDebug() << "Can't create out context!";
outCodec = avcodec_find_encoder(AV_CODEC_ID_H264);
if(!outCodec) qDebug() << "Can't find codec!";
videoStream = avformat_new_stream(outFormatContext, outCodec);
videoStream->time_base = {1, 30};
const AVPixelFormat* pixelFormat = outCodec->pix_fmts;
while (*pixelFormat != AV_PIX_FMT_NONE)
{
qDebug() << "OUT_FORMAT" << av_get_pix_fmt_name(*pixelFormat);
++pixelFormat;
}
outCodecContext = videoStream->codec;
outCodecContext->bit_rate = 16000000;
outCodecContext->rc_max_rate = 0;
outCodecContext->rc_buffer_size = 0;
outCodecContext->qmin = 10;
outCodecContext->qmax = 51;
outCodecContext->qcompress = 0.6f;
outCodecContext->width = inputCodecContext->width;
outCodecContext->height = inputCodecContext->height;
outCodecContext->time_base = videoStream->time_base;
outCodecContext->gop_size = 10;
outCodecContext->max_b_frames = 1;
outCodecContext->pix_fmt = AV_PIX_FMT_YUV420P;
if (outFormatContext->oformat->flags & AVFMT_GLOBALHEADER) outCodecContext->flags |= CODEC_FLAG_GLOBAL_HEADER;
if(avcodec_open2(outCodecContext, outCodec, NULL)) qDebug() << "Can't open out codec!";
swsContext = sws_getContext(inputCodecContext->width,
inputCodecContext->height,
inputCodecContext->pix_fmt,
outCodecContext->width,
outCodecContext->height,
outCodecContext->pix_fmt,
SWS_BICUBIC, NULL, NULL, NULL);
if(avio_open(&outFormatContext->pb, filename, AVIO_FLAG_WRITE) < 0) qDebug() << "Can't open file!";
if(avformat_write_header(outFormatContext, NULL) < 0) qDebug() << "Can't write header!";
}
void MainWindow::collectFrames()
{
QtConcurrent::run([this](){
AVFrame* inFrame = av_frame_alloc();
inFrame->format = inputCodecContext->pix_fmt;
inFrame->width = inputCodecContext->width;
inFrame->height = inputCodecContext->height;
int size = av_image_alloc(inFrame->data, inFrame->linesize, inFrame->width, inFrame->height, inputCodecContext->pix_fmt, 1);
AVFrame* outFrame = av_frame_alloc();
outFrame->format = outCodecContext->pix_fmt;
outFrame->width = outCodecContext->width;
outFrame->height = outCodecContext->height;
av_image_alloc(outFrame->data, outFrame->linesize, outFrame->width, outFrame->height, outCodecContext->pix_fmt, 1);
AVPacket packet;
av_init_packet(&packet);
while(isRunning && (av_read_frame(inputFormatContext, &packet) >= 0))
{
if(packet.stream_index == videoStream->index)
{
//for gdigrab
// uint8_t* result = new uint8_t[inFrame->width * inFrame->height * 4];
// for (int i = 0; i < inFrame->height * inFrame->width * 4; i += 4)
// {
// result[i + 0] = packet.data[i + 2]; //B
// result[i + 1] = packet.data[i + 3]; //G
// result[i + 2] = packet.data[i + 0]; //R
// result[i + 3] = packet.data[i + 1]; //A
// }
// memcpy(inFrame->data[0], result, size);
// delete result;
QImage image(packet.data, inFrame->width, inFrame->height, QImage::Format_ARGB32);
QImage mirrored = image.mirrored(false, true);
emit imageReady(mirrored);
memcpy(inFrame->data[0], mirrored.bits(), size);
sws_scale(swsContext, inFrame->data, inFrame->linesize, 0, inputCodecContext->height, outFrame->data, outFrame->linesize);
av_packet_unref(&packet);
AVPacket outPacket;
av_init_packet(&outPacket);
int encodeResult = AVERROR(EAGAIN);
while(encodeResult == AVERROR(EAGAIN))
{
if(avcodec_send_frame(outCodecContext, outFrame)) qDebug() << "Send frame error!";
encodeResult = avcodec_receive_packet(outCodecContext, &outPacket);
}
if(encodeResult != 0) qDebug() << "Encoding error!" << encodeResult;
if(outPacket.pts != AV_NOPTS_VALUE) outPacket.pts = av_rescale_q(outPacket.pts, videoStream->codec->time_base, videoStream->time_base);
if(outPacket.dts != AV_NOPTS_VALUE) outPacket.dts = av_rescale_q(outPacket.dts, videoStream->codec->time_base, videoStream->time_base);
av_interleaved_write_frame(outFormatContext, &outPacket);
av_packet_unref(&outPacket);
}
}
av_freep(inFrame->data);
av_freep(outFrame->data);
av_write_trailer(outFormatContext);
avio_close(outFormatContext->pb);
});
}
最佳答案
您需要在调用 send_frame 之前设置 outFrame 的 pts。
关于c++ - 带有 H264 编解码器的 libav 错误消息。 "non-strictly-monotonic PTS",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49253180/
我正在使用 gmock 并模拟了一个函数 boost::beast::http::response_parser作为输出参数。功能签名看起来像: error_code readFromSocket(b
我的任务是打印由“非元音、元音、非元音”组成的单词列表,即 bab、bac、bad、bad ... 到 zuz。 我已经设法创建了一个代码,它执行前两个字母,但在最后一个循环中丢失并只打印'}' -
我正在尝试使用 label2rgb 生成 RGB 标签切片并使用它来更新 RGB 体积,如下所示: labelRGB_slice=label2rgb(handles.label(:,:,han
我有一个很奇怪的问题。我在 dll 中定义了一个接口(interface),如下所示: public interface IKreator2 { string Name { get; set;
在我的 openshift Drupal 托管中,网络都在 SSL 下 http://domain.com -> https://www.domain.com 确定 http://www.domain
我收到警告“退出构造函数时不可为空的事件‘SomeEvent’必须包含非空值。考虑将事件声明为可空。” 这是我的代码的一个非常简化的版本,它复制了完全相同的问题。我在这里错过了什么?这与 .Net 6
在一次大学考试中,我被要求测试一些 apache 簿记员类/方法,在这样做的过程中,我想在我的参数化测试中使用 mockito。没有 mockito 的测试工作正常但是当我尝试模拟接口(interfa
假设 A 列在 7 行中有以下值: 2 [空白的] 0 -0.3 0 [空白的] 0 如何获取范围(7 行)中非空/空白且不为零的最后一个值?因此,在这种情况下,正确答案是 -0.3。 最佳答案 =I
考虑以下受 this talk 启发的代码: template struct even_common_type_helper_impl; template struct even_common_typ
考虑这段代码, struct A {}; struct B { B(const A&) {} }; void f(B) { cout << "f()"<
考虑下面的类(class)。如果我对它运行 Findbugs,它会在第 5 行但不在第 7 行给我一个错误(“可序列化类中的非 transient 非可序列化实例字段”)。 1 public clas
我正在编写一个 python 脚本来计算 数据包丢失 通过使用 ping IP 地址linux 中的 subprocess 模块。 CSV 文件中保存了多个 IP 地址。当只给出可 ping 目的地时
我只是做文本更改,在文本之前它工作正常。请任何人都可以帮助我。 提前致谢 最佳答案 我已经解决了: ionic cordova 插件rmcordova-plugin-ionic-webview ion
我如何定义 在 persistence.xml 中? 我的项目在 Tomcat 6 和 Tomcat 7 中运行良好。 现在我正在使用 Struts 2 Spring 3.0.5 JPA 2 Jbos
我有一个 maven 仓库中不存在的第三方 jar,我们称它为“a.jar”,它也依赖于至少 20 多个第三方 jar,其中大部分不在 maven 中或者,我们称它们为“b.jar、c.jar、d.j
我已经浏览了各种线程很多小时(不夸张),但一直无法找到一种解决方案组合,使我能够将非 www 和 http 转发到 www 和 https,同时仍然能够查看 php 文件没有扩展名。如下是我的ngin
Scott Meyer 关于非成员函数增加封装并允许更优雅的设计(设计方面)的论点对我来说似乎非常有效。看这里:Article 但是我对此有疑问。 (似乎还有其他人,尤其是库开发人员,他们通常完全忽略
在对类设计的一些事实感到困惑时,特别是函数是否应该是成员,我查看了 Effective c++ 并找到了第 23 条,即 Prefer non-member non-friend functions
我正在尝试使用 firebase 云功能将通知发送到一个点半径的圆内的设备。我能够获取圈内设备的 ID,但无法获取 token ,使用 console.log(token) 打印时 token 为空。
我在我的项目中使用 React-ckeditor 5 包。我得到一个反序列化的 html 数据,我正在使用 React-html-parser 包将它解析成 html 模板,并将这个解析的数据传递给
我是一名优秀的程序员,十分优秀!