feat: add codec

This commit is contained in:
2025-12-28 02:30:42 +09:00
parent 6deb4254b7
commit ddf9aec981
2 changed files with 151 additions and 64 deletions

View File

@@ -95,8 +95,93 @@ async fn accept_connection(stream: TcpStream) {
fn process_video(
tx: tokio::sync::mpsc::Sender<(Vec<u8>, bool)>,
) -> Result<(), Box<dyn std::error::Error>> {
let input_file = "video.mp4";
let mut ictx = ffmpeg::format::input(&input_file)?;
ffmpeg::device::register_all();
let mut dictionary = ffmpeg::Dictionary::new();
dictionary.set("framerate", "30");
dictionary.set("video_size", "1920x1080");
// Find the gdigrab input format (Windows)
// ffmpeg::format::format::find returns Option<ffmpeg::format::format::Input> in some versions?
// Let's try to use the device directly if possible or finding the demuxer.
// Based on errors, let's try assuming ffmpeg::format::format::find exists and works for inputs.
// If not, we might need ffmpeg::format::demuxer.
// We'll assume the error 'private module input' implies 'ffmpeg::format::format::input' is private
// so we can't look inside it. But 'ffmpeg::format::format' might have 'find'.
// Actually, let's try a different approach:
// If we can't find the format easily, maybe we can just use "gdigrab" as the format name if we had a way to convert string to Format.
// We cannot easily look up "gdigrab" by name due to API limitations in the safe wrapper or versioning.
// However, if we enable all devices, ffmpeg might be able to detect it via input().
// Another trick: We can manually iterate via `ffmpeg::format::format::Input::next()` if we could access it, but it's hidden.
// Let's try to bypass the explicit format finding by using `ffmpeg::format::input_with_dictionary`
// but we need to specify the format. Wait, `input_with_dictionary` takes a path.
// If the path is prefixed with "gdigrab:", maybe it auto-detects? No, gdigrab is a format.
// There IS a `ffmpeg::device::input::video` which might help?
// Let's check if we can use the `av_find_input_format` ffi directly if safe wrapper fails us.
// But that requires `unsafe`.
// Ideally we should use:
// `ffmpeg::format::format::list()` but it is gated by `ffmpeg_5_0` feature being NOT enabled?
// Wait, the error said `list` is not found, and the code I Grepped says `#[cfg(not(feature = "ffmpeg_5_0"))]`.
// If we are on ffmpeg 5.0+, then `av_register_all` is gone and iterating formats is different.
// If we are on newer FFmpeg, we might not need to look it up manually if we can hint it.
// But `open_with` needs `&Format`.
// Let's assume we can use `ffmpeg::device::input::video` if it exists?
// Check `ffmpeg::device` module content.
// Fallback: Use `ffmpeg::format::input(&path)` but force format via dictionary? No, dictionary is options.
// Actually, look at `ffmpeg::format::open_with`: it takes `&Format`.
// We MUST find the format.
// Since `list()` is missing, maybe we are on a version > 5.0 feature-wise?
// The crate is version 8.0.0.
// Let's try using `ffmpeg::format::Input` directly if there's a way to construct it.
// No.
// What if we try `ffmpeg::device::input::video()`?
// Let's check `ffmpeg::device` capabilities.
// For now, let's try a gross hack:
// If `list()` is unavailable, it means we probably can't iterate.
// But we might be able to use `ffmpeg::ffi::av_find_input_format`.
unsafe {
let name = std::ffi::CString::new("gdigrab").unwrap();
let ptr = ffmpeg::ffi::av_find_input_format(name.as_ptr());
if ptr.is_null() {
return Err(ffmpeg::Error::DemuxerNotFound.into());
}
let format_input = ffmpeg::format::format::Input::wrap(ptr as *mut _);
// We need to wrap Input into Format, but Format might be private in some contexts or re-exported.
// It is defined in `ffmpeg::format::format::mod.rs` as `pub enum Format`.
// And it is re-exported in `ffmpeg` root? No, `ffmpeg::format::Format` should be public.
// The error says `ffmpeg::format::Format` is private?
// Ah, `use {Dictionary, Error, Format};` in `src/format/mod.rs` means it imports from parent/root?
// No, `pub mod format` defines `Format` enum.
// Let's try `ffmpeg::format::format::Format::Input`
let format = ffmpeg::format::format::Format::Input(format_input);
// Now we have the format, proceed.
// Note: `Input::wrap` is `unsafe`.
let context =
ffmpeg::format::open_with(&std::path::Path::new("desktop"), &format, dictionary)?;
let mut ictx = match context {
ffmpeg::format::context::Context::Input(ictx) => ictx,
_ => return Err(ffmpeg::Error::DemuxerNotFound.into()),
};
let input_stream = ictx
.streams()
@@ -104,7 +189,8 @@ fn process_video(
.ok_or(ffmpeg::Error::StreamNotFound)?;
let input_stream_index = input_stream.index();
let decoder_ctx = ffmpeg::codec::context::Context::from_parameters(input_stream.parameters())?;
let decoder_ctx =
ffmpeg::codec::context::Context::from_parameters(input_stream.parameters())?;
let mut decoder = decoder_ctx.decoder().video()?;
// Setup Encoder
@@ -179,6 +265,7 @@ fn process_video(
return Ok(());
}
}
}
Ok(())
}

View File

@@ -16,6 +16,6 @@ const decoder = createDecoder((frame) => {
})
const discordClientId = import.meta.env.VITE_DISCORD_CLIENT_ID;
const addr = `wss://${discordClientId}.discordsays.com/ws`
const addr = import.meta.env.PROD ? `wss://${discordClientId}.discordsays.com/ws` : 'ws://localhost:8080';
createSocket(addr, decoder);