+void ProjectList::slotCreateProxy(const QString id)
+{
+ ProjectItem *item = getItemById(id);
+ if (!item || item->isProxyRunning() || item->referencedClip()->isPlaceHolder()) return;
+ QString path = item->referencedClip()->getProperty("proxy");
+ if (path.isEmpty()) {
+ setProxyStatus(item, PROXYCRASHED);
+ return;
+ }
+ setProxyStatus(item, PROXYWAITING);
+ if (m_abortProxy.contains(path)) m_abortProxy.removeAll(path);
+ if (m_processingProxy.contains(path)) {
+ // Proxy is already being generated
+ return;
+ }
+ if (QFileInfo(path).size() > 0) {
+ // Proxy already created
+ setProxyStatus(item, PROXYDONE);
+ slotGotProxy(path);
+ return;
+ }
+ m_processingProxy.append(path);
+
+ PROXYINFO info;
+ info.dest = path;
+ info.src = item->clipUrl().path();
+ info.type = item->clipType();
+ info.exif = QString(item->referencedClip()->producerProperty("_exif_orientation")).toInt();
+ m_proxyList.append(info);
+ if (!m_proxyThreads.futures().isEmpty()) {
+ // Remove inactive threads
+ QList <QFuture<void> > futures = m_proxyThreads.futures();
+ m_proxyThreads.clearFutures();
+ for (int i = 0; i < futures.count(); i++)
+ if (!futures.at(i).isFinished()) {
+ m_proxyThreads.addFuture(futures.at(i));
+ }
+ }
+ if (m_proxyThreads.futures().isEmpty() || m_proxyThreads.futures().count() < KdenliveSettings::proxythreads()) m_proxyThreads.addFuture(QtConcurrent::run(this, &ProjectList::slotGenerateProxy));
+}
+
+void ProjectList::slotAbortProxy(const QString id, const QString path)
+{
+ QTreeWidgetItemIterator it(m_listView);
+ ProjectItem *item = getItemById(id);
+ if (!path.isEmpty() && m_processingProxy.contains(path)) {
+ m_abortProxy << path;
+ setProxyStatus(item, NOPROXY);
+ }
+ else {
+ setProxyStatus(item, NOPROXY);
+ slotGotProxy(item);
+ }
+}
+
+void ProjectList::slotGenerateProxy()
+{
+ while (!m_proxyList.isEmpty() && !m_abortAllProxies) {
+ emit projectModified();
+ PROXYINFO info = m_proxyList.takeFirst();
+ if (m_abortProxy.contains(info.dest)) {
+ m_abortProxy.removeAll(info.dest);
+ continue;
+ }
+ // Get the list of clips that will need to get progress info
+ QTreeWidgetItemIterator it(m_listView);
+ QList <ProjectItem *> processingItems;
+ while (*it && !m_abortAllProxies) {
+ if ((*it)->type() == PROJECTCLIPTYPE) {
+ ProjectItem *item = static_cast <ProjectItem *>(*it);
+ if (item->referencedClip()->getProperty("proxy") == info.dest) {
+ processingItems.append(item);
+ }
+ }
+ ++it;
+ }
+
+ // Make sure proxy path is writable
+ QFile file(info.dest);
+ if (!file.open(QIODevice::WriteOnly)) {
+ for (int i = 0; i < processingItems.count(); i++)
+ setProxyStatus(processingItems.at(i), PROXYCRASHED);
+ m_processingProxy.removeAll(info.dest);
+ continue;
+ }
+ file.close();
+ QFile::remove(info.dest);
+
+ for (int i = 0; i < processingItems.count(); i++)
+ setProxyStatus(processingItems.at(i), CREATINGPROXY);
+
+ // Special case: playlist clips (.mlt or .kdenlive project files)
+ if (info.type == PLAYLIST) {
+ // change FFmpeg params to MLT format
+ QStringList parameters;
+ parameters << info.src;
+ parameters << "-consumer" << "avformat:" + info.dest;
+ QStringList params = m_doc->getDocumentProperty("proxyparams").simplified().split('-', QString::SkipEmptyParts);
+
+ foreach(QString s, params) {
+ s = s.simplified();
+ if (s.count(' ') == 0) {
+ s.append("=1");
+ }
+ else s.replace(' ', '=');
+ parameters << s;
+ }
+
+ parameters.append(QString("real_time=-%1").arg(KdenliveSettings::mltthreads()));
+
+ //TODO: currently, when rendering an xml file through melt, the display ration is lost, so we enforce it manualy
+ double display_ratio = KdenliveDoc::getDisplayRatio(info.src);
+ parameters << "aspect=" + QString::number(display_ratio);
+
+ //kDebug()<<"TRANSCOD: "<<parameters;
+ QProcess myProcess;
+ myProcess.setProcessChannelMode(QProcess::MergedChannels);
+ myProcess.start(KdenliveSettings::rendererpath(), parameters);
+ myProcess.waitForStarted();
+ int result = -1;
+ int duration = 0;
+ while (myProcess.state() != QProcess::NotRunning) {
+ // building proxy file
+ if (m_abortProxy.contains(info.dest) || m_abortAllProxies) {
+ myProcess.close();
+ myProcess.waitForFinished();
+ QFile::remove(info.dest);
+ m_abortProxy.removeAll(info.dest);
+ m_processingProxy.removeAll(info.dest);
+ for (int i = 0; i < processingItems.count(); i++)
+ setProxyStatus(processingItems.at(i), NOPROXY);
+ result = -2;
+ }
+ else {
+ QString log = QString(myProcess.readAll());
+ processLogInfo(processingItems, &duration, log);
+ }
+ myProcess.waitForFinished(500);
+ }
+ myProcess.waitForFinished();
+ m_processingProxy.removeAll(info.dest);
+ if (result == -1) result = myProcess.exitStatus();
+ if (result == 0) {
+ // proxy successfully created
+ for (int i = 0; i < processingItems.count(); i++)
+ setProxyStatus(processingItems.at(i), PROXYDONE);
+ slotGotProxy(info.dest);
+ }
+ else if (result == 1) {
+ // Proxy process crashed
+ QFile::remove(info.dest);
+ for (int i = 0; i < processingItems.count(); i++)
+ setProxyStatus(processingItems.at(i), PROXYCRASHED);
+ }
+ continue;
+ }
+
+ if (info.type == IMAGE) {
+ // Image proxy
+ QImage i(info.src);
+ if (i.isNull()) {
+ // Cannot load image
+ for (int i = 0; i < processingItems.count(); i++)
+ setProxyStatus(processingItems.at(i), PROXYCRASHED);
+ continue;
+ }
+ QImage proxy;
+ // Images are scaled to profile size.
+ //TODO: Make it be configurable?
+ if (i.width() > i.height()) proxy = i.scaledToWidth(m_render->frameRenderWidth());
+ else proxy = i.scaledToHeight(m_render->renderHeight());
+ if (info.exif > 1) {
+ // Rotate image according to exif data
+ QImage processed;
+ QMatrix matrix;
+
+ switch ( info.exif ) {
+ case 2:
+ matrix.scale( -1, 1 );
+ break;
+ case 3:
+ matrix.rotate( 180 );
+ break;
+ case 4:
+ matrix.scale( 1, -1 );
+ break;
+ case 5:
+ matrix.rotate( 270 );
+ matrix.scale( -1, 1 );
+ break;
+ case 6:
+ matrix.rotate( 90 );
+ break;
+ case 7:
+ matrix.rotate( 90 );
+ matrix.scale( -1, 1 );
+ break;
+ case 8:
+ matrix.rotate( 270 );
+ break;
+ }
+ processed = proxy.transformed( matrix );
+ processed.save(info.dest);
+ }
+ else proxy.save(info.dest);
+ for (int i = 0; i < processingItems.count(); i++)
+ setProxyStatus(processingItems.at(i), PROXYDONE);
+ slotGotProxy(info.dest);
+ m_abortProxy.removeAll(info.dest);
+ m_processingProxy.removeAll(info.dest);
+ continue;
+ }
+
+ QStringList parameters;
+ parameters << "-i" << info.src;
+ QString params = m_doc->getDocumentProperty("proxyparams").simplified();
+ foreach(QString s, params.split(' '))
+ parameters << s;
+
+ // Make sure we don't block when proxy file already exists
+ parameters << "-y";
+ parameters << info.dest;
+ QProcess myProcess;
+ myProcess.setProcessChannelMode(QProcess::MergedChannels);
+ myProcess.start("ffmpeg", parameters);
+ myProcess.waitForStarted();
+ int result = -1;
+ int duration = 0;
+
+ while (myProcess.state() != QProcess::NotRunning) {
+ // building proxy file
+ if (m_abortProxy.contains(info.dest) || m_abortAllProxies) {
+ myProcess.close();
+ myProcess.waitForFinished();
+ m_abortProxy.removeAll(info.dest);
+ m_processingProxy.removeAll(info.dest);
+ QFile::remove(info.dest);
+ if (!m_abortAllProxies) {
+ for (int i = 0; i < processingItems.count(); i++)
+ setProxyStatus(processingItems.at(i), NOPROXY);
+ }
+ else continue;
+ result = -2;
+ }
+ else {
+ QString log = QString(myProcess.readAll());
+ processLogInfo(processingItems, &duration, log);
+ }
+ myProcess.waitForFinished(500);
+ }
+ myProcess.waitForFinished();
+ m_abortProxy.removeAll(info.dest);
+ m_processingProxy.removeAll(info.dest);
+ if (result == -1) {
+ result = myProcess.exitStatus();
+ }
+
+ // FFmpeg process terminated normally, but make sure proxy clip exists
+ if (result != -2 && QFileInfo(info.dest).size() == 0) {
+ result = QProcess::CrashExit;
+ }
+
+ if (result == QProcess::NormalExit) {
+ // proxy successfully created
+ for (int i = 0; i < processingItems.count(); i++)
+ setProxyStatus(processingItems.at(i), PROXYDONE);
+ slotGotProxy(info.dest);
+ }
+ else if (result == QProcess::CrashExit) {
+ // Proxy process crashed
+ QFile::remove(info.dest);
+ for (int i = 0; i < processingItems.count(); i++)
+ setProxyStatus(processingItems.at(i), PROXYCRASHED);
+ }
+ }
+}
+
+
+void ProjectList::processLogInfo(QList <ProjectItem *>items, int *duration, const QString &log)
+{
+ int progress;
+ if (*duration == 0) {
+ if (log.contains("Duration:")) {
+ QString data = log.section("Duration:", 1, 1).section(',', 0, 0).simplified();
+ QStringList numbers = data.split(':');
+ *duration = (int) (numbers.at(0).toInt() * 3600 + numbers.at(1).toInt() * 60 + numbers.at(2).toDouble());
+ }
+ }
+ else if (log.contains("time=")) {
+ QString time = log.section("time=", 1, 1).simplified().section(' ', 0, 0);
+ if (time.contains(':')) {
+ QStringList numbers = time.split(':');
+ progress = numbers.at(0).toInt() * 3600 + numbers.at(1).toInt() * 60 + numbers.at(2).toDouble();
+ }
+ else progress = (int) time.toDouble();
+ for (int i = 0; i < items.count(); i++)
+ setProxyStatus(items.at(i), CREATINGPROXY, (int) (100.0 * progress / (*duration)));
+ }
+}
+