From a6299235d12eec3d87c42aca6e0cf0e656bf7286 Mon Sep 17 00:00:00 2001 From: Erik Zandvliet Date: Mon, 11 Mar 2013 14:43:47 +0100 Subject: [PATCH] Working conversion --- lib/converter.cpp | 93 +++++++++++++++++++++++++++-------------------- lib/converter.h | 1 + 2 files changed, 54 insertions(+), 40 deletions(-) diff --git a/lib/converter.cpp b/lib/converter.cpp index bc0e224b..e31dc65f 100644 --- a/lib/converter.cpp +++ b/lib/converter.cpp @@ -117,6 +117,16 @@ namespace Converter { statusHistory[name] = "Can not find encoder " + parameters["encoder"]; return; } + if (parameters.isMember("video")){ + if (parameters["video"].isMember("width") && !parameters["video"].isMember("height")){ + statusHistory[name] = "No height parameter given"; + return; + } + if (parameters["video"].isMember("height") && !parameters["video"].isMember("width")){ + statusHistory[name] = "No width parameter given"; + return; + } + } std::stringstream encoderCommand; if (parameters["encoder"] == "ffmpeg"){ encoderCommand << "ffmpeg -i "; @@ -134,6 +144,9 @@ namespace Converter { if (parameters["video"].isMember("kfps")){ encoderCommand << "-r " << parameters["video"]["kfps"].asInt() / 1000 << " "; } + if (parameters["video"].isMember("width")){ + encoderCommand << "-s " << parameters["video"]["width"].asInt() << "x" << parameters["video"]["height"].asInt() << " "; + } ///\todo Keyframe interval (different in older and newer versions of ffmpeg?) } }else{ @@ -175,14 +188,45 @@ namespace Converter { hasChanged = false; for (cIt = allConversions.begin(); cIt != allConversions.end(); cIt++){ if (Util::Procs::isActive(cIt->first)){ - continue; + int statusFD = dup(cIt->second["statusFD"].asInt()); + fsync( statusFD ); + FILE* statusFile = fdopen( statusFD, "r" ); + char * fileBuf = 0; + size_t fileBufLen = 0; + fseek(statusFile,0,SEEK_END); + std::string line; + int totalTime = 0; + do{ + getdelim(&fileBuf, &fileBufLen, '\r', statusFile); + line = fileBuf; + if (line.find("Duration") != std::string::npos){ + int curOffset = line.find("Duration: ") + 10; + totalTime += atoi(line.substr(curOffset, 2).c_str()) * 60 * 60 * 1000; + totalTime += atoi(line.substr(curOffset+3, 2).c_str()) * 60 * 1000; + totalTime += atoi(line.substr(curOffset+6, 2).c_str()) *1000; + totalTime += atoi(line.substr(curOffset+9, 2).c_str()) * 10; + cIt->second["duration"] = totalTime; + } + }while ( !feof(statusFile) && line.find("frame") != 0);//"frame" is the fist word on an actual status line of ffmpeg + if ( !feof(statusFile)){ + cIt->second["status"] = parseFFMpegStatus( line ); + cIt->second["status"]["duration"] = cIt->second["duration"]; + cIt->second["status"]["progress"] = (cIt->second["status"]["time"].asInt() * 100) / cIt->second["duration"].asInt(); + }else{ + line.erase(line.end()-1); + line = line.substr( line.rfind("\n") + 1 ); + cIt->second["status"] = line; + } + free(fileBuf); + fclose(statusFile); + }else{ + if (statusHistory.find( cIt->first ) == statusHistory.end()){ + statusHistory[cIt->first] = "Conversion successful, running DTSCFix"; + Util::Procs::Start(cIt->first+"DTSCFix",Util::getMyPath() + "MistDTSCFix " + cIt->second["output"].asString()); + } + allConversions.erase(cIt); + hasChanged = true; } - if (statusHistory.find( cIt->first ) == statusHistory.end()){ - statusHistory[cIt->first] = "Conversion successful, running DTSCFix"; - Util::Procs::Start(cIt->first+"DTSCFix",Util::getMyPath() + "MistDTSCFix " + cIt->second["output"].asString()); - } - allConversions.erase(cIt); - hasChanged = true; break; } } @@ -200,7 +244,7 @@ namespace Converter { } } - JSON::Value parseFFMpegStatus(std::string statusLine){ + JSON::Value Converter::parseFFMpegStatus(std::string statusLine){ JSON::Value result; int curOffset = statusLine.find("frame=") + 6; result["frame"] = atoi(statusLine.substr(curOffset, statusLine.find("fps=") - curOffset).c_str() ); @@ -219,38 +263,7 @@ namespace Converter { JSON::Value result; if (allConversions.size()){ for (std::map::iterator cIt = allConversions.begin(); cIt != allConversions.end(); cIt++){ - int statusFD = dup(cIt->second["statusFD"].asInt()); - fsync( statusFD ); - FILE* statusFile = fdopen( statusFD, "r" ); - char * fileBuf = 0; - size_t fileBufLen = 0; - fseek(statusFile,0,SEEK_END); - std::string line; - int totalTime = 0; - do{ - getdelim(&fileBuf, &fileBufLen, '\r', statusFile); - line = fileBuf; - if (line.find("Duration") != std::string::npos){ - int curOffset = line.find("Duration: ") + 10; - totalTime += atoi(line.substr(curOffset, 2).c_str()) * 60 * 60 * 1000; - totalTime += atoi(line.substr(curOffset+3, 2).c_str()) * 60 * 1000; - totalTime += atoi(line.substr(curOffset+6, 2).c_str()) *1000; - totalTime += atoi(line.substr(curOffset+9, 2).c_str()) * 10; - cIt->second["duration"] = totalTime; - } - }while ( !feof(statusFile) && line.find("frame") != 0);//"frame" is the fist word on an actual status line of ffmpeg - if ( !feof(statusFile)){ - result[cIt->first] = parseFFMpegStatus( line ); - result[cIt->first]["duration"] = cIt->second["duration"]; - result[cIt->first]["progress"] = (result[cIt->first]["time"].asInt() * 100) / cIt->second["duration"].asInt(); - }else{ - line.erase(line.end()-1); - line = line.substr( line.rfind("\n") + 1 ); - result[cIt->first] = line; - statusHistory[cIt->first] = line; - } - free(fileBuf); - fclose(statusFile); + result[cIt->first] = cIt->second["status"]; } } if (statusHistory.size()){ diff --git a/lib/converter.h b/lib/converter.h index 9f3d4f28..41a89170 100644 --- a/lib/converter.h +++ b/lib/converter.h @@ -17,6 +17,7 @@ namespace Converter { void updateStatus(); JSON::Value getStatus(); void clearStatus(); + JSON::Value parseFFMpegStatus(std::string statusLine); private: void fillFFMpegEncoders(); converterInfo allCodecs;