Skip to content

🎥 Make videos programmatically with React

first example

Jonny Burger238a377198bd
diff --git a/packages/example/public/transparent-with-dar.webm b/packages/example/public/transparent-with-dar.webm
new file mode 100644
index 0000000000..4fa3909933
Binary files /dev/null and b/packages/example/public/transparent-with-dar.webm differ
diff --git a/packages/renderer/rust/opened_stream.rs b/packages/renderer/rust/opened_stream.rs
index adcc90c1cb..3e2a70492e 100644
--- a/packages/renderer/rust/opened_stream.rs
+++ b/packages/renderer/rust/opened_stream.rs
@@ -1,7 +1,7 @@
use std::{io::ErrorKind, time::SystemTime};
use ffmpeg_next::Rational;
-use remotionffmpeg::{codec::Id, frame::Video, media::Type, Dictionary, StreamMut};
+use remotionffmpeg::{codec::Id, frame::Video, media::Type, Dictionary};
extern crate ffmpeg_next as remotionffmpeg;
use std::time::UNIX_EPOCH;
@@ -412,24 +412,14 @@ impl OpenedStream {
}
}
-pub fn calculate_display_video_size(dar_x: i32, dar_y: i32, x: u32, y: u32) -> (u32, u32) {
- if dar_x == 0 || dar_y == 0 {
- return (x, y);
+// https://chat.openai.com/share/9d7d5ebe-555a-46df-a339-691e1a7f83eb
+pub fn calculate_display_video_size(sar_x: i32, sar_y: i32, width: u32, height: u32) -> (u32, u32) {
+ if sar_x == 0 || sar_y == 0 {
+ return (width, height);
}
- 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 get_display_aspect_ratio(mut_stream: &StreamMut) -> Rational {
- unsafe {
- let asp = mut_stream.get_display_aspect_ratio();
- return Rational::new(asp.numerator(), asp.denominator());
- }
+ let new_width = (width as f64 * (sar_x as f64 / sar_y as f64) as f64).round() as u32;
+ (new_width, height)
}
pub fn open_stream(
@@ -514,11 +504,15 @@ pub fn open_stream(
let original_height = decoder.height();
let fps = mut_stream.avg_frame_rate();
- let aspect_ratio = get_display_aspect_ratio(&mut_stream);
+ let sample_aspect_ratio: Rational;
+ unsafe {
+ let ar = (*mut_stream.as_ptr()).sample_aspect_ratio;
+ sample_aspect_ratio = Rational::new(ar.num, ar.den);
+ }
let (scaled_width, scaled_height) = calculate_display_video_size(
- aspect_ratio.0,
- aspect_ratio.1,
+ sample_aspect_ratio.0,
+ sample_aspect_ratio.1,
original_width,
original_height,
);
diff --git a/packages/renderer/rust/scalable_frame.rs b/packages/renderer/rust/scalable_frame.rs
index 6d224c07d3..45a24fd7e7 100644
--- a/packages/renderer/rust/scalable_frame.rs
+++ b/packages/renderer/rust/scalable_frame.rs
@@ -222,38 +222,47 @@ pub fn scale_and_make_bitmap(
&mut scaled,
)?;
+ let is_transparent_pixel_format = src_format == Pixel::YUVA420P
+ || src_format == Pixel::YUVA444P10LE
+ || src_format == Pixel::YUVA444P12LE;
+
+ let channels = match is_transparent_pixel_format {
+ true => 4,
+ false => 3,
+ } as usize;
+
let (rotated, rotated_width, rotated_height, stride) = match native_frame.rotate {
Rotate::Rotate90 => rotate_90(
scaled.data(0),
native_frame.scaled_width,
native_frame.scaled_height,
scaled.stride(0),
+ channels,
),
Rotate::Rotate180 => rotate_180(
scaled.data(0),
native_frame.scaled_width,
native_frame.scaled_height,
scaled.stride(0),
+ channels,
),
Rotate::Rotate270 => rotate_270(
scaled.data(0),
native_frame.scaled_width,
native_frame.scaled_height,
scaled.stride(0),
+ channels,
),
- Rotate::Rotate0 => (
- scaled.data(0).to_vec(),
+ Rotate::Rotate0 => rotate_0(
+ scaled.data(0),
native_frame.scaled_width,
native_frame.scaled_height,
scaled.stride(0),
+ channels,
),
};
if transparent {
- let is_transparent_pixel_format = src_format == Pixel::YUVA420P
- || src_format == Pixel::YUVA444P10LE
- || src_format == Pixel::YUVA444P12LE;
-
if is_transparent_pixel_format {
return get_png_data(&rotated, rotated_width, rotated_height);
} else {
@@ -284,28 +293,57 @@ pub fn rotate_270(
width: u32,
height: u32,
stride: usize,
+ channels: usize,
) -> (Vec<u8>, u32, u32, usize) {
- let new_stride = (height as usize * 3 + 3) & !3; // This ensures the new stride is a multiple of 4 for alignment
+ let new_stride = (height as usize * channels + channels) & !channels; // This ensures the new stride is a multiple of channels for alignment
let mut new_data: Vec<u8> = vec![0; new_stride * width as usize];
for y in 0..height {
for x in 0..width {
let new_x = y;
let new_y = width - x - 1;
- let new_index = (new_y * height + new_x) as usize * 3;
- let old_index = y as usize * stride + x as usize * 3;
- new_data[new_index..new_index + 3].copy_from_slice(&data[old_index..old_index + 3]);
+ let new_index = (new_y * height + new_x) as usize * channels;
+ let old_index = y as usize * stride + x as usize * channels;
+ new_data[new_index..new_index + channels]
+ .copy_from_slice(&data[old_index..old_index + channels]);
}
}
(new_data, height, width, new_stride)
}
+pub fn rotate_0(
+ data: &[u8],
+ width: u32,
+ height: u32,
+ stride: usize,
+ channels: usize,
+) -> (Vec<u8>, u32, u32, usize) {
+ let new_stride = width as usize * channels;
+ if new_stride == stride {
+ return (data.to_vec(), width, height, stride);
+ }
+
+ let mut new_data: Vec<u8> = vec![0; new_stride * height as usize];
+
+ for y in 0..height {
+ for x in 0..width {
+ let new_index = (y as usize) * (new_stride as usize) + x as usize * channels;
+ let old_index = y as usize * stride + x as usize * channels;
+ new_data[new_index..new_index + channels]
+ .copy_from_slice(&data[old_index..old_index + channels]);
+ }
+ }
+
+ (new_data, width, height, new_stride)
+}
+
pub fn rotate_180(
data: &[u8],
width: u32,
height: u32,
stride: usize,
+ channels: usize,
) -> (Vec<u8>, u32, u32, usize) {
let mut new_data: Vec<u8> = vec![0; stride * height as usize];
@@ -313,9 +351,10 @@ pub fn rotate_180(
for x in 0..width {
let new_x = width - x - 1;
let new_y = height - y - 1;
- let new_index = (new_y as usize * stride) + (new_x as usize * 3);
- let old_index = (y as usize * stride) + (x as usize * 3);
- new_data[new_index..new_index + 3].copy_from_slice(&data[old_index..old_index + 3]);
+ let new_index = (new_y as usize * stride) + (new_x as usize * channels);
+ let old_index = (y as usize * stride) + (x as usize * channels);
+ new_data[new_index..new_index + channels]
+ .copy_from_slice(&data[old_index..old_index + channels]);
}
}
@@ -327,17 +366,19 @@ pub fn rotate_90(
width: u32,
height: u32,
stride: usize,
+ channels: usize,
) -> (Vec<u8>, u32, u32, usize) {
- let new_stride = (height as usize * 3 + 3) & !3; // This ensures the new stride is a multiple of 4 for alignment
+ let new_stride = (height as usize * channels + channels) & !channels; // This ensures the new stride is a multiple of 4 for alignment
let mut new_data: Vec<u8> = vec![0; new_stride * width as usize];
for y in 0..height {
for x in 0..width {
let new_x = height - y - 1;
let new_y = x;
- let new_index = (new_y as usize * new_stride) + (new_x as usize * 3);
- let old_index = (y as usize * stride) + (x as usize * 3);
- new_data[new_index..new_index + 3].copy_from_slice(&data[old_index..old_index + 3]);
+ let new_index = (new_y as usize * new_stride) + (new_x as usize * channels);
+ let old_index = (y as usize * stride) + (x as usize * channels);
+ new_data[new_index..new_index + channels]
+ .copy_from_slice(&data[old_index..old_index + channels]);
}
}
diff --git a/packages/renderer/src/test/example-videos.ts b/packages/renderer/src/test/example-videos.ts
index cf7ae5b7e0..99df51b2ff 100644
--- a/packages/renderer/src/test/example-videos.ts
+++ b/packages/renderer/src/test/example-videos.ts
@@ -24,4 +24,9 @@ export const exampleVideos = {
music: path.join(examplePackage, 'public', 'music.mp3'),
notavideo: path.join(examplePackage, 'public', 'giphy.gif'),
notafile: path.join(examplePackage, 'public', 'doesnotexist'),
+ transparentwithdar: path.join(
+ examplePackage,
+ 'public',
+ 'transparent-with-dar.webm',
+ ),
};
diff --git a/packages/renderer/src/test/extract-frame-rust.test.ts b/packages/renderer/src/test/extract-frame-rust.test.ts
index 8f91c733c1..47beff3426 100644
--- a/packages/renderer/src/test/extract-frame-rust.test.ts
+++ b/packages/renderer/src/test/extract-frame-rust.test.ts
@@ -228,10 +228,48 @@ test(
{timeout: 20000},
);
-test('Should be able to extract a frame with abnormal DAR', async () => {
+test(
+ 'Should get a frame of a transparent video with a custom DAR',
+ async () => {
+ const compositor = startLongRunningCompositor({
+ maximumFrameCacheItemsInBytes: null,
+ logLevel: 'info',
+ indent: false,
+ binariesDirectory: null,
+ });
+
+ const data = await compositor.executeCommand('ExtractFrame', {
+ src: exampleVideos.transparentwithdar,
+ original_src: exampleVideos.transparentwithdar,
+ time: 0.5,
+ transparent: false,
+ tone_mapped: true,
+ });
+
+ // Expected length fixing
+ expect(data.length).toBe(1050678);
+
+ const transparentdata = await compositor.executeCommand('ExtractFrame', {
+ src: exampleVideos.transparentwithdar,
+ original_src: exampleVideos.transparentwithdar,
+ time: 0.5,
+ transparent: true,
+ tone_mapped: true,
+ });
+
+ // Expected length fixing
+ expect(transparentdata.length).toBe(174887);
+
+ await compositor.finishCommands();
+ await compositor.waitForDone();
+ },
+ {timeout: 20000},
+);
+
+test.only('Should be able to extract a frame with abnormal DAR', async () => {
const compositor = startLongRunningCompositor({
maximumFrameCacheItemsInBytes: null,
- logLevel: 'info',
+ logLevel: 'verbose',
indent: false,
binariesDirectory: null,
});