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
impl Ap4CencDecryptingProcessor
Sourcepub fn new() -> Ap4CencDecryptingProcessorBuilder
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()?;Sourcepub fn decrypt<T: AsRef<[u8]>>(
&self,
input_data: T,
init_data: Option<T>,
) -> Result<Vec<u8>, Error>
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.,.m4sfragment)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:
- The input or init data exceeds 4GB (
Error::DataTooLarge) - Decryption fails (
Error::DecryptionFailed)
§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.
Sourcepub fn decrypt_file<T: AsRef<Path>>(
&self,
input_path: T,
output_path: T,
init_path: Option<T>,
) -> Result<(), Error>
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 writteninit_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.