本程序是通过使用QT+VS2010调用windows摄像头,QCamera采集视频帧,通过QAudioInput采集音频,将采集的视频格式RGB32通过传递内存使用ffmpeg将RGB32
实时转换为yuv420p,并分别将视频与音频分别保存为yuv文件和pcm文件。
视频采集设置每一帧数据 myvideosurface.h
#ifndef MYVIDEOSURFACE_H
#define MYVIDEOSURFACE_H
#include <QObject>
#include <QCamera>
#include <QCameraViewfinder>
#include <QCameraImageCapture>
#include <QMediaRecorder>
#include <QAudioEncoderSettings>
#include <QAbstractVideoSurface>
#include <QVideoSurfaceFormat>
#include <QVideoFrame>
#include <QUrl>
#include <QAbstractVideoSurface>
#include <QVideoProbe>
#include <QCamera>
class CCaptureVideoFrame : public QAbstractVideoSurface
{
Q_OBJECT
public:
explicit CCaptureVideoFrame(QObject *parent = 0);
virtual QList<QVideoFrame::PixelFormat> supportedPixelFormats(
QAbstractVideoBuffer::HandleType handleType = QAbstractVideoBuffer::NoHandle) const;
bool setSource(QCamera *pCamera);
signals:
//视频帧捕获信号
void CaptureFrame(const QVideoFrame& frame);
public slots:
virtual bool present(const QVideoFrame &frame);
virtual bool start(const QVideoSurfaceFormat & format);
private:
QVideoProbe m_Probe;//android下,目前只能用probe捕获视频
};
#endif // MYVIDEOSURFACE_H
myvideosurface.cpp
#include <QCamera>
#include "myvideosurface.h"
CCaptureVideoFrame::CCaptureVideoFrame(QObject *parent) :
QAbstractVideoSurface(parent)
{
}
QList<QVideoFrame::PixelFormat> CCaptureVideoFrame::supportedPixelFormats(QAbstractVideoBuffer::HandleType handleType) const
{
Q_UNUSED(handleType);
QList<QVideoFrame::PixelFormat> lst;
//lst.push_back(QVideoFrame::Format_YUYV);//Qt现在不支持此格式,因为Qt内部用了QImage来处理视频帧。
lst.push_back(QVideoFrame::Format_RGB32);
lst.push_back(QVideoFrame::Format_BGR32);
return lst;
}
//捕获视频帧。windows下格式是RGB32;android下是NV21
bool CCaptureVideoFrame::present(const QVideoFrame &frame)
{
//frame.map(QAbstractVideoBuffer::ReadOnly);
QVideoSurfaceFormat format = surfaceFormat();
//qDebug("frameRate=%d,frameHeight=%d,frameSize=%d\n",format.frameRate(),format.frameHeight(),format.frameSize());
emit CaptureFrame(frame);
return true;
}
//根据不同的平台,设置捕获方式
bool CCaptureVideoFrame::setSource(QCamera *pCamera)
{
bool ret = true;
#ifdef ANDROID
//android下,目前只能用probe捕获视频
ret = m_Probe.setSource(pCamera);
if(ret)
{
connect(&m_Probe, SIGNAL(videoFrameProbed(QVideoFrame)),
SLOT(present(QVideoFrame)));
}
#else
//windows下,只能用下面方式捕获视频
pCamera->setViewfinder(this);
#endif
return ret;
}
bool CCaptureVideoFrame::start(const QVideoSurfaceFormat & format)
{
qDebug("frameRate=%f\n",format.frameRate());
return true;
}
音频采集主要代码:
QAudioFormat format;
// Set up the desired format, for example:
format.setSampleRate(44100);
format.setChannelCount(1);
format.setSampleSize(16);
format.setCodec("audio/pcm");
format.setByteOrder(QAudioFormat::LittleEndian);
format.setSampleType(QAudioFormat::SignedInt);
QAudioDeviceInfo info = QAudioDeviceInfo::defaultInputDevice();
if (!info.isFormatSupported(format)) {
qWarning() << "Default format not supported, trying to use the nearest.";
format = info.nearestFormat(format);
}
audio = new QAudioInput(format, this);
audio->start(&destinationFile);
博客内有完整工程代码。