2828#include < QRegularExpression>
2929#include < Logger.h>
3030
31+ #include < cstdio>
32+
3133FfmpegJob::FfmpegJob (const QString &name, const QStringList &args, bool isOpenLog,
3234 QThread::Priority priority)
3335 : AbstractJob(name, priority)
34- , m_outputMsgRead(false )
35- , m_totalFrames(0 )
36+ , m_duration(0.0 )
3637 , m_previousPercent(0 )
3738 , m_isOpenLog(isOpenLog)
3839{
@@ -80,6 +81,17 @@ void FfmpegJob::onOpenTriggered()
8081 }
8182}
8283
84+ static double timeToSeconds (QString time)
85+ {
86+ int h, m, s, mil;
87+ const int ret = std::sscanf (time.toLatin1 ().constData (), " %d:%d:%d.%d" , &h, &m, &s, &mil);
88+ if (ret != 4 ) {
89+ LOG_ERROR () << " unable to parse time:" << time;
90+ return -1.0 ;
91+ }
92+ return (h * 60.0 * 60.0 ) + (m * 60.0 ) + s + (mil / 100.0 );
93+ }
94+
8395void FfmpegJob::onReadyRead ()
8496{
8597 QString msg;
@@ -88,34 +100,16 @@ void FfmpegJob::onReadyRead()
88100 if (!msg.startsWith (" frame=" ) && (!msg.trimmed ().isEmpty ())) {
89101 appendToLog (msg);
90102 }
91- if (msg.contains (" Duration:" )) {
92- m_duration = msg.mid (msg.indexOf (" Duration:" ) + 9 );
93- m_duration = m_duration.left (m_duration.indexOf (' ,' ));
103+ if (m_duration == 0 && msg.contains (" Duration:" )) {
104+ msg = msg.mid (msg.indexOf (" Duration:" ) + 9 );
105+ msg = msg.left (msg.indexOf (' ,' ));
106+ m_duration = timeToSeconds (msg);
94107 emit progressUpdated (m_item, 0 );
95- } else if (!m_outputMsgRead) {
96- // Wait for the "Output" then read the output fps to calculate number of frames.
97- if (msg.contains (" Output " )) {
98- m_outputMsgRead = true ;
99- }
100- }
101- if (!m_totalFrames && msg.contains (" fps" )) {
102- Mlt::Profile profile;
103- QRegularExpression re (" (\\ d+|\\ d+.\\ d+) fps" );
104- QRegularExpressionMatch match = re.match (msg);
105- if (match.hasMatch ()) {
106- QString fps = match.captured (1 );
107- profile.set_frame_rate (qRound (fps.toFloat () * 1000 ), 1000 );
108- } else {
109- profile.set_frame_rate (25 , 1 );
110- }
111- Mlt::Properties props;
112- props.set (" _profile" , profile.get_profile (), 0 );
113- m_totalFrames = props.time_to_frames (m_duration.toLatin1 ().constData ());
114- } else if (msg.startsWith (" frame=" ) && m_totalFrames > 0 ) {
115- msg = msg.mid (msg.indexOf (" frame=" ) + 6 );
116- msg = msg.left (msg.indexOf (" fps" ));
117- int frame = msg.toInt ();
118- int percent = qRound (frame * 100.0 / m_totalFrames);
108+ } else if (m_duration != 0 && msg.startsWith (" frame=" )) {
109+ msg = msg.mid (msg.indexOf (" time=" ) + 6 );
110+ msg = msg.left (msg.indexOf (" bitrate" ));
111+ double time = timeToSeconds (msg);
112+ int percent = qRound (time * 100.0 / m_duration);
119113 if (percent != m_previousPercent) {
120114 emit progressUpdated (m_item, percent);
121115 m_previousPercent = percent;
0 commit comments