Skip to content

Commit 8daea90

Browse files
committed
[*] move video-encoder to a new crate
1 parent 4e4cfc6 commit 8daea90

File tree

19 files changed

+173
-140
lines changed

19 files changed

+173
-140
lines changed

Cargo.lock

Lines changed: 17 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ pmacro = { path = "lib/pmacro" }
130130
bytesio = { path = "lib/bytesio" }
131131
recorder = { path = "lib/recorder" }
132132
mp4-player = { path = "lib/mp4-player" }
133+
video-encoder = { path = "lib/video-encoder" }
133134
screen-capture = { path = "lib/screen-capture" }
134135
screen-capture-windows = { path = "lib/screen-capture-windows" }
135136
screen-capture-wayland-wlr = { path = "lib/screen-capture-wayland-wlr" }

lib/mp4m/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ homepage.workspace = true
1010
repository.workspace = true
1111
description.workspace = true
1212

13-
1413
[dependencies]
1514
log.workspace = true
1615
mp4.workspace = true
@@ -20,14 +19,15 @@ fdk-aac.workspace = true
2019
thiserror.workspace = true
2120
crossbeam.workspace = true
2221
derive_builder.workspace = true
22+
video-encoder.workspace = true
2323

2424
[dev-dependencies]
2525
rand.workspace = true
2626
image.workspace = true
2727
env_logger.workspace = true
2828

2929
[target.'cfg(target_os = "linux")'.dev-dependencies]
30-
recorder = { workspace = true, features = ["wayland-wlr"] }
30+
video-encoder = { workspace = true, features = ["x264"] }
3131

3232
[target.'cfg(target_os = "windows")'.dev-dependencies]
33-
recorder = { workspace = true, features = ["windows"] }
33+
video-encoder = { workspace = true, features = ["ffmpeg"] }

lib/mp4m/examples/mp4_processor_demo.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ use image::{ImageBuffer, Rgb};
33
use mp4m::mp4_processor::{
44
AudioConfig, Mp4Processor, Mp4ProcessorConfigBuilder, VideoConfig, VideoFrameType,
55
};
6-
use recorder::{self, EncodedFrame, FPS, VideoEncoderConfig};
76
use std::{path::PathBuf, thread, time::Duration};
7+
use video_encoder::{EncodedFrame, VideoEncoderConfig};
88

99
fn main() -> Result<(), Box<dyn std::error::Error>> {
1010
env_logger::init();
1111

1212
let audio_file = "data/speaker.wav";
1313
let output_file = "data/tmp/output.mp4";
1414
let (width, height) = (1920, 1080);
15-
let (fps, duration_seconds) = (FPS::Fps25, 10);
16-
let total_frames = fps.to_u32() * duration_seconds;
15+
let (fps, duration_seconds) = (25, 10);
16+
let total_frames = fps * duration_seconds;
1717

1818
// Create red, green, blue images (RGB format)
1919
let red_frame = create_color_frame(width, height, 255, 0, 0);
@@ -43,11 +43,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
4343
// Create video config
4444
let config = Mp4ProcessorConfigBuilder::default()
4545
.save_path(PathBuf::from(output_file))
46-
.video_config(VideoConfig {
47-
width,
48-
height,
49-
fps: fps.to_u32(),
50-
})
46+
.video_config(VideoConfig { width, height, fps })
5147
.build()?;
5248

5349
let mut processor = Mp4Processor::new(config);
@@ -99,7 +95,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
9995
}
10096

10197
let config = VideoEncoderConfig::new(width, height).with_fps(fps);
102-
let mut h264_encoder = recorder::video_encoder_new(config)?;
98+
let mut h264_encoder = video_encoder::new(config)?;
10399
let headers_data = h264_encoder.headers()?;
104100

105101
let processor_thread = thread::spawn(move || {
@@ -109,7 +105,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
109105
});
110106

111107
for frame_num in 0..total_frames {
112-
let img = match (frame_num / fps.to_u32()) % 3 {
108+
let img = match (frame_num / fps) % 3 {
113109
0 => &red_frame,
114110
1 => &green_frame,
115111
2 => &blue_frame,

lib/mp4m/examples/mp4_processor_two_audios_demo.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use image::{ImageBuffer, Rgb};
33
use mp4m::mp4_processor::{
44
AudioConfig, Mp4Processor, Mp4ProcessorConfigBuilder, VideoConfig, VideoFrameType,
55
};
6-
use recorder::{EncodedFrame, FPS, VideoEncoderConfig};
76
use std::{path::PathBuf, thread, time::Duration};
7+
use video_encoder::{EncodedFrame, VideoEncoderConfig};
88

99
fn main() -> Result<(), Box<dyn std::error::Error>> {
1010
env_logger::init();
@@ -13,8 +13,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
1313
let audio_file2 = "data/input.wav";
1414
let output_file = "data/tmp/output.mp4";
1515
let (width, height) = (1920, 1080);
16-
let (fps, duration_seconds) = (FPS::Fps25, 10);
17-
let total_frames = fps.to_u32() * duration_seconds;
16+
let (fps, duration_seconds) = (25, 10);
17+
let total_frames = fps * duration_seconds;
1818

1919
// Create red, green, blue images (RGB format)
2020
let red_frame = create_color_frame(width, height, 255, 0, 0);
@@ -63,11 +63,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
6363
// Create video config
6464
let config = Mp4ProcessorConfigBuilder::default()
6565
.save_path(PathBuf::from(output_file))
66-
.video_config(VideoConfig {
67-
width,
68-
height,
69-
fps: fps.to_u32(),
70-
})
66+
.video_config(VideoConfig { width, height, fps })
7167
.build()?;
7268

7369
let mut processor = Mp4Processor::new(config);
@@ -178,14 +174,14 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
178174

179175
// Generate and send video frames
180176
let config = VideoEncoderConfig::new(width, height).with_fps(fps);
181-
let mut h264_encoder = recorder::video_encoder_new(config)?;
177+
let mut h264_encoder = video_encoder::new(config)?;
182178
let headers_data = h264_encoder.headers()?;
183179
if let Err(e) = video_sender.send(VideoFrameType::Frame(headers_data)) {
184180
panic!("video sender h264 header failed: {e}");
185181
}
186182

187183
for frame_num in 0..total_frames {
188-
let img = match (frame_num / fps.to_u32()) % 3 {
184+
let img = match (frame_num / fps) % 3 {
189185
0 => &red_frame,
190186
1 => &green_frame,
191187
2 => &blue_frame,

lib/mp4m/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ pub use audio_processor::{
66
AudioProcessor, AudioProcessorConfigBuilder, OutputDestination, sample_rate,
77
};
88
pub use mp4_processor::{
9-
AudioConfig, Mp4Processor, Mp4ProcessorConfigBuilder, VIDEO_TIMESCALE, VideoConfig,
10-
VideoFrameType,
9+
AudioConfig, Mp4Processor, Mp4ProcessorConfigBuilder, VideoConfig, VideoFrameType,
1110
};
1211
pub use sample_type::{I24, SampleType};
1312

lib/mp4m/src/mp4_processor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use mp4::{
88
};
99
use std::{fs::File, io::BufWriter, path::PathBuf};
1010
use thiserror::Error;
11+
use video_encoder::VIDEO_TIMESCALE;
1112

12-
pub const VIDEO_TIMESCALE: u32 = 90000; // Standard video timescale (90kHz) for better compatibility
1313
const DEFAULT_PPS: [u8; 6] = [0x68, 0xeb, 0xe3, 0xcb, 0x22, 0xc0];
1414
const DEFAULT_SPS: [u8; 25] = [
1515
0x67, 0x64, 0x00, 0x1e, 0xac, 0xd9, 0x40, 0xa0, 0x2f, 0xf9, 0x70, 0x11, 0x00, 0x00, 0x03, 0x03,

lib/recorder/Cargo.toml

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,12 @@ thiserror.workspace = true
2222
once_cell.workspace = true
2323
spin_sleep.workspace = true
2424
nnnoiseless.workspace = true
25+
video-encoder.workspace = true
2526
derive_setters.workspace = true
2627
screen-capture.workspace = true
27-
openh264 = { workspace = true, optional = true }
28-
yuv = { workspace = true, features = ["rayon"] }
29-
ffmpeg-next = { workspace = true, optional = true }
3028
fast_image_resize = { workspace = true, features = ["rayon"] }
3129

3230
[target.'cfg(target_os = "linux")'.dependencies]
33-
x264 = { workspace = true, optional = true }
3431
pipewire.workspace = true
3532
screen-capture-wayland-wlr = { workspace = true, optional = true }
3633
screen-capture-wayland-portal = { workspace = true, optional = true }
@@ -64,14 +61,10 @@ env_logger.workspace = true
6461

6562
[features]
6663
default = []
67-
windows = ["ffmpeg-video-encoder"]
68-
wayland-wlr = ["dep:screen-capture-wayland-wlr", "x264-video-encoder"]
69-
wayland-portal = ["dep:screen-capture-wayland-portal", "x264-video-encoder"]
64+
windows = ["video-encoder/ffmpeg"]
65+
wayland-wlr = ["dep:screen-capture-wayland-wlr", "video-encoder/x264"]
66+
wayland-portal = ["dep:screen-capture-wayland-portal", "video-encoder/x264"]
7067

7168
# For debug on Linux
72-
# wayland-wlr = ["dep:screen-capture-wayland-wlr", "ffmpeg-video-encoder"]
73-
# wayland-wlr = ["dep:screen-capture-wayland-wlr", "openh264-video-encoder"]
74-
75-
x264-video-encoder = ["dep:x264"]
76-
openh264-video-encoder = ["dep:openh264"]
77-
ffmpeg-video-encoder = ["dep:ffmpeg-next"]
69+
# wayland-wlr = ["dep:screen-capture-wayland-wlr", "video-encoder/ffmpeg"]
70+
# wayland-wlr = ["dep:screen-capture-wayland-wlr", "video-encoder/openh264"]

lib/recorder/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub enum RecorderError {
1212
ImageProcessingFailed(String),
1313

1414
#[error("Video encoding failed: {0}")]
15-
VideoEncodingFailed(String),
15+
VideoEncodingFailed(#[from] video_encoder::EncoderError),
1616

1717
#[error("Video decoding failed: {0}")]
1818
VideoDecodingFailed(String),

lib/recorder/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ mod error;
77
mod recorder;
88
mod resolution;
99
mod speaker_recorder;
10-
mod video_encoder;
1110

1211
pub use audio_level::*;
1312
pub use audio_recorder::{AudioDeviceInfo, AudioRecorder, AudioRecorderError};

0 commit comments

Comments
 (0)