/*
Copyright (c) 2010 by Drake Justice <djustice.kde@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

*/

#include "videofile.h"
#include <QProcess>
#include <QDebug>
#include <QFileInfo>
#include <QMessageBox>

VideoFile::VideoFile(const QString& filename)
{
    m_filename = filename;
    m_process = new QProcess;
    m_process->setProcessChannelMode(QProcess::MergedChannels);
    m_process->start("ffmpeg -i \"" + m_filename + "\"");
    m_process->waitForFinished();
    QString out = m_process->readAll();
    foreach (QString line, out.split("\n")) {
        if (line.contains("Duration:")) {
            QString t = line.split(" ", QString::SkipEmptyParts).at(1);
            int hours = t.split(":", QString::SkipEmptyParts).at(0).toInt();
            int mins = t.split(":", QString::SkipEmptyParts).at(1).toInt();
            int secs = t.right(6).left(2).toInt();
            duration = (hours * 60 * 60) + (mins * 60) + secs;

            if (t.left(3) == "N/A") { duration = 0; }

        } else if (line.contains("this may be inaccurate")) {
            missingHeader = true;
        }
    }
}
