Skip to content

Commit

Permalink
Make the requested changes for PR omeryusufyagci#34 issue
Browse files Browse the repository at this point in the history
*(deletion) Original app.py

*(Updation) requirements

*(modification) make changes to preserve the CLI nature and only make
the backend changes
  • Loading branch information
prakash-2002-ramanathan committed Oct 31, 2024
1 parent 7b8057b commit 27053d7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 266 deletions.
60 changes: 16 additions & 44 deletions MediaProcessor/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@
#include <string>

#include "AudioProcessor.h"
#include "CommunicationHandler.h"
#include "ConfigManager.h"
#include "Utils.h"
#include "VideoProcessor.h"

namespace fs = std::filesystem;
using json = nlohmann::json;
using namespace MediaProcessor;

int main() {
int main(int argc, char* argv[]) {
/**
* @brief Processes a video file to isolate vocals and merge them back with the original video
* using JSON input and output.
* @brief Processes a video file to isolate vocals and merge them back with the original video.
*
* This program removes music, sound effects, and noise while retaining clear vocals.
*
Expand All @@ -25,47 +24,24 @@ int main() {
* 3. Process the audio in chunks with parallel processing.
* 4. Merge the isolated vocals back with the original video.
*
* @param json_input This input is received from the input cin.
* @param argc Number of command-line arguments.
* @param argv Array of command-line argument strings.
* @return Exit status code (0 for success, non-zero for failure).
*
* Usage: JSON input via stdin
* Usage: <executable> <video_file_path>
*/

// Read JSON input from stdin
std::string json_input;
std::getline(std::cin, json_input);

json input;
try {
input = json::parse(json_input);
} catch (const json::parse_error& e) {
std::cerr << "Invalid JSON input." << e.what() << std::endl;
return 1;
}

// Extract video file path and config file path from JSON
std::string video_file_path, config_file_path;
try {
json data = input.at("data");
video_file_path = data.at("video_file_path").get<std::string>();
config_file_path = data.at("config_file_path").get<std::string>();

std::cerr << "\nParameters received by Media Handler\n"
<< "video_file_path: " << video_file_path << "\n"
<< "config_file_path: " << config_file_path << "\n";

} catch (const json::exception& e) {
std::cerr << "Missing or invalid input fields." << e.what() << std::endl;
return 1;
if (argc != 3) {
std::string usageMessage =
"Usage: " + std::string(argv[0]) + " <video_file_path> <config_file_path>";
CommunicationHandler::handleError(usageMessage);
}

fs::path inputMediaPath = fs::absolute(video_file_path);
fs::path configFilePath = fs::absolute(config_file_path);
fs::path inputMediaPath = fs::absolute(argv[1]);
fs::path configFilePath = fs::absolute(argv[2]);

// Load the configuration
ConfigManager& configManager = ConfigManager::getInstance();
if (!configManager.loadConfig(configFilePath)) {
std::cerr << "Error: Could not load configuration from " << configFilePath << std::endl;
if (!configManager.loadConfig("config.json")) {
std::cerr << "Error: Could not load configuration." << std::endl;
return 1;
}

Expand All @@ -78,15 +54,11 @@ int main() {
}

VideoProcessor videoProcessor(inputMediaPath, extractedVocalsPath, processedMediaPath);

if (!videoProcessor.mergeMedia()) {
std::cerr << "Failed to merge audio and video." << std::endl;
return 1;
}
// Output JSON success response
json success_response = {{"status", "success"},
{"message", "Video processed successfully."},
{"data", {{"processed_video_path", processedMediaPath.string()}}}};
std::cout << success_response.dump() << std::endl;

std::cout << "Video processed successfully: " << processedMediaPath << std::endl;
return 0;
}
200 changes: 0 additions & 200 deletions app.py

This file was deleted.

33 changes: 11 additions & 22 deletions backend/media_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,11 @@ def process_with_media_processor(video_path: str, base_directory:Union[Path, str
"""Run the C++ MediaProcessor binary with the video path"""
try:
logging.info(f"Processing video at path: {video_path}")

input_data = {
"video_file_path": video_path,
"config_file_path": config_path
}
base_directory = Path(base_directory)


result = subprocess.run(
[str(base_directory / "MediaProcessor/build/MediaProcessor")],
input= ResponseHandler.core_data_passer("The input data",input_data),
capture_output=True,
text=True
[str(base_directory / "MediaProcessor/build/MediaProcessor"), str(video_path), config_path], capture_output=True, text=True
)

if result.returncode != 0:
Expand All @@ -72,20 +65,16 @@ def process_with_media_processor(video_path: str, base_directory:Union[Path, str
print(result.stderr)

# Parse the output to get the processed video path (TODO: encapsulate)
try:
response = json.loads(result.stdout)
if response.get("status") == "success":
processed_video_path = response["data"]["processed_video_path"]
# Log the processed video path
logging.info(f"Processed video path returned: {processed_video_path}")
# Return success response
return processed_video_path

else:
logging.error("Unexpected response from MediaProcessor")
for line in result.stdout.splitlines():
if "Video processed successfully" in line:
processed_video_path = line.split(": ", 1)[1].strip()

except json.JSONDecodeError:
logging.error("Failed to parse JSON from MediaProcessor output")
# Remove any surrounding quotes (TODO: encapsulate)
if processed_video_path.startswith('"') and processed_video_path.endswith('"'):
processed_video_path = processed_video_path[1:-1]
processed_video_path = str(Path(processed_video_path).resolve())
logging.info(f"Processed video path returned: {processed_video_path}")
return processed_video_path

return None
except Exception as e:
Expand Down
4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ gevent-websocket==0.10.1
greenlet==3.1.0
h11==0.14.0
idna==3.8
iniconfig==2.0.0
itsdangerous==2.2.0
Jinja2==3.1.4
MarkupSafe==2.1.5
Expand All @@ -27,12 +28,15 @@ mypy-extensions==1.0.0
packaging==24.1
pathspec==0.12.1
platformdirs==4.3.6
pluggy==1.5.0
pycodestyle==2.12.1
pycryptodomex==3.20.0
pyflakes==3.2.0
pytest==8.3.3
python-engineio==4.9.1
python-socketio==5.11.4
requests==2.32.3
setuptools==75.2.0
simple-websocket==1.0.0
tomli==2.0.2
typing_extensions==4.12.2
Expand Down

0 comments on commit 27053d7

Please sign in to comment.