#include "mologram/tapi/VideoProbe.hpp"
#include <algorithm>
#include <cctype>
#include <cstdio>
#include "mologram/ProcessRunner.hpp"
#include "tapi/tl/types.hpp"
namespace mologram {
VideoInfo VideoProbe::probe(const std::string& path) {
VideoInfo v;
std::string out;
if (ProcessRunner::capture({"ffprobe", "-v", "error", "-select_streams", "v:0", "-show_entries", "stream=width,height:format=duration",
"-of", "csv=p=0:s=x", path}, out) != 0)
return v;
int w = 0, h = 0;
if (std::sscanf(out.c_str(), "%dx%d", &w, &h) != 2 || w <= 0 || h <= 0) return v;
double dur = 0;
size_t nl = out.find('\n');
if (nl != std::string::npos) std::sscanf(out.c_str() + nl + 1, "%lf", &dur);
v.known = true;
v.width = w;
v.height = h;
v.duration = dur;
return v;
}
tapi::tl::TlObjectPtr VideoProbe::attribute(const VideoInfo& info) {
if (!info.known) return nullptr;
auto a = std::make_shared<tapi::tl::types::DocumentAttributeVideo>();
a->supports_streaming = true;
a->w = info.width;
a->h = info.height;
a->duration = info.duration;
return a;
}
bool VideoProbe::isVideoPath(const std::string& path) {
size_t dot = path.rfind('.');
if (dot == std::string::npos) return false;
std::string ext = path.substr(dot);
for (auto& c : ext) c = (char)std::tolower((unsigned char)c);
return ext == ".mp4" || ext == ".mov" || ext == ".m4v" || ext == ".webm" || ext == ".mkv";
}
} // namespace mologram