Skip to main content

Ap4CencDecryptingProcessor

Struct Ap4CencDecryptingProcessor 

Source
pub struct Ap4CencDecryptingProcessor { /* private fields */ }
Expand description

A CENC (Common Encryption) decrypting processor for MP4 files.

This struct wraps the Bento4 AP4_CencDecryptingProcessor and provides a safe Rust interface for decrypting CENC or CBCS encrypted MP4 content.

§Thread Safety

Ap4CencDecryptingProcessor implements both Send and Sync, meaning it can be safely shared across threads using Arc<Ap4CencDecryptingProcessor>. Internally, all decryption operations are synchronized using a mutex to ensure thread safety.

§Example

use mp4decrypt::Ap4CencDecryptingProcessor;
use std::{fs, sync::Arc, thread};

let processor = Arc::new(
    Ap4CencDecryptingProcessor::new()
        .key("eb676abbcb345e96bbcf616630f1a3da", "100b6c20940f779a4589152b57d2dacb")
        .unwrap()
        .build()
        .unwrap()
);

// Clone Arc for use in another thread
let processor_clone = Arc::clone(&processor);
let handle = thread::spawn(move || {
    // Use processor_clone in this thread
});

Implementations§

Source§

impl Ap4CencDecryptingProcessor

Source

pub fn new() -> Ap4CencDecryptingProcessorBuilder

Creates a new builder for configuring the decryption processor.

Use the builder to add one or more KID/key pairs, then call Ap4CencDecryptingProcessorBuilder::build to create the processor.

§Example
use mp4decrypt::Ap4CencDecryptingProcessor;

let processor = Ap4CencDecryptingProcessor::new()
    .key("eb676abbcb345e96bbcf616630f1a3da", "100b6c20940f779a4589152b57d2dacb")?
    .build()?;
Source

pub fn decrypt<T: AsRef<[u8]>>( &self, input_data: T, init_data: Option<T>, ) -> Result<Vec<u8>, Error>

Decrypts encrypted MP4 data in memory.

This method takes encrypted segment data and an optional initialization segment, and returns the decrypted segment data. The init segment is passed separately to the decryption processor for parsing encryption metadata.

§Arguments
  • input_data - The encrypted MP4 segment data (e.g., .m4s fragment)
  • init_data - Optional initialization segment data (e.g., init.mp4). Required for fragmented MP4 streams to provide encryption metadata.
§Returns

Returns Ok(Vec<u8>) containing the decrypted segment data on success, or an error if:

§Note

The returned data contains only the decrypted segment, not the init segment. To create a playable MP4 file, you must manually prepend the init segment.

§Example
use mp4decrypt::Ap4CencDecryptingProcessor;
use std::{fs, io::Write};

let processor = Ap4CencDecryptingProcessor::new()
    .key("eb676abbcb345e96bbcf616630f1a3da", "100b6c20940f779a4589152b57d2dacb")?
    .build()?;

// With initialization segment
let init = fs::read("video_init.mp4")?;
let segment = fs::read("video_1.m4s")?;
let decrypted = processor.decrypt(&segment, Some(&init))?;

// Create playable MP4 by prepending init segment
let mut f = fs::File::create("output.mp4")?;
f.write_all(&init)?;
f.write_all(&decrypted)?;

// Without initialization segment (raw decrypted segment)
let decrypted_raw = processor.decrypt(&segment, None::<&Vec<u8>>)?;
§Thread Safety

This method is thread-safe and can be called concurrently from multiple threads when the processor is shared via Arc.

Source

pub fn decrypt_file<T: AsRef<Path>>( &self, input_path: T, output_path: T, init_path: Option<T>, ) -> Result<(), Error>

Decrypts an encrypted MP4 file and writes the result to disk.

This method is more memory-efficient than decrypt for large files as it streams data directly from disk rather than loading it all into memory.

§Arguments
  • input_path - Path to the encrypted MP4 segment file (e.g., segment.m4s)
  • output_path - Path where the decrypted data will be written
  • init_path - Optional path to the initialization segment (e.g., init.mp4). Required for proper decryption of fragmented MP4 files.
§Returns

Returns Ok(()) on success, or an error if decryption fails.

§Note

Unlike decrypt, this method does not combine the init and segment data in the output. If you need a playable MP4 file, you must manually concatenate the init segment with the decrypted output:

use mp4decrypt::Ap4CencDecryptingProcessor;
use std::fs;

let processor = Ap4CencDecryptingProcessor::new()
    .key("eb676abbcb345e96bbcf616630f1a3da", "100b6c20940f779a4589152b57d2dacb")?
    .build()?;

// Decrypt the segment
processor.decrypt_file(
    "encrypted_segment.m4s",
    "decrypted_segment.m4s",
    Some("init.mp4"),
)?;

// Create playable MP4 by concatenating init + decrypted segment
let init = fs::read("init.mp4")?;
let segment = fs::read("decrypted_segment.m4s")?;
let mut playable = init;
playable.extend(segment);
fs::write("playable.mp4", playable)?;
§Thread Safety

This method is thread-safe and can be called concurrently from multiple threads when the processor is shared via Arc.

Trait Implementations§

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.