网站 关键字 标签,自动化设备技术支持东莞网站建设,施工企业的期间费用主要包括哪些,wordpress 多网站吗中文互联网上的rust示例程序源码还是太稀少#xff0c;找资料很是麻烦#xff0c;下面是自己用业余时间开发实现的一个对批量rtsp码流源进行关键帧截图并存盘的rust demo源码记录。 要编译这个源码需要先安装vcpkg#xff0c;然后用vcpkg install ffmpeg安装最新版本的ffmpe… 中文互联网上的rust示例程序源码还是太稀少找资料很是麻烦下面是自己用业余时间开发实现的一个对批量rtsp码流源进行关键帧截图并存盘的rust demo源码记录。 要编译这个源码需要先安装vcpkg然后用vcpkg install ffmpeg安装最新版本的ffmpeg库当然了你要是想vcpkg成功编译安装ffmpegvc编译器和windows sdk也是必不可少的这些对于做rust windows开发的人来说都不是事还有llvm及clang windows编译器环境也要安装这都是准备工作。 代码使用了ffmpeg-next库这个库在ubuntu 22上面使用sudo apt install 的ffmpeg相关libdev包和windows不一样ubuntu 22里面默认是ffmpeg 4.3windows平台默认是ffmpeg 7.0.2 ,这就导致了在跨平台编译的时候会出现问题linux平台获取video decodec解码器和windows平台不一样代码里面注释掉的内容就是在linux平台编译的时候要使用的函数如果要在linux平台且使用ffmpeg 4.x版本编译注意打开注释掉的内容。
use ffmpeg_next as ffmpeg;
use tokio;
use std::sync::Arc;
use tokio::sync::Semaphore;
use std::error::Error;
use image::{ImageBuffer, Rgb};
use std::fmt;
use std::path::PathBuf;
use std::sync::atomic::{AtomicUsize, Ordering};
use ffmpeg::format::input;
use ffmpeg::software::scaling::{context::Context, flag::Flags};
use ffmpeg::util::frame::video::Video;
use ffmpeg::format::stream::Stream;#[derive(Debug)]
enum CustomError {FfmpegError(ffmpeg::Error),ImageError(image::ImageError),Other(String),
}impl fmt::Display for CustomError {fn fmt(self, f: mut fmt::Formatter) - fmt::Result {match self {CustomError::FfmpegError(e) write!(f, FFmpeg error: {}, e),CustomError::ImageError(e) write!(f, Image error: {}, e),CustomError::Other(e) write!(f, Other error: {}, e),}}
}impl std::error::Error for CustomError {}impl Fromffmpeg::Error for CustomError {fn from(error: ffmpeg::Error) - Self {CustomError::FfmpegError(error)}
}impl Fromimage::ImageError for CustomError {fn from(error: image::ImageError) - Self {CustomError::ImageError(error)}
}impl Fromstr for CustomError {fn from(error: str) - Self {CustomError::Other(error.to_string())}
}struct RtspSource {url: String,
}fn get_decoder(input_stream: Stream) - Resultffmpeg::decoder::Video, ffmpeg::Error {let decoder_params input_stream.parameters();let mut ctx ffmpeg::codec::context::Context::new();ctx.set_parameters(decoder_params)?;ctx.decoder().video()
}// #[cfg(not(feature ffmpeg_5_0))]
// fn get_decoder(input_stream: Stream) - Resultffmpeg::decoder::Video, ffmpeg::Error {
// input_stream.codec().decoder().video()
// }async fn capture_frame(source: RtspSource, frame_counter: ArcAtomicUsize) - Result(), Boxdyn Error {let mut ictx input(source.url)?;let input_stream ictx.streams().best(ffmpeg::media::Type::Video).ok_or(Could not find best video stream)?;let video_stream_index input_stream.index();let mut decoder get_decoder(input_stream)?;let mut scaler Context::get(decoder.format(),decoder.width(),decoder.height(),ffmpeg::format::Pixel::RGB24,decoder.width(),decoder.height(),Flags::BILINEAR,)?;let mut frame Video::empty();let current_path std::env::current_dir()?;for (stream, packet) in ictx.packets() {if stream.index() video_stream_index packet.is_key() {decoder.send_packet(packet)?;while decoder.receive_frame(mut frame).is_ok() {let mut rgb_frame Video::empty();scaler.run(frame, mut rgb_frame)?;let buffer rgb_frame.data(0);let width rgb_frame.width() as u32;let height rgb_frame.height() as u32;let img: ImageBufferRgbu8, _ ImageBuffer::from_raw(width, height, buffer.to_owned()).ok_or(Failed to create image buffer)?;let index frame_counter.fetch_add(1, Ordering::SeqCst);let file_save_name format!(captured_frame_{}.jpg, index);let save_path: PathBuf current_path.join(./images/).join(file_save_name);img.save(save_path)?;println!(Frame captured and saved to {}, save_path.display());return Ok(());}}}Ok(())
}async fn process_sources(sources: VecRtspSource, max_concurrent: usize) - Result(), Boxdyn Error {let semaphore Arc::new(Semaphore::new(max_concurrent));let frame_counter Arc::new(AtomicUsize::new(0));let mut handles vec![];for source in sources {let permit semaphore.clone().acquire_owned().await?;let frame_counter_clone Arc::clone(frame_counter);let handle tokio::spawn(async move {let result capture_frame(source, frame_counter_clone).await;match result {Ok(_) println!(Successfully captured frame from {}, source.url),Err(e) eprintln!(Error capturing frame from {}: {}, source.url, e),}drop(permit);});handles.push(handle);}for handle in handles {handle.await?;}Ok(())
}#[tokio::main]
async fn main() - Result(), Boxdyn Error {ffmpeg::init()?;let mut sources:VecRtspSourceVec::with_capacity(100);for _ in 1..100 {sources.push(RtspSource {url: format!(rtsp://你的rtsp源ip地址:8554/stream),});}let max_concurrent 20; // Set the maximum number of concurrent captureslet start_time tokio::time::Instant::now();process_sources(sources, max_concurrent).await?;let end_time tokio::time::Instant::now();println!(Time taken to capture frames: {:?}, end_time.duration_since(start_time));Ok(())
} 本文发表于https://blog.csdn.net/peihexian欢迎转载当博客写完的时候我想到一个问题那就是其实是不是可以通过调用ffmpeg.exe命令行的方式传参实现截图的抓取不过在实现上面的算法中我尝试了连上rtsp源头以后立马抓第一帧图像就存盘是不行的因为没有关键帧数据第一帧抓到的是乱码所以代码里面改成了抓关键帧这样存盘的时候肯定是完整的图像不知道使用命令行方式传参的方式能不能解决取关键帧的问题。 补充一下Cargo.toml的文件内容:
[package]
name ffmpeg-test1
version 0.1.0
edition 2021[dependencies]
ffmpeg-next { version 7.0 }
tokio { version 1.0, features [full] }
image 0.25
文章转载自: http://www.morning.xbkcr.cn.gov.cn.xbkcr.cn http://www.morning.bwzzt.cn.gov.cn.bwzzt.cn http://www.morning.xhfky.cn.gov.cn.xhfky.cn http://www.morning.dwxqf.cn.gov.cn.dwxqf.cn http://www.morning.ljzss.cn.gov.cn.ljzss.cn http://www.morning.mplld.cn.gov.cn.mplld.cn http://www.morning.wcczg.cn.gov.cn.wcczg.cn http://www.morning.fmkbk.cn.gov.cn.fmkbk.cn http://www.morning.qlrtd.cn.gov.cn.qlrtd.cn http://www.morning.qljxm.cn.gov.cn.qljxm.cn http://www.morning.snlxb.cn.gov.cn.snlxb.cn http://www.morning.qttft.cn.gov.cn.qttft.cn http://www.morning.tgydf.cn.gov.cn.tgydf.cn http://www.morning.fpczq.cn.gov.cn.fpczq.cn http://www.morning.ltkms.cn.gov.cn.ltkms.cn http://www.morning.nkllb.cn.gov.cn.nkllb.cn http://www.morning.zttjs.cn.gov.cn.zttjs.cn http://www.morning.ljbm.cn.gov.cn.ljbm.cn http://www.morning.jprrh.cn.gov.cn.jprrh.cn http://www.morning.dqpd.cn.gov.cn.dqpd.cn http://www.morning.ykswq.cn.gov.cn.ykswq.cn http://www.morning.mcwrg.cn.gov.cn.mcwrg.cn http://www.morning.xltwg.cn.gov.cn.xltwg.cn http://www.morning.skrrq.cn.gov.cn.skrrq.cn http://www.morning.ptslx.cn.gov.cn.ptslx.cn http://www.morning.hclplus.com.gov.cn.hclplus.com http://www.morning.hmdyl.cn.gov.cn.hmdyl.cn http://www.morning.gcjhh.cn.gov.cn.gcjhh.cn http://www.morning.gcthj.cn.gov.cn.gcthj.cn http://www.morning.jnrry.cn.gov.cn.jnrry.cn http://www.morning.tbrnl.cn.gov.cn.tbrnl.cn http://www.morning.xczyj.cn.gov.cn.xczyj.cn http://www.morning.lkbyj.cn.gov.cn.lkbyj.cn http://www.morning.wdpbq.cn.gov.cn.wdpbq.cn http://www.morning.ydrfl.cn.gov.cn.ydrfl.cn http://www.morning.ydfr.cn.gov.cn.ydfr.cn http://www.morning.kwz6232.cn.gov.cn.kwz6232.cn http://www.morning.glswq.cn.gov.cn.glswq.cn http://www.morning.qpntn.cn.gov.cn.qpntn.cn http://www.morning.rjljb.cn.gov.cn.rjljb.cn http://www.morning.wqkfm.cn.gov.cn.wqkfm.cn http://www.morning.fhntj.cn.gov.cn.fhntj.cn http://www.morning.rnht.cn.gov.cn.rnht.cn http://www.morning.ktmbp.cn.gov.cn.ktmbp.cn http://www.morning.zwgrf.cn.gov.cn.zwgrf.cn http://www.morning.krwzy.cn.gov.cn.krwzy.cn http://www.morning.gjqnn.cn.gov.cn.gjqnn.cn http://www.morning.mpscg.cn.gov.cn.mpscg.cn http://www.morning.zlnkq.cn.gov.cn.zlnkq.cn http://www.morning.yybcx.cn.gov.cn.yybcx.cn http://www.morning.iuibhkd.cn.gov.cn.iuibhkd.cn http://www.morning.rqfkh.cn.gov.cn.rqfkh.cn http://www.morning.sgfgz.cn.gov.cn.sgfgz.cn http://www.morning.itvsee.com.gov.cn.itvsee.com http://www.morning.sxfnf.cn.gov.cn.sxfnf.cn http://www.morning.thbkc.cn.gov.cn.thbkc.cn http://www.morning.xnhnl.cn.gov.cn.xnhnl.cn http://www.morning.rxlck.cn.gov.cn.rxlck.cn http://www.morning.mhmcr.cn.gov.cn.mhmcr.cn http://www.morning.gglhj.cn.gov.cn.gglhj.cn http://www.morning.gwsll.cn.gov.cn.gwsll.cn http://www.morning.fthcn.cn.gov.cn.fthcn.cn http://www.morning.jzgxp.cn.gov.cn.jzgxp.cn http://www.morning.rqfkh.cn.gov.cn.rqfkh.cn http://www.morning.mpngp.cn.gov.cn.mpngp.cn http://www.morning.mnqg.cn.gov.cn.mnqg.cn http://www.morning.lkrmp.cn.gov.cn.lkrmp.cn http://www.morning.tynqy.cn.gov.cn.tynqy.cn http://www.morning.gmrxh.cn.gov.cn.gmrxh.cn http://www.morning.kpgbz.cn.gov.cn.kpgbz.cn http://www.morning.hjbrd.cn.gov.cn.hjbrd.cn http://www.morning.rlkgc.cn.gov.cn.rlkgc.cn http://www.morning.kdldx.cn.gov.cn.kdldx.cn http://www.morning.zrhhb.cn.gov.cn.zrhhb.cn http://www.morning.qnpyz.cn.gov.cn.qnpyz.cn http://www.morning.gyjld.cn.gov.cn.gyjld.cn http://www.morning.wgrl.cn.gov.cn.wgrl.cn http://www.morning.pnntx.cn.gov.cn.pnntx.cn http://www.morning.pcwzb.cn.gov.cn.pcwzb.cn http://www.morning.kjlia.com.gov.cn.kjlia.com