You've already forked libopenshot
mirror of
https://github.com/OpenShot/libopenshot.git
synced 2026-03-02 08:53:52 -08:00
Improved Profile Class (Helper methods, Sortable, Unit tests) (#895)
* Removing legacy profile property. Add new operators for Profile classes (for comparison). Also added new functions to generate different variations of the Profile data (key, short name, long name, long name w/description). * Add empty constructor for Profile class, and new Profile unit tets * Adding zero padding to profile Key function, for easier sorting: 01920x1080i2997_16:09 * Clear setfill flag after creating Key() output * Updating example exe to load an *.osp project file via C++, which makes debugging complex broken projects much easier. * - Add new unit test to FFmpegWriter to create an animated GIF and verify it can be wrapped with a FrameMapper (with no audio track) - Improve FrameMapper to ignore missing audio data (i.e. when no audio samples present, don't try and find them or resample them) * Fix some whitespace issues * Fix inline documentation mistype * Fixed missing reuse licensing on new example profile files * Changing Profile::Key() format to exclude the : character, since Windows file names cannot contain that * - Large memory leak fixed in FFmpegWriter when closing the video & audio contexts - Reducing # of cached frames and rescalers to 1, since we no longer use OMP and this is unneeded - we need to refactor much of this code out eventually * - Fixing whitespace issues - Code clean-up / line wrapping / etc...
This commit is contained in:
112
src/Profiles.cpp
112
src/Profiles.cpp
@@ -10,11 +10,27 @@
|
||||
//
|
||||
// SPDX-License-Identifier: LGPL-3.0-or-later
|
||||
|
||||
#include <iomanip>
|
||||
#include "Profiles.h"
|
||||
#include "Exceptions.h"
|
||||
|
||||
using namespace openshot;
|
||||
|
||||
// default constructor
|
||||
Profile::Profile() {
|
||||
// Initialize info values
|
||||
info.description = "";
|
||||
info.height = 0;
|
||||
info.width = 0;
|
||||
info.pixel_format = 0;
|
||||
info.fps.num = 0;
|
||||
info.fps.den = 0;
|
||||
info.pixel_ratio.num = 0;
|
||||
info.pixel_ratio.den = 0;
|
||||
info.display_ratio.num = 0;
|
||||
info.display_ratio.den = 0;
|
||||
info.interlaced_frame = false;
|
||||
}
|
||||
|
||||
// @brief Constructor for Profile.
|
||||
// @param path The folder path / location of a profile file
|
||||
@@ -22,21 +38,11 @@ Profile::Profile(std::string path) {
|
||||
|
||||
bool read_file = false;
|
||||
|
||||
// Call default constructor
|
||||
Profile();
|
||||
|
||||
try
|
||||
{
|
||||
// Initialize info values
|
||||
info.description = "";
|
||||
info.height = 0;
|
||||
info.width = 0;
|
||||
info.pixel_format = 0;
|
||||
info.fps.num = 0;
|
||||
info.fps.den = 0;
|
||||
info.pixel_ratio.num = 0;
|
||||
info.pixel_ratio.den = 0;
|
||||
info.display_ratio.num = 0;
|
||||
info.display_ratio.den = 0;
|
||||
info.interlaced_frame = false;
|
||||
|
||||
QFile inputFile(path.c_str());
|
||||
if (inputFile.open(QIODevice::ReadOnly))
|
||||
{
|
||||
@@ -55,8 +61,9 @@ Profile::Profile(std::string path) {
|
||||
int value_int = 0;
|
||||
|
||||
// update struct (based on line number)
|
||||
if (setting == "description")
|
||||
if (setting == "description") {
|
||||
info.description = value;
|
||||
}
|
||||
else if (setting == "frame_rate_num") {
|
||||
std::stringstream(value) >> value_int;
|
||||
info.fps.num = value_int;
|
||||
@@ -98,7 +105,7 @@ Profile::Profile(std::string path) {
|
||||
info.pixel_format = value_int;
|
||||
}
|
||||
}
|
||||
read_file = true;
|
||||
read_file = true;
|
||||
inputFile.close();
|
||||
}
|
||||
|
||||
@@ -115,6 +122,81 @@ Profile::Profile(std::string path) {
|
||||
throw InvalidFile("Profile could not be found or loaded (or is invalid).", path);
|
||||
}
|
||||
|
||||
// Return a formatted FPS
|
||||
std::string Profile::formattedFPS(bool include_decimal) {
|
||||
// Format FPS to use 2 decimals (if needed)
|
||||
float fps = info.fps.ToFloat();
|
||||
std::stringstream fps_string;
|
||||
if (info.fps.den == 1) {
|
||||
// For example: 24.0 will become 24
|
||||
fps_string << std::fixed << std::setprecision(0) << fps;
|
||||
} else {
|
||||
// For example: 29.97002997 will become 29.97
|
||||
fps_string << std::fixed << std::setprecision(2) << fps;
|
||||
// Remove decimal place using QString (for convenience)
|
||||
if (!include_decimal) {
|
||||
QString fps_qstring = QString::fromStdString(fps_string.str());
|
||||
fps_qstring.replace(".", "");
|
||||
fps_string.str(fps_qstring.toStdString());
|
||||
}
|
||||
}
|
||||
return fps_string.str();
|
||||
}
|
||||
|
||||
// Return a unique key of this profile (01920x1080i2997_16-09)
|
||||
std::string Profile::Key() {
|
||||
std::stringstream output;
|
||||
std::string progressive_str = "p";
|
||||
if (info.interlaced_frame) {
|
||||
progressive_str = "i";
|
||||
}
|
||||
std::string fps_string = formattedFPS(false);
|
||||
output << std::setfill('0') << std::setw(5) << info.width << std::setfill('\0') << "x";
|
||||
output << std::setfill('0') << std::setw(4) << info.height << std::setfill('\0') << progressive_str;
|
||||
output << std::setfill('0') << std::setw(4) << fps_string << std::setfill('\0') << "_";
|
||||
output << std::setfill('0') << std::setw(2) << info.display_ratio.num << std::setfill('\0') << "-";
|
||||
output << std::setfill('0') << std::setw(2) << info.display_ratio.den << std::setfill('\0');
|
||||
return output.str();
|
||||
}
|
||||
|
||||
// Return the name of this profile (1920x1080p29.97)
|
||||
std::string Profile::ShortName() {
|
||||
std::stringstream output;
|
||||
std::string progressive_str = "p";
|
||||
if (info.interlaced_frame) {
|
||||
progressive_str = "i";
|
||||
}
|
||||
std::string fps_string = formattedFPS(true);
|
||||
output << info.width << "x" << info.height << progressive_str << fps_string;
|
||||
return output.str();
|
||||
}
|
||||
|
||||
// Return a longer format name (1920x1080p @ 29.97 fps (16:9))
|
||||
std::string Profile::LongName() {
|
||||
std::stringstream output;
|
||||
std::string progressive_str = "p";
|
||||
if (info.interlaced_frame) {
|
||||
progressive_str = "i";
|
||||
}
|
||||
std::string fps_string = formattedFPS(true);
|
||||
output << info.width << "x" << info.height << progressive_str << " @ " << fps_string
|
||||
<< " fps (" << info.display_ratio.num << ":" << info.display_ratio.den << ")";
|
||||
return output.str();
|
||||
}
|
||||
|
||||
// Return a longer format name (1920x1080p @ 29.97 fps (16:9) HD 1080i 29.97 fps)
|
||||
std::string Profile::LongNameWithDesc() {
|
||||
std::stringstream output;
|
||||
std::string progressive_str = "p";
|
||||
if (info.interlaced_frame) {
|
||||
progressive_str = "i";
|
||||
}
|
||||
std::string fps_string = formattedFPS(true);
|
||||
output << info.width << "x" << info.height << progressive_str << " @ " << fps_string
|
||||
<< " fps (" << info.display_ratio.num << ":" << info.display_ratio.den << ") " << info.description;
|
||||
return output.str();
|
||||
}
|
||||
|
||||
// Generate JSON string of this object
|
||||
std::string Profile::Json() const {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user