Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 21 additions & 34 deletions src/transcoder/src/transcoder_fftool.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <cstdlib>

Check failure on line 1 in src/transcoder/src/transcoder_fftool.cpp

View workflow job for this annotation

GitHub Actions / clang-format-check

Style Issue

Code style violation detected
#include <cstring>
#include <fstream>
#include <iostream>
Expand All @@ -18,24 +18,15 @@

TranscoderFFTool::TranscoderFFTool(ProcessParameter *processParameter,
EncodeParameter *encodeParameter)
: Transcoder(processParameter, encodeParameter), copyVideo(false),
copyAudio(false), frameTotalNumber(0) {}
: Transcoder(processParameter, encodeParameter),
copyVideo(false),
copyAudio(false),
frameTotalNumber(0) {}

TranscoderFFTool::~TranscoderFFTool() {
// Destructor implementation
}

// Helper function to escape file paths for Windows
std::string escapeWindowsPath(const std::string &path) {
std::string escaped = path;
size_t pos = 0;
while ((pos = escaped.find("\\", pos)) != std::string::npos) {
escaped.replace(pos, 1, "\\\\");
pos += 2;
}
return escaped;
}

bool TranscoderFFTool::prepared_opt() {

if (encodeParameter->get_Video_Codec_Name() == "") {
Expand All @@ -60,66 +51,63 @@
return true;
}

bool TranscoderFFTool::transcode(std::string input_path,
std::string output_path) {
bool TranscoderFFTool::transcode(std::string input_path, std::string output_path) {
if (!prepared_opt()) {
std::cerr << "Failed to prepare options for transcoding." << std::endl;
return false;
}

// Convert paths for Windows (escape backslashes)
#ifdef _WIN32
input_path = escapeWindowsPath(input_path);
output_path = escapeWindowsPath(output_path);
#endif
// Unified Path Processing Function
auto quotePath = [](const std::string& path) {
return "\"" + path + "\"";
};

// Build the FFmpeg command
std::stringstream cmd;

// Check if FFMPEG_PATH is defined (ensure it's set by CMake)
#ifdef FFTOOL_PATH
cmd << FFTOOL_PATH << " -i \"" << input_path << "\"";
cmd << quotePath(FFTOOL_PATH);
#else
std::cerr << "FFmpeg path is not defined! Ensure CMake sets FFMPEG_PATH."
<< std::endl;
std::cerr << "FFmpeg path is not defined! Ensure CMake sets FFMPEG_PATH." << std::endl;
return false;
#endif

// Add the -y flag to overwrite output file without prompting
cmd << " -y";
cmd << " -i " << quotePath(input_path) << " -y";

// Video codec options
if (copyVideo) {
cmd << " -c:v copy"; // Copy video stream without re-encoding
cmd << " -c:v copy"; // Copy video stream without re-encoding
} else {
if (!videoCodec.empty()) {
cmd << " -c:v " << videoCodec; // Use specified video codec
cmd << " -c:v " << videoCodec; // Use specified video codec
} else {
std::cerr << "Video codec is not specified!" << std::endl;
return false;
}
if (videoBitRate > 0) {
cmd << " -b:v " << videoBitRate; // Set video bitrate if specified
cmd << " -b:v " << videoBitRate; // Set video bitrate if specified
}
}

// Audio codec options
if (copyAudio) {
cmd << " -c:a copy"; // Copy audio stream without re-encoding
cmd << " -c:a copy"; // Copy audio stream without re-encoding
} else {
if (!audioCodec.empty()) {
cmd << " -c:a " << audioCodec; // Use specified audio codec
cmd << " -c:a " << audioCodec; // Use specified audio codec
} else {
std::cerr << "Audio codec is not specified!" << std::endl;
return false;
}
if (audioBitRate > 0) {
cmd << " -b:a " << audioBitRate; // Set audio bitrate if specified
cmd << " -b:a " << audioBitRate; // Set audio bitrate if specified
}
}

// Output file path
cmd << " \"" << output_path << "\"";
cmd << " " << quotePath(output_path);

// Execute the command
std::cout << "Executing: " << cmd.str() << std::endl;
Expand All @@ -128,16 +116,15 @@

#ifdef _WIN32
// Windows-specific command execution (use cmd /c for shell commands)
std::string fullCmd = "cmd /c " + cmd.str();
std::string fullCmd = "cmd /c " + quotePath(cmd.str());
ret = system(fullCmd.c_str());
#else
// Unix-like systems (Linux/macOS) can directly use system()
ret = system(cmd.str().c_str());
#endif

if (ret != 0) {
std::cerr << "FFmpeg transcoding failed with exit code: " << ret
<< std::endl;
std::cerr << "FFmpeg transcoding failed with exit code: " << ret << std::endl;
return false;
}

Expand Down
Loading