Skip to content

Commit 5933b4e

Browse files
committed
youtube downloading
1 parent b2940ab commit 5933b4e

File tree

10 files changed

+336
-309
lines changed

10 files changed

+336
-309
lines changed

foreign/build/CMakeFiles/CMakeConfigureLog.yaml

Lines changed: 98 additions & 98 deletions
Large diffs are not rendered by default.

foreign/main.cpp

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ typedef double float64_t;
66
#endif
77
#ifdef _WIN32
88
#define EXPORT __declspec(dllexport)
9+
#include <windows.h>
910
#else
1011
#define EXPORT __attribute__((visibility("default")))
1112
#endif
@@ -79,6 +80,7 @@ extern "C" {
7980
EXPORT void train_nn();
8081
// EXPORT StreamAnalysis* cut_stream(const char* tesseract_path, const char* svm_path, const char* video_path, uint8_t overlay_id, const char* output_folder, void (*callback)(int));
8182
EXPORT void cut_stream_async(const char* tesseract_path, const char* svm_path, const char* video_path, uint8_t overlay_id, const char* output_folder, const char* event_name, void (*callback)(int));
83+
EXPORT void download_stream_async(const char* youtube_url, void (*callback)(const char*));
8284
}
8385

8486
int32_t add(int32_t a, int32_t b) {
@@ -691,7 +693,7 @@ extern "C" StreamAnalysis* cut_stream(const std::string& tesseract_path, const s
691693
while (true) {
692694
cv::Mat drawingFrame = frame.clone();
693695

694-
cv::Rect overlayRect = cv::selectROI("Select the overlay region", drawingFrame);
696+
cv::Rect overlayRect = cv::selectROI("Select the overlay region and press ENTER", drawingFrame);
695697
int x = overlayRect.x;
696698
int y = 0;
697699
int width = drawingFrame.cols - overlayRect.x - 1;
@@ -714,7 +716,7 @@ extern "C" StreamAnalysis* cut_stream(const std::string& tesseract_path, const s
714716
std::cout << "Got key: " << key << std::endl;
715717
if (key == 127 || key == 8) // Backspace
716718
{
717-
cv::destroyWindow("Select the overlay region");
719+
cv::destroyWindow("Select the overlay region and press ENTER");
718720
cv::destroyWindow("Press BACKSPACE if the overlay does not line up, otherwise press ENTER");
719721
continue;
720722
}
@@ -725,7 +727,7 @@ extern "C" StreamAnalysis* cut_stream(const std::string& tesseract_path, const s
725727
overlay.red_score.fromRect(redScoreRect, frame.cols, frame.rows);
726728
overlay.green_score.fromRect(greenScoreRect, frame.cols, frame.rows);
727729

728-
cv::destroyWindow("Select the overlay region");
730+
cv::destroyWindow("Select the overlay region and press ENTER");
729731
cv::destroyWindow("Press BACKSPACE if the overlay does not line up, otherwise press ENTER");
730732
break;
731733
}
@@ -779,7 +781,7 @@ extern "C" StreamAnalysis* cut_stream(const std::string& tesseract_path, const s
779781
}
780782
bool score_nonzero = redScore > 0 || greenScore > 0;
781783

782-
std::cout << "Score at frame " << i << " (" << i / fps << "s, " << i * 100 / frame_count << "%): " << (int)redScore << "-" << (int)greenScore << std::endl;
784+
// std::cout << "Score at frame " << i << " (" << i / fps << "s, " << i * 100 / frame_count << "%): " << (int)redScore << "-" << (int)greenScore << std::endl;
783785

784786
if (!bout_running && score_nonzero)
785787
{
@@ -971,4 +973,75 @@ void cut_stream_async(const char* tesseract_path, const char* svm_path, const ch
971973
// Call callback with result
972974
if (callback) callback(255);
973975
}).detach(); // Detach thread to run independently
976+
}
977+
978+
int download_stream(const std::string& url, char* message)
979+
{
980+
std::cout << "[YT DOWNLOADER] Downloading video from " << url << std::endl;
981+
#ifdef _WIN32
982+
STARTUPINFO si = { sizeof(si) };
983+
PROCESS_INFORMATION pi;
984+
985+
std::string command = "yt-dlp -N 4 -f \"bv*+ba[ext=m4a]/b\" --merge-output-format mp4 --no-progress --no-warnings -o \"TEMP_SOURCE_VIDEO_YOUTUBE.mp4\" " + url;
986+
987+
// Convert to LPSTR
988+
char cmd[1024];
989+
strcpy_s(cmd, command.c_str());
990+
991+
if (CreateProcess(NULL, cmd, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi))
992+
{
993+
WaitForSingleObject(pi.hProcess, INFINITE);
994+
995+
// Get exit code
996+
DWORD exitCode;
997+
GetExitCodeProcess(pi.hProcess, &exitCode);
998+
999+
CloseHandle(pi.hProcess);
1000+
CloseHandle(pi.hThread);
1001+
if (exitCode == 0) {
1002+
std::cout << "[YT DOWNLOADER] Finished downloading video!" << std::endl;
1003+
}
1004+
else {
1005+
std::cerr << "[YT DOWNLOADER] Download failed with exit code: " << exitCode << std::endl;
1006+
}
1007+
return exitCode;
1008+
}
1009+
else
1010+
{
1011+
strncpy(message, "Failed to start YouTube downloader", 1024);
1012+
}
1013+
#else
1014+
std::string command = "yt-dlp -o \"TEMP_SOURCE_VIDEO_YOUTUBE.mp4\" " + url + " > /dev/null 2>&1 &";
1015+
system(command.c_str());
1016+
#endif
1017+
1018+
return 0;
1019+
}
1020+
1021+
void download_stream_async(const char* youtube_url, void (*callback)(const char*)) {
1022+
std::string url_str(youtube_url);
1023+
char* message = (char*)malloc(1024 * sizeof(char));
1024+
1025+
std::thread([url_str, message, callback]() {
1026+
try {
1027+
int exitCode = download_stream(url_str, message);
1028+
if (exitCode == 0)
1029+
{
1030+
strncpy(message, "Finished downloading YouTube video", 1023);
1031+
}
1032+
else
1033+
{
1034+
std::string error_message = std::string("Download failed with exit code ") + std::to_string(exitCode);
1035+
strncpy(message, error_message.c_str(), 1023);
1036+
}
1037+
}
1038+
catch (const std::exception& e)
1039+
{
1040+
std::string str = std::string("Exception: ") + e.what();
1041+
strncpy(message, str.c_str(), 1023);
1042+
}
1043+
1044+
// Call callback with result
1045+
if (callback) callback(message);
1046+
}).detach(); // Detach thread to run independently
9741047
}

foreign/old.cpp

Lines changed: 0 additions & 154 deletions
This file was deleted.

lib/ffi.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ base class StreamAnalysis extends Struct {
3030
}
3131

3232
typedef CallbackFunc = Void Function(Int32);
33+
typedef CallbackFuncStr = Void Function(Pointer<Utf8>);
3334
typedef DartCallback = void Function(int);
3435

3536
typedef CutStreamC = Pointer<Void> Function(
@@ -49,6 +50,11 @@ typedef CutStreamDart = Pointer<Void> Function(
4950
Pointer<Utf8>,
5051
Pointer<NativeFunction<CallbackFunc>>);
5152

53+
typedef DownloadYoutubeC = Pointer<Void> Function(
54+
Pointer<Utf8>, Pointer<NativeFunction<CallbackFuncStr>>);
55+
typedef DownloadYoutubeDart = Pointer<Void> Function(
56+
Pointer<Utf8>, Pointer<NativeFunction<CallbackFuncStr>>);
57+
5258
class NativeLibrary {
5359
late final DynamicLibrary _lib;
5460

@@ -64,4 +70,8 @@ class NativeLibrary {
6470

6571
late final CutStreamDart cutStream =
6672
_lib.lookup<NativeFunction<CutStreamC>>('cut_stream_async').asFunction();
73+
74+
late final DownloadYoutubeDart downloadYoutube = _lib
75+
.lookup<NativeFunction<DownloadYoutubeC>>('download_stream_async')
76+
.asFunction();
6777
}

0 commit comments

Comments
 (0)