I'm using the wonderful [QtAV](https://github.com/wang-bin/QtAV/) package to perform video decoding. What I want to achieve is to obtain a thumbnail for a video. Here is what I have done so far.
bool saveThumb( QString videoFile ) {
AVDemuxer *demux = new AVDemuxer();
demux->setSeekUnit( SeekByFrame );
demux->setSeekType( KeyFrameSeek );
VideoDecoder *vdec = VideoDecoder::create( VideoDecoderId_FFmpeg );
demux->setMedia( videoFile );
qDebug() << "Loading file:" << demux->load();
qDebug() << "Seeking to 50%" << demux->seek( 0.5 );
qDebug() << "Reading frame:" << demux->readFrame();
vdec->setCodecContext( demux->videoCodecContext() );
vdec->open();
Packet pkt = demux->packet();
qDebug() << "Packet valid:" << pkt.isValid();
qDebug() << "Decoding packet:" << vdec->decode( pkt );
VideoFrame frame = vdec->frame();
qDebug() << "Valid frame:" << frame.isValid();
QImage img = frame.toImage();
qDebug() << "Valid image:" << ( not img.isNull() );
bool saved = img.save( videoFile + ".jpg" );
return saved;
}
My problem is that frame.isValid() always returns false, no matter to where I seek, or which video I play. All the checks above return true.
I would like to add that if I use AVPlayer and play the video, the video renders properly, however there is no audio playing.
Also I am able to capture snapshots using AVPlayer::videoCapture()->capture()
For the record, I have tried this using both Qt4 and Qt5
For getting thumbnails of videos you can simply use QtAV VideoFrameExtractor class like:
#include "QtAV/VideoFrameExtractor.h"
...
bool saveThumb(const QString& videoFile) {
auto extractor = new QtAV::VideoFrameExtractor;
connect(
extractor,
&QtAV::VideoFrameExtractor::frameExtracted,
extractor,
[this, extractor, videoFile](const QtAV::VideoFrame& frame) {
const auto& img = frame.toImage();
auto saved = img.save(videoFile + ".jpg" );
extractor->deleteLater();
});
connect(
extractor,
&QtAV::VideoFrameExtractor::error,
extractor,
[this, extractor](const QString& errorStr) {
qDebug() << errorStr;
extractor->deleteLater();
});
extractor->setAsync(true);
extractor->setSource(videoFile);
extractor->setPosition(1);
}
Related
I am working on content migration application. I have to migrate contacts, calendars, media from Blackberry device to Android device. Contacts and Calendars I have done.
I used below snip of code for Contacts
ContactService contactService;
ContactListFilters filters;
filters.setLimit(0);
QList<Contact> contactList = contactService.contacts(filters);
And below for Calendars
CalendarService calendarService;
EventSearchParameters searchParams;
searchParams.setStart(QDateTime(QDate(1918, 01, 01), QTime(00,00,00)));
searchParams.setEnd(QDateTime(QDate(2118, 12, 31), QTime(00,00,00)));
QList<CalendarEvent> eventList = calendarService.events(searchParams);
Its working fine.
Now, I have to lookup media in device i.e get media path based on type
say all Image, all Audio and all Video present in device.
Then with those path have to create a output stream and send it to
destination.
I've heard you can query the media SQL database available on every device, but I've never done it myself so I can't help on that one. The db file is located at /db/mmlibrary.db for media files stored on device and at /db/mmlibrary_SD.db for media files stored on SD card.
Otherwise, you can recursively navigate through the device and keep a global list of file paths. Note that doing so can take a long time, for my personal device it took 25 seconds to recursively go through all folders and find 186 audio files, 5127 picture files and 28 video files. You might want to execute this code in a separate thread to avoid blocking UI.
#include "applicationui.hpp"
#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include <QFileInfo>
#include <QDir>
using namespace bb::cascades;
const QStringList audioFileExtensions = QStringList() << "mp3" << "wav";
const QStringList pictureFileExtensions = QStringList() << "bmp" << "gif" << "ico" << "jpg" << "jpeg" << "png" << "tiff";
const QStringList videoFileExtensions = QStringList() << "avi" << "mkv" << "mp4" << "mpeg";
ApplicationUI::ApplicationUI() :
QObject()
{
QmlDocument *qml = QmlDocument::create("asset:///main.qml").parent(this);
qml->setContextProperty("_app", this);
AbstractPane *root = qml->createRootObject<AbstractPane>();
Application::instance()->setScene(root);
}
//Declared as public Q_INVOKABLE in hpp
void ApplicationUI::findMediaFiles(QString parentFolder) {
QDateTime start = QDateTime::currentDateTime();
qDebug() << "findMediaFiles() started in" << parentFolder;
//Those 3 QStringList are declared as private variables in hpp
audioFilePaths.clear();
pictureFilePaths.clear();
videoFilePaths.clear();
if (parentFolder.isEmpty()) {
parentFolder = QString(getenv("PERIMETER_HOME")) + "/shared";
}
findMediaFilesRecursively(parentFolder);
qDebug() << audioFilePaths.size() << audioFilePaths;
qDebug() << pictureFilePaths.size() << pictureFilePaths;
qDebug() << videoFilePaths.size() << videoFilePaths;
qDebug() << "Took" << start.secsTo(QDateTime::currentDateTime()) << "seconds";
}
//Declared as private in hpp
void ApplicationUI::findMediaFilesRecursively(QString parentFolder) {
QDir dir(parentFolder);
dir.setFilter(QDir::Dirs | QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot | QDir::NoSymLinks);
dir.setSorting(QDir::DirsFirst);
QFileInfoList fileInfoList = dir.entryInfoList();
foreach(QFileInfo fileInfo, fileInfoList) {
if (fileInfo.isDir()) {
findMediaFilesRecursively(fileInfo.absoluteFilePath());
continue;
}
QString extension = fileInfo.fileName().split(".").last();
if (audioFileExtensions.contains(extension, Qt::CaseInsensitive)) {
audioFilePaths.append(fileInfo.absoluteFilePath());
}
else if (pictureFileExtensions.contains(extension, Qt::CaseInsensitive)) {
pictureFilePaths.append(fileInfo.absoluteFilePath());
}
else if (videoFileExtensions.contains(extension, Qt::CaseInsensitive)) {
videoFilePaths.append(fileInfo.absoluteFilePath());
}
}
}
I want to use QSettings to save my window's dimensions so I came up with these two functions to save & load the settings:
void MainWindow::loadSettings()
{
settings = new QSettings("Nothing","KTerminal");
int MainWidth = settings->value("MainWidth").toInt();
int MainHeight = settings->value("MainHeight").toInt();
std::cout << "loadSettings " << MainWidth << "x" << MainHeight << std::endl;
std::cout << "file: " << settings->fileName().toLatin1().data() << std::endl;
if (MainWidth && MainHeight)
this->resize(MainWidth,MainHeight);
else
this->resize(1300, 840);
}
void MainWindow::saveSettings()
{
int MainHeight = this->size().height();
int MainWidth = this->size().width();
std::cout << "file: " << settings->fileName().toLatin1().data() << std::endl;
std::cout << "saveSettings " << MainWidth << "x" << MainHeight << std::endl;
settings->setValue("MainHeight",MainHeight);
settings->setValue("MainWidth",MainWidth);
}
Now, I can see the demensions being extracted in saveSettings as expected but no file gets created and hence loadSettings will always load 0 only. Why is this?
QSettings isn't normally instantiated on the heap. To achieve the desired effect that you are looking for, follow the Application Example and how it is shown in the QSettings documentation.
void MainWindow::readSettings()
{
QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName());
const QByteArray geometry = settings.value("geometry", QByteArray()).toByteArray();
if (geometry.isEmpty()) {
const QRect availableGeometry = QApplication::desktop()->availableGeometry(this);
resize(availableGeometry.width() / 3, availableGeometry.height() / 2);
move((availableGeometry.width() - width()) / 2,
(availableGeometry.height() - height()) / 2);
} else {
restoreGeometry(geometry);
}
}
void MainWindow::writeSettings()
{
QSettings settings(QCoreApplication::organizationName(), QCoreApplication::applicationName());
settings.setValue("geometry", saveGeometry());
}
Also note the use of saveGeometry() and restoreGeometry(). Other similarly useful functions for QWidget based GUIs are saveState() and restoreState() (not shown in the above example).
I strongly recommend the zero parameter constructor of QSettings, and to setup the defaults in your main.cpp, like so:
QSettings::setDefaultFormat(QSettings::IniFormat); // personal preference
qApp->setOrganizationName("Moose Soft");
qApp->setApplicationName("Facturo-Pro");
Then when you want to use QSettings in any part of your application, you simply do:
QSettings settings;
settings.setValue("Category/name", value);
// or
QString name_str = settings.value("Category/name", default_value).toString();
QSettings in general is highly optimized, and works really well.
Hope that helps.
Some other places where I've talked up usage of QSettings:
Using QSettings in a global static class
https://stackoverflow.com/a/14365937/999943
I'm reimplemented the present method from a QAbstractVideo Surface in order to capture frames from an IP camera.
This is my reimplemented methods (the required ones):
QList<QVideoFrame::PixelFormat> CameraFrameGrabber::supportedPixelFormats(QAbstractVideoBuffer::HandleType handleType) const
{
Q_UNUSED(handleType);
return QList<QVideoFrame::PixelFormat>()
<< QVideoFrame::Format_ARGB32
<< QVideoFrame::Format_ARGB32_Premultiplied
<< QVideoFrame::Format_RGB32
<< QVideoFrame::Format_RGB24
<< QVideoFrame::Format_RGB565
<< QVideoFrame::Format_RGB555
<< QVideoFrame::Format_ARGB8565_Premultiplied
<< QVideoFrame::Format_BGRA32
<< QVideoFrame::Format_BGRA32_Premultiplied
<< QVideoFrame::Format_BGR32
<< QVideoFrame::Format_BGR24
<< QVideoFrame::Format_BGR565
<< QVideoFrame::Format_BGR555
<< QVideoFrame::Format_BGRA5658_Premultiplied
<< QVideoFrame::Format_AYUV444
<< QVideoFrame::Format_AYUV444_Premultiplied
<< QVideoFrame::Format_YUV444
<< QVideoFrame::Format_YUV420P
<< QVideoFrame::Format_YV12
<< QVideoFrame::Format_UYVY
<< QVideoFrame::Format_YUYV
<< QVideoFrame::Format_NV12
<< QVideoFrame::Format_NV21
<< QVideoFrame::Format_IMC1
<< QVideoFrame::Format_IMC2
<< QVideoFrame::Format_IMC3
<< QVideoFrame::Format_IMC4
<< QVideoFrame::Format_Y8
<< QVideoFrame::Format_Y16
<< QVideoFrame::Format_Jpeg
<< QVideoFrame::Format_CameraRaw
<< QVideoFrame::Format_AdobeDng;
}
bool CameraFrameGrabber::present(const QVideoFrame &frame)
{
//qWarning() << "A frame";
if (frame.isValid()) {
//qWarning() << "Valid Frame";
QVideoFrame cloneFrame(frame);
cloneFrame.map(QAbstractVideoBuffer::ReadOnly);
const QImage image(cloneFrame.bits(),
cloneFrame.width(),
cloneFrame.height(),
QVideoFrame::imageFormatFromPixelFormat(cloneFrame .pixelFormat()));
qWarning() << "Is created image NULL?" << image.isNull();
if (!image.isNull())
emit nextFrameAsImage(image);
cloneFrame.unmap();
return true;
}
return false;
}
And this is is how I used it:
grabber = new CameraFrameGrabber(this);
connect(grabber,&CameraFrameGrabber::nextFrameAsImage,this,&QCmaraTest::on_newFrame);
QMediaPlayer *a = new QMediaPlayer(this);
QString url = "http://Admin:1234#10.255.255.67:8008";
a->setMedia(QUrl(url));
a->setVideoOutput(grabber);
a->play();
The problem is that the image that is created is null. As far as I can tell, this can only be because the frame is valid but does not contain data.
Any ideas what the problem could be?
Important Detail: If I set the stream to a QVideoWidget and simply show that, it works just fine.
So I found out what the problem was.
This:
QVideoFrame::imageFormatFromPixelFormat(cloneFrame .pixelFormat())
Was returning invalid format because the IP cam gave the format as a YUV format which QImage can't handle. The solution was to force the format and the only one I found that did not make the program crash was: QImage::Format_Grayscale8.
With that, it worked.
I'm relatively new to Qt, but I have done a little searching around. I have a base class that handles UDP broadcasting, and does the connect statements in the constructor of the class like this:
NetworkConnection::NetworkConnection(QObject *parent)
: QObject(parent) // Based on QObject
, m_server_search( new QUdpSocket ) // Our UDP Broadcast socket
, m_waiting_for_server( false )
, m_found_server( false )
{
qDebug() << "NetworkConnection::constructor";
connect( m_server_search, SIGNAL(readyRead()), this, SLOT(serverResponse()), Qt::UniqueConnection );
if ( m_server_search->bind( QHostAddress::AnyIPv4, (quint16)PORT_MULTICAST, QUdpSocket::ShareAddress ) )
{
if ( m_server_search->joinMulticastGroup( QHostAddress( MULTICAST_GROUP ) ) )
{
connect( this, SIGNAL(broadcast(NetworkMessage)), this, SLOT(broadcast_message(NetworkMessage)), Qt::UniqueConnection );
this->m_ping_timer = this->startTimer(2000);
qDebug() << "Ping timer id=" << this->m_ping_timer;
} else qDebug() << "Couldn't start multicast listener";
} else qDebug() << "Couldn't bind multicast to port" << PORT_MULTICAST;
}
I set up a signal/slot interface for broadcasting:
signals:
void serverFound();
void serverNotFound();
void broadcast(NetworkMessage);
private slots:
void serverResponse();
void broadcast_message( NetworkMessage msg );
And broadcast_message looks like this:
void NetworkConnection::broadcast_message( NetworkMessage msg )
{
QByteArray raw = msg.toString();
qDebug() << "NetworkConnection::broadcast_message>" << raw;
if ( m_server_search->writeDatagram( raw.data(), raw.size(), QHostAddress(MULTICAST_GROUP), (quint16)PORT_MULTICAST ) < 1 ) qDebug() << "Failed broadcast last message";
}
My timer works well, and here is the code:
void NetworkConnection::timerEvent(QTimerEvent *event)
{
qDebug() << "NetworkConnection::timerEvent with id" << event->timerId() << "(ping timer=" << this->m_ping_timer << ")";
if ( event->timerId() == this->m_ping_timer )
{
qDebug() << "NetworkConnection::pingForServer";
if ( m_waiting_for_server && !m_found_server )
{
qDebug() << "Server not found!";
emit this->serverNotFound();
return;
}
if ( !m_found_server )
{
qDebug() << "Sending a ping to the server";
NetworkMessage msg( m_software_guid, get_microseconds(), QString("whoisaserver") );
emit this->broadcast( msg );
m_waiting_for_server = true;
m_found_server = false;
}
}
}
I only get the text "Sending a pint to the server" once, but my broadcast_message outputs it's qDebug() multiple times.
I'm not explicitly using multiple threads, and as you can see I'm using Qt::UniqueConnection, which is apparently having no affect?
SO why would the slot be called multiple times? I've even tried debugging it a little and just calling this->broadcast( ... ) without using emit, and it still gets called multiple times.
Edit: I just added a counter to the broadcast_message slot, and it gets called 340 times. Is there any significance to that?
I am writing a class to parse Itunes Libray File using QXmlQuery and QT-XSLT.
Here's my sample code:
ItunesLibParser::ItunesLibParser()
{
pathToLib = QString("/Users/rakesh/temp/itunes_xslt/itunes_music_library.xml");
}
void ItunesLibParser::createXSLFile(QFile &inFile)
{
if (inFile.exists()) {
inFile.remove();
}
inFile.open(QIODevice::WriteOnly);
QTextStream out(&inFile);
out << QString("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
out << QString("<xsl:stylesheet version=\"2.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">");
out << QString("<xsl:output method=\"text\" />");
out << QString("<xsl:template name=\"playlistNames\">");
out << QString("<xsl:value-of select=\"child::integer[preceding-sibling::key[1]='Playlist ID']\"/>");
out << QString("<xsl:text>
</xsl:text>");
out << QString("<xsl:value-of select=\"child::string[preceding-sibling::key[1]='Name']\"/>");
out << QString("<xsl:text>
</xsl:text>");
out << QString("</xsl:template>");
out << QString("<xsl:template match=\"/\">");
out << QString("<xsl:for-each select=\"plist/dict/array/dict\">");
out << QString("<xsl:call-template name=\"playlistNames\"/>");
out << QString("</xsl:for-each>");
out << QString("</xsl:template>");
out << QString("</xsl:stylesheet>");
inFile.close();
return;
}
void ItunesLibParser::dumpPlayList()
{
QXmlQuery query(QXmlQuery::XSLT20);
query.setFocus(QUrl(pathToLib));
QFile xslFile("plist.xsl");
createXSLFile(xslFile);
query.setQuery(QUrl("plist.xsl"));
QStringList* outDump = new QStringList();
query.evaluateTo(outDump);
if(outDump != NULL) {
QStringList::iterator iter = (*outDump).begin();
for (; iter != (*outDump).end();
++iter)
//code flow doesn't come here. It means being() == end()
std::cout << (*iter).toLocal8Bit().constData() << std::endl;
}
return;
}
OutDump here doesn't contain data. While in Shell (xmlpatterns-4.7 mystlye.xsl itunes_music_library.xml ), If I run my Query I get proper output.
Is there anything, wrong I am doing while calling it programatically? I checked out plist.xsl is created properly, but my doubt is whether "/Users/rakesh/temp/itunes_xslt/itunes_music_library.xml" this is getting loaded or not? Or there might be another reasons, I am confused. Is there any experts to throw some light onto problem, I will be glad.
Intead from reading from the file, I read the file into buffer and converted that int string as passed to setquery. That solved the problem.
Here's sample code for those who could face similar problem in future.
void ITunesMlibParser::parsePlayListItemXml(int plistId)
{
QXmlQuery xQuery(QXmlQuery::XSLT20);
QFile inFile("/Users/rakesh/temp/itunes_xslt/itunes_music_library.xml");
if (!inFile.open(QIODevice::ReadOnly)) {
return;
}
QByteArray bArray;
while (!inFile.atEnd()) {
bArray += inFile.readLine();
}
QBuffer xOriginalContent(&bArray);
xOriginalContent.open(QBuffer::ReadWrite);
xOriginalContent.reset();
if (xQuery.setFocus(&xOriginalContent))
std::cout << "File Loaded" << std::endl;
//..
//..
}
Thanks
Rakesh