feat: stream groups

This commit is contained in:
2024-11-11 14:10:03 +00:00
parent b66a8db0c6
commit 363ad4f55c
6 changed files with 154 additions and 89 deletions

View File

@ -1,4 +1,4 @@
use crate::{Decoder, Demuxer, DemuxerInfo, Encoder, Muxer, Scaler, StreamInfoChannel};
use crate::{Decoder, Demuxer, DemuxerInfo, Encoder, Muxer, Scaler, StreamInfo};
use anyhow::Result;
use ffmpeg_sys_the_third::{av_frame_free, av_packet_free};
use std::collections::HashMap;
@ -39,7 +39,7 @@ impl Transcoder {
/// a pre-configured output encoder
pub unsafe fn transcode_stream(
&mut self,
in_stream: &StreamInfoChannel,
in_stream: &StreamInfo,
encoder_out: Encoder,
) -> Result<()> {
let src_index = in_stream.index as i32;
@ -47,7 +47,8 @@ impl Transcoder {
let out_ctx = encoder_out.codec_context();
if in_stream.width != (*out_ctx).width as usize
|| in_stream.height != (*out_ctx).height as usize
|| in_stream.format != (*out_ctx).pix_fmt as usize {
|| in_stream.format != (*out_ctx).pix_fmt as isize
{
// Setup scaler if the size/format is different from what the codec expects
self.scalers.insert(src_index, Scaler::new());
}
@ -60,7 +61,7 @@ impl Transcoder {
}
/// Copy a stream from the input to the output
pub unsafe fn copy_stream(&mut self, in_stream: StreamInfoChannel) -> Result<()> {
pub unsafe fn copy_stream(&mut self, in_stream: StreamInfo) -> Result<()> {
let dst_stream = self.muxer.add_copy_stream(in_stream.stream)?;
self.copy_stream
.insert(in_stream.index as i32, (*dst_stream).index);
@ -84,12 +85,16 @@ impl Transcoder {
let src_index = (*stream).index;
// check if encoded stream
if let Some(enc) = self.encoders.get_mut(&src_index) {
for mut frame in self.decoder.decode_pkt(pkt, stream)? {
for mut frame in self.decoder.decode_pkt(pkt)? {
// scale frame before sending to encoder
let mut frame = if let Some(mut sws) = self.scalers.get_mut(&src_index) {
let mut frame = if let Some(sws) = self.scalers.get_mut(&src_index) {
let enc_ctx = enc.codec_context();
let new_frame = sws.process_frame(frame, (*enc_ctx).width as u16, (*enc_ctx).height as u16, (*enc_ctx).pix_fmt)?;
let new_frame = sws.process_frame(
frame,
(*enc_ctx).width as u16,
(*enc_ctx).height as u16,
(*enc_ctx).pix_fmt,
)?;
av_frame_free(&mut frame);
new_frame
} else {
@ -135,7 +140,7 @@ mod tests {
let mut transcoder =
Transcoder::new("test_output/test.mp4", "test_output/test_transcode.mkv")?;
let info = transcoder.prepare()?;
for c in info.channels {
for c in info.streams {
transcoder.copy_stream(c)?;
}
transcoder.run()?;