DataScience
Published 2023. 7. 3. 12:35
[Rust] video-rs Rust
728x90

https://crates.io/crates/video-rs


video-rs는 ffmpeg의 libav 계열 라이브러리를 사용하는 Rust용 범용 비디오 라이브러리입니다. 읽기, 쓰기, 믹싱, 인코딩 및 디코딩과 같은 많은 일반적인 비디오 작업에 안정적이고 Rusty 인터페이스를 제공합니다.

 

1. 설치

먼저 ffmpeg 라이브러리를 설치합니다. 
그런 다음 Cargo.toml의 종속성에 다음을 추가합니다:

 

video-rs = "0.4"


ndarray 크레이트에서 원시 프레임을 사용할 수 있게 하려면 ndarray 기능을 사용합니다:

video-rs = { version = "0.4", features = ["ndarray"] }

 

2. 예제

Decode a video and print the RGB value for the top left pixel:

<bash />
use video_rs::{self, Decoder, Locator}; fn main() { video_rs::init().unwrap(); let source = Locator::Url( "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4" .parse() .unwrap(), ); let mut decoder = Decoder::new(&source) .expect("failed to create decoder"); for frame in decoder.decode_iter() { if let Ok((_, frame)) = frame { let rgb = frame .slice(ndarray::s![0, 0, ..]) .to_slice() .unwrap(); println!( "pixel at 0, 0: {}, {}, {}", rgb[0], rgb[1], rgb[2], ); } else { break; } } }

 

Rust에서 비디오 불러와서 비디오 저장하기(crowd.mp4불러와서 man.mp4로 저장)

<bash />
use video_rs::{self, Decoder, Locator}; use std::path::PathBuf; use ndarray::Array3; use video_rs::{Encoder, EncoderSettings, Time}; fn main() { video_rs::init().unwrap(); let source:Locator=PathBuf::from("crowd.mp4").into(); let mut decoder = Decoder::new(&source) .expect("failed to create decoder"); let destination: Locator = PathBuf::from("man.mp4").into(); let settings = EncoderSettings::for_h264_yuv420p(1280, 720, false); let mut encoder = Encoder::new(&destination, settings) .expect("failed to create encoder"); let duration: Time = Time::from_nth_of_a_second(24); let mut position = Time::zero(); for frame in decoder.decode_iter() { if let Ok((_, frame)) = frame { let rgb = frame .slice(ndarray::s![0, 0, ..]) .to_slice() .unwrap(); encoder .encode(&frame, &position) .expect("failed to encode frame"); // Update the current position and add the inter-frame // duration to it. position = position.aligned_with(&duration).add(); } else { break; } } encoder.finish().expect("failed to finish encoder"); }

'Rust' 카테고리의 다른 글

[Rust] error: failed to run custom build command for `clang-sys v1.6.1`  (80) 2023.07.04
[Rust] error:linker link.exe not found  (17) 2023.07.04
[Rust] ONNX모델로 detection  (23) 2023.07.02
[Rust-Python] PyO3  (96) 2023.06.30
Rust Trait  (160) 2023.06.20
profile

DataScience

@Ninestar

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!