-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathffmpeg_tool.cpp
More file actions
75 lines (62 loc) · 2.01 KB
/
Copy pathffmpeg_tool.cpp
File metadata and controls
75 lines (62 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "ffmpeg_tool.hpp"
#include "filesystem_tools.hpp"
#include <cstdlib>
#include <filesystem>
namespace droidcli::cli {
namespace {
namespace fs = std::filesystem;
#ifdef _WIN32
constexpr const char* kFfmpegBinaryName = "ffmpeg.exe";
#else
constexpr const char* kFfmpegBinaryName = "ffmpeg";
#endif
} // namespace
FfmpegResolveResult resolve_ffmpeg()
{
FfmpegResolveResult result;
const WhichResult on_path = which_executable("ffmpeg");
if (on_path.ok)
{
result.ok = true;
result.resolved_path = on_path.resolved_path;
return result;
}
const char* ffmpeg_root = std::getenv("DROIDCLI_FFMPEG_ROOT");
if (ffmpeg_root != nullptr && ffmpeg_root[0] != '\0')
{
std::error_code error;
const fs::path candidate = fs::path(ffmpeg_root) / "bin" / kFfmpegBinaryName;
if (fs::exists(candidate, error) && fs::is_regular_file(candidate, error))
{
result.ok = true;
result.resolved_path = fs::absolute(candidate, error).string();
return result;
}
}
result.error_message =
"ffmpeg not found on PATH or under $DROIDCLI_FFMPEG_ROOT/bin - install it "
"(e.g. `winget install ffmpeg` on Windows) or set DROIDCLI_FFMPEG_ROOT to a "
"prefix with a bin/" + core::String(kFfmpegBinaryName) + " in it.";
return result;
}
CommandRunResult run_ffmpeg(
const core::String& args,
const core::String& work_dir,
const int32_t timeout_ms)
{
const FfmpegResolveResult resolved = resolve_ffmpeg();
if (!resolved.ok)
{
CommandRunResult result;
result.error_message = resolved.error_message;
return result;
}
const core::String command = "\"" + resolved.resolved_path + "\" " + args;
// via_shell=false: ffmpeg invocations never need shell features (pipes,
// redirects, env var expansion) and args frequently contain their own
// nested double quotes (filter expressions like s="sin(2*PI*440)") that
// cmd.exe's `/c` re-tokenizing pass can silently mangle - see the
// header comment on run_command_once.
return run_command_once(command, work_dir, timeout_ms, /*via_shell=*/false);
}
} // namespace droidcli::cli