Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Encoding result video with the same time scale as original #47

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions backend/src/ffmpeg/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ pub enum VideoInfoError {
#[error("Failed to read bitrate from video")]
NoBitrate,
#[error("Failed to read video duration from video")]
NoTimeScale,
#[error("Failed to read time scale value from video")]
NoDuration,
#[error("No stream in video file")]
NoStream,
Expand Down
3 changes: 3 additions & 0 deletions backend/src/ffmpeg/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub fn start_video_render(
video_info.width,
video_info.height,
video_info.frame_rate,
video_info.time_base,
render_settings.bitrate_mbps,
&render_settings.encoder,
output_video,
Expand Down Expand Up @@ -116,6 +117,7 @@ pub fn spawn_encoder(
width: u32,
height: u32,
frame_rate: f32,
time_base: u32,
bitrate_mbps: u32,
video_encoder: &Encoder,
output_video: &PathBuf,
Expand All @@ -139,6 +141,7 @@ pub fn spawn_encoder(
.pix_fmt("yuv420p")
.codec_video(&video_encoder.name)
.args(["-b:v", &format!("{}M", bitrate_mbps)])
.args(["-video_track_timescale", time_base.to_string().as_str()])
.overwrite()
.output(output_video.to_str().unwrap());

Expand Down
11 changes: 11 additions & 0 deletions backend/src/ffmpeg/video_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub struct VideoInfo {
pub width: u32,
pub height: u32,
pub frame_rate: f32,
pub time_base: u32,
pub bitrate: u32,
pub duration: Duration,
pub total_frames: u32,
Expand Down Expand Up @@ -57,12 +58,22 @@ impl TryFrom<FfProbe> for VideoInfo {
.ok_or(VideoInfoError::NoDuration)?,
);

let time_base = {
let tbn_string = &stream.time_base;
let mut split = tbn_string.split('/');
split
.nth(1)
.and_then(|num| num.parse::<u32>().ok())
.ok_or(VideoInfoError::NoTimeScale)?
};

let total_frames = (frame_rate * duration.as_secs_f32()) as u32;

Ok(Self {
width,
height,
frame_rate,
time_base,
bitrate,
duration,
total_frames,
Expand Down