Skip to content

🎥 Make videos programmatically with React

let's go

Jonny Burger168a6e27a595
diff --git a/packages/Cargo.lock b/packages/Cargo.lock
index 13dac7d060..ead19f8923 100644
--- a/packages/Cargo.lock
+++ b/packages/Cargo.lock
@@ -133,7 +133,7 @@ checksum = "7fcaabb2fef8c910e7f4c7ce9f67a1283a1715879a7c230ca9d6d1ae31f16d91"
[[package]]
name = "ffmpeg-next"
version = "6.0.0"
-source = "git+https://github.com/JonnyBurger/rust-ffmpeg?rev=7d1463a2d8237b81e14c8290ce59181f8d2f2374#7d1463a2d8237b81e14c8290ce59181f8d2f2374"
+source = "git+https://github.com/JonnyBurger/rust-ffmpeg?rev=11a21ce6585fbfc4ee3e3b9edd63e44bbd3e8124#11a21ce6585fbfc4ee3e3b9edd63e44bbd3e8124"
dependencies = [
"bitflags",
"ffmpeg-sys-next",
diff --git a/packages/Cargo.toml b/packages/Cargo.toml
index f4e85d7938..e1779e536c 100644
--- a/packages/Cargo.toml
+++ b/packages/Cargo.toml
@@ -15,7 +15,7 @@ jpeg-decoder = "0.3"
jpeg-encoder = "0.5.1"
threadpool = "1.8.1"
lazy_static = "1.4"
-ffmpeg-next = {git = "https://github.com/JonnyBurger/rust-ffmpeg", rev ="7d1463a2d8237b81e14c8290ce59181f8d2f2374"}
+ffmpeg-next = {git = "https://github.com/JonnyBurger/rust-ffmpeg", rev ="11a21ce6585fbfc4ee3e3b9edd63e44bbd3e8124"}
[[bin]]
name = "compositor"
diff --git a/packages/compositor-darwin-arm64/compositor b/packages/compositor-darwin-arm64/compositor
index 53bf3d9dfd..9e3a24c901 100755
Binary files a/packages/compositor-darwin-arm64/compositor and b/packages/compositor-darwin-arm64/compositor differ
diff --git a/packages/renderer/rust/opened_video.rs b/packages/renderer/rust/opened_video.rs
index d55e4c36af..4e44cfa4de 100644
--- a/packages/renderer/rust/opened_video.rs
+++ b/packages/renderer/rust/opened_video.rs
@@ -19,8 +19,10 @@ pub struct LastSeek {
pub struct OpenedVideo {
pub stream_index: usize,
pub time_base: Rational,
- pub width: u32,
- pub height: u32,
+ pub original_width: u32,
+ pub original_height: u32,
+ pub scaled_width: u32,
+ pub scaled_height: u32,
pub format: Pixel,
pub video: remotionffmpeg::codec::decoder::Video,
pub src: String,
@@ -78,8 +80,10 @@ impl OpenedVideo {
linesizes: linesize,
planes,
format: self.format,
- width: self.width,
- height: self.height,
+ original_width: self.original_width,
+ original_height: self.original_height,
+ scaled_height: self.scaled_height,
+ scaled_width: self.scaled_width,
};
let item = FrameCacheItem {
@@ -206,8 +210,10 @@ impl OpenedVideo {
linesizes: linesize,
planes,
format: self.format,
- width: self.width,
- height: self.height,
+ original_height: self.original_height,
+ original_width: self.original_width,
+ scaled_height: self.scaled_height,
+ scaled_width: self.scaled_width,
};
let item = FrameCacheItem {
@@ -249,6 +255,15 @@ impl OpenedVideo {
}
}
+fn calculate_display_video_size(dar_x: i32, dar_y: i32, x: u32, y: u32) -> (u32, u32) {
+ let dimensions = (x * y) as f64;
+ let new_width = (dimensions * (dar_x as f64 / dar_y as f64) as f64).sqrt();
+ let new_height = dimensions / new_width;
+ let height = new_height.round() as u32;
+ let width = new_width.round() as u32;
+ (width, height)
+}
+
pub fn open_video(src: &str) -> Result<OpenedVideo, PossibleErrors> {
let mut dictionary = Dictionary::new();
dictionary.set("fflags", "+genpts");
@@ -269,14 +284,30 @@ pub fn open_video(src: &str) -> Result<OpenedVideo, PossibleErrors> {
let video = context_decoder.decoder().video()?;
let format = video.format();
- let width = video.width();
- let height = video.height();
+
+ let original_width = video.width();
+ let original_height = video.height();
+
+ let mut aspect_ratio: Rational = Rational::new(1, 1);
+ unsafe {
+ let new = mut_stream.get_display_aspect_ratio();
+ aspect_ratio = Rational::new(new.0 .0, new.0 .1);
+ }
+
+ let (scaled_width, scaled_height) = calculate_display_video_size(
+ aspect_ratio.0,
+ aspect_ratio.1,
+ original_width,
+ original_height,
+ );
let opened_video = OpenedVideo {
stream_index,
time_base,
- width,
- height,
+ original_height,
+ original_width,
+ scaled_height,
+ scaled_width,
format,
video,
src: src.to_string(),
diff --git a/packages/renderer/rust/scalable_frame.rs b/packages/renderer/rust/scalable_frame.rs
index fa07f21a77..a0beb8a30d 100644
--- a/packages/renderer/rust/scalable_frame.rs
+++ b/packages/renderer/rust/scalable_frame.rs
@@ -10,8 +10,10 @@ pub struct NotRgbFrame {
pub planes: Vec<Vec<u8>>,
pub linesizes: [i32; 8],
pub format: Pixel,
- pub width: u32,
- pub height: u32,
+ pub original_width: u32,
+ pub original_height: u32,
+ pub scaled_width: u32,
+ pub scaled_height: u32,
}
pub struct RgbFrame {
@@ -105,11 +107,11 @@ fn create_bmp_image_from_frame(rgb_frame: &mut Video) -> Vec<u8> {
pub fn scale_and_make_bitmap(native_frame: &NotRgbFrame) -> Result<Vec<u8>, PossibleErrors> {
let mut scaler = Context::get(
native_frame.format,
- native_frame.width,
- native_frame.height,
+ native_frame.original_width,
+ native_frame.original_height,
Pixel::BGR24,
- native_frame.width,
- native_frame.height,
+ native_frame.scaled_width,
+ native_frame.scaled_height,
Flags::BILINEAR,
)?;
@@ -126,8 +128,8 @@ pub fn scale_and_make_bitmap(native_frame: &NotRgbFrame) -> Result<Vec<u8>, Poss
let mut scaled = Video::empty();
scaler.run(
native_frame.format,
- native_frame.width,
- native_frame.height,
+ native_frame.original_width,
+ native_frame.original_height,
ptr,
native_frame.linesizes.as_ptr(),
&mut scaled,
diff --git a/packages/renderer/src/calculate-sar-dar-pixels.ts b/packages/renderer/src/calculate-sar-dar-pixels.ts
deleted file mode 100644
index 47e78ef47f..0000000000
--- a/packages/renderer/src/calculate-sar-dar-pixels.ts
+++ /dev/null
@@ -1,29 +0,0 @@
-// https://superuser.com/questions/907933/correct-aspect-ratio-without-re-encoding-video-file
-
-export const calculateDisplayVideoSize = ({
- darX,
- darY,
- x,
- y,
-}: {
- x: number;
- y: number;
- darX: number;
- darY: number;
-}): {width: number; height: number} => {
- // We know two equations:
- // newWidth / newHeight = darX / darY
- // and:
- // x * y = (newWidth * newHeight)
-
- // I solved it then on pen and paper and simplified the formula:
-
- const dimensions = x * y;
- const newWidth = Math.sqrt(dimensions * (darX / darY));
- const newHeight = dimensions / newWidth;
-
- return {
- height: Math.round(newHeight),
- width: Math.round(newWidth),
- };
-};
diff --git a/packages/renderer/src/test/extract-frame-rust.test.ts b/packages/renderer/src/test/extract-frame-rust.test.ts
index fc70549023..cce0bdb1d7 100644
--- a/packages/renderer/src/test/extract-frame-rust.test.ts
+++ b/packages/renderer/src/test/extract-frame-rust.test.ts
@@ -170,11 +170,11 @@ test.only('Should be able to extract a frame with abnormal DAR', async () => {
const header = data.slice(0, BMP_HEADER_SIZE);
- const width = header.readInt32LE(18); // Read the width from position 18
- const height = header.readInt32LE(22); // Read the height from position 22
+ const width = header.readInt32LE(18);
+ const height = header.readInt32LE(22);
- expect(width).toBe(720);
expect(height).toBe(1280);
+ expect(width).toBe(720);
compositor.finishCommands();
await compositor.waitForDone();
diff --git a/packages/renderer/src/test/sar-dar.test.ts b/packages/renderer/src/test/sar-dar.test.ts
deleted file mode 100644
index bddea56cbb..0000000000
--- a/packages/renderer/src/test/sar-dar.test.ts
+++ /dev/null
@@ -1,28 +0,0 @@
-import {expect, test} from 'vitest';
-import {calculateDisplayVideoSize} from '../calculate-sar-dar-pixels';
-
-test('Should parse display dimensions based on SAR and DAR correctly', () => {
- const size = calculateDisplayVideoSize({
- x: 1280,
- y: 720,
- darX: 896,
- darY: 1593,
- });
-
- expect(size).toEqual({
- width: 720,
- height: 1280,
- });
-
- const size2 = calculateDisplayVideoSize({
- x: 1920,
- y: 1080,
- darX: 16,
- darY: 9,
- });
-
- expect(size2).toEqual({
- width: 1920,
- height: 1080,
- });
-});