### Write Video Data (Default FPS) Source: https://context7.com/darkskygit/minimp4.rs/llms.txt Writes raw H.264 or H.265 NAL unit data to the MP4 file at a default framerate of 60 fps. Ensure input data contains complete NAL units with start codes. ```rust use minimp4::Mp4Muxer; use std::fs::File; use std::io::Read; let mut muxer = Mp4Muxer::new(File::create("output.mp4").unwrap()); muxer.init_video(1280, 720, false, "video"); // Read raw H.264 data from file let mut h264_data = Vec::new(); File::open("input.264").unwrap().read_to_end(&mut h264_data).unwrap(); // Write video at default 60 fps muxer.write_video(&h264_data); muxer.close(); ``` -------------------------------- ### Mux H.264 Stream to MP4 using minimp4.rs Source: https://github.com/darkskygit/minimp4.rs/blob/master/README.md Use this snippet to create an MP4 file from a H.264 stream. Ensure the input file exists and the output file path is valid. The `init_video` function sets up the video track with specified dimensions and title. ```rust let mut mp4muxer = Mp4Muxer::new(File::create("1.mp4").unwrap()); let mut buf = Vec::new(); File::open("1.264").unwrap().read_to_end(&mut buf).unwrap(); mp4muxer.init_video(316, 342, false, "title"); mp4muxer.write_video(&buf); mp4muxer.close(); ``` -------------------------------- ### Create MP4 Muxer Instance Source: https://context7.com/darkskygit/minimp4.rs/llms.txt Instantiates an Mp4Muxer that can write to any `Write + Seek` implementation, such as files or memory buffers. This is the primary entry point for creating MP4 files. ```rust use minimp4::Mp4Muxer; use std::fs::File; use std::io::Cursor; // Create a muxer writing to a file let file = File::create("output.mp4").unwrap(); let mut muxer = Mp4Muxer::new(file); // Or create a muxer writing to an in-memory buffer let buffer = Cursor::new(Vec::new()); let mut muxer = Mp4Muxer::new(buffer); ``` -------------------------------- ### Mp4Muxer::new Source: https://context7.com/darkskygit/minimp4.rs/llms.txt Creates a new MP4 muxer instance that wraps any writer implementing `Write + Seek`. This is the main entry point for creating MP4 files. ```APIDOC ## Mp4Muxer::new ### Description Creates a new MP4 muxer instance that wraps any writer implementing `Write + Seek`. The muxer is the main entry point for creating MP4 files and manages the underlying minimp4 state. ### Method `new` ### Endpoint N/A (Rust function) ### Parameters None ### Request Example ```rust use minimp4::Mp4Muxer; use std::fs::File; use std::io::Cursor; // Create a muxer writing to a file let file = File::create("output.mp4").unwrap(); let mut muxer = Mp4Muxer::new(file); // Or create a muxer writing to an in-memory buffer let buffer = Cursor::new(Vec::new()); let mut muxer = Mp4Muxer::new(buffer); ``` ### Response N/A (Rust struct instance) ### Response Example N/A ``` -------------------------------- ### Initialize Audio Track (AAC Feature) Source: https://context7.com/darkskygit/minimp4.rs/llms.txt Initializes an audio track with specified bit rate, sample rate, and channel count. This requires the `aac` feature to be enabled in `Cargo.toml` and must be called before `write_video_with_audio`. ```rust // Enable in Cargo.toml: minimp4 = { version = "0.2", features = ["aac"] } use minimp4::Mp4Muxer; use std::io::Cursor; let mut muxer = Mp4Muxer::new(Cursor::new(Vec::new())); muxer.init_video(1280, 720, false, "video"); // Initialize audio track: 128kbps, 44.1kHz sample rate, stereo (2 channels) muxer.init_audio(128000, 44100, 2); ``` -------------------------------- ### Mp4Muxer::init_video Source: https://context7.com/darkskygit/minimp4.rs/llms.txt Initializes a video track with specified dimensions, codec type, and track name. Must be called before writing video data. ```APIDOC ## Mp4Muxer::init_video ### Description Initializes a video track with the specified dimensions, codec type, and track name. This must be called before writing any video data. Set `is_hevc` to `true` for H.265/HEVC streams or `false` for H.264/AVC streams. ### Method `init_video` ### Endpoint N/A (Rust function) ### Parameters - **width** (u32) - Required - The width of the video. - **height** (u32) - Required - The height of the video. - **is_hevc** (bool) - Required - Set to `true` for H.265/HEVC, `false` for H.264/AVC. - **track_name** (&str) - Required - The name of the video track. ### Request Example ```rust use minimp4::Mp4Muxer; use std::io::Cursor; let mut muxer = Mp4Muxer::new(Cursor::new(Vec::new())); // Initialize H.264 video track at 1280x720 resolution muxer.init_video(1280, 720, false, "Main Video Track"); // For H.265/HEVC video, set is_hevc to true // muxer.init_video(1920, 1080, true, "HEVC Video"); ``` ### Response N/A (Modifies muxer state) ### Response Example N/A ``` -------------------------------- ### Mp4Muxer::init_audio (AAC Feature) Source: https://context7.com/darkskygit/minimp4.rs/llms.txt Initializes an audio track with the specified bit rate, sample rate, and channel count. Requires the `aac` feature to be enabled. ```APIDOC ## Mp4Muxer::init_audio (AAC Feature) ### Description Initializes an audio track with the specified bit rate, sample rate, and channel count. Requires the `aac` feature to be enabled. This must be called before `write_video_with_audio`. ### Method `init_audio` ### Endpoint N/A (Rust function) ### Parameters - **bitrate** (u32) - Required - The audio bitrate in bits per second. - **sample_rate** (u32) - Required - The audio sample rate in Hz. - **channels** (u32) - Required - The number of audio channels. ### Request Example ```rust // Enable in Cargo.toml: minimp4 = { version = "0.2", features = ["aac"] } use minimp4::Mp4Muxer; use std::io::Cursor; let mut muxer = Mp4Muxer::new(Cursor::new(Vec::new())); muxer.init_video(1280, 720, false, "video"); // Initialize audio track: 128kbps, 44.1kHz sample rate, stereo (2 channels) muxer.init_audio(128000, 44100, 2); ``` ### Response N/A (Modifies muxer state) ### Response Example N/A ``` -------------------------------- ### Mux Video with Audio Source: https://context7.com/darkskygit/minimp4.rs/llms.txt Writes video and audio data simultaneously using the AAC feature. Requires prior initialization of both video and audio tracks. ```rust // Enable in Cargo.toml: minimp4 = { version = "0.2", features = ["aac"] } use minimp4::Mp4Muxer; use std::fs::File; use std::io::{Cursor, Read}; let mut buffer = Cursor::new(Vec::new()); let mut muxer = Mp4Muxer::new(&mut buffer); // Read video and audio data let mut h264_data = Vec::new(); File::open("input.264").unwrap().read_to_end(&mut h264_data).unwrap(); let mut pcm_data = Vec::new(); File::open("input.pcm").unwrap().read_to_end(&mut pcm_data).unwrap(); // Initialize video and audio tracks muxer.init_video(1280, 720, false, "h264 stream"); muxer.init_audio(128000, 44100, 2); // 128kbps, 44.1kHz, stereo // Write video at 25 fps with synchronized audio muxer.write_video_with_audio(&h264_data, 25, &pcm_data); muxer.write_comment("Video with audio"); muxer.close(); ``` -------------------------------- ### Initialize Video Track Source: https://context7.com/darkskygit/minimp4.rs/llms.txt Initializes a video track for H.264 or H.265 streams. This must be called before writing any video data. Specify dimensions, codec type (HEVC or AVC), and a track name. ```rust use minimp4::Mp4Muxer; use std::io::Cursor; let mut muxer = Mp4Muxer::new(Cursor::new(Vec::new())); // Initialize H.264 video track at 1280x720 resolution muxer.init_video(1280, 720, false, "Main Video Track"); // For H.265/HEVC video, set is_hevc to true // muxer.init_video(1920, 1080, true, "HEVC Video"); ``` -------------------------------- ### Mux H.264 Video Source: https://context7.com/darkskygit/minimp4.rs/llms.txt Demonstrates muxing a raw H.264 NAL unit stream into an MP4 file with metadata. ```rust use minimp4::Mp4Muxer; use std::fs::File; use std::io::{Cursor, Read}; fn main() { // Create output buffer (or use File::create for file output) let mut buffer = Cursor::new(Vec::new()); let mut mp4muxer = Mp4Muxer::new(&mut buffer); // Read raw H.264 NAL units from file let mut h264_data = Vec::new(); File::open("input.264") .expect("Failed to open input file") .read_to_end(&mut h264_data) .expect("Failed to read input file"); // Initialize video track: width=1280, height=720, H.264 codec mp4muxer.init_video(1280, 720, false, "Main Video"); // Write video data at 25 fps mp4muxer.write_video_with_fps(&h264_data, 25); // Add metadata mp4muxer.write_comment("Encoded with minimp4.rs"); // Finalize the MP4 file mp4muxer.close(); // Get the final MP4 bytes let mp4_bytes = buffer.into_inner(); println!("Generated MP4 size: {} bytes", mp4_bytes.len()); } ``` -------------------------------- ### Write Video Data (Custom FPS) Source: https://context7.com/darkskygit/minimp4.rs/llms.txt Writes raw video NAL unit data with a specified custom framerate. This offers more control over the output video timing. Common framerates like 25 or 30 fps can be used. ```rust use minimp4::Mp4Muxer; use std::fs::File; use std::io::Read; let mut muxer = Mp4Muxer::new(File::create("output.mp4").unwrap()); muxer.init_video(1920, 1080, false, "h264 stream"); // Read raw H.264 data let mut h264_data = Vec::new(); File::open("input.264").unwrap().read_to_end(&mut h264_data).unwrap(); // Write video at 25 fps (common for PAL video) muxer.write_video_with_fps(&h264_data, 25); // Or at 30 fps (common for NTSC video) // muxer.write_video_with_fps(&h264_data, 30); muxer.close(); ``` -------------------------------- ### Mux H.265/HEVC Video Source: https://context7.com/darkskygit/minimp4.rs/llms.txt Demonstrates muxing a raw H.265/HEVC video stream into an MP4 file by setting the is_hevc flag to true. ```rust use minimp4::Mp4Muxer; use std::fs::File; use std::io::{Cursor, Read}; fn main() { let mut buffer = Cursor::new(Vec::new()); let mut mp4muxer = Mp4Muxer::new(&mut buffer); // Read raw H.265 NAL units from file let mut h265_data = Vec::new(); File::open("input.265") .expect("Failed to open HEVC file") .read_to_end(&mut h265_data) .expect("Failed to read HEVC file"); // Initialize video track with is_hevc=true for H.265 mp4muxer.init_video(1920, 1080, true, "HEVC Video Stream"); // Write video data at 30 fps mp4muxer.write_video_with_fps(&h265_data, 30); // Add metadata comment mp4muxer.write_comment("HEVC video encoded with minimp4.rs"); // Finalize mp4muxer.close(); let mp4_bytes = buffer.into_inner(); println!("Generated HEVC MP4 size: {} bytes", mp4_bytes.len()); } ``` -------------------------------- ### Mp4Muxer::write_video Source: https://context7.com/darkskygit/minimp4.rs/llms.txt Writes raw H.264 or H.265 NAL unit data to the MP4 file at a default framerate of 60 fps. ```APIDOC ## Mp4Muxer::write_video ### Description Writes raw H.264 or H.265 NAL unit data to the MP4 file at a default framerate of 60 fps. The input data should contain complete NAL units with start codes (0x00 0x00 0x01 or 0x00 0x00 0x00 0x01). ### Method `write_video` ### Endpoint N/A (Rust function) ### Parameters - **nalu_data** (&[u8]) - Required - A byte slice containing the raw NAL unit data. ### Request Example ```rust use minimp4::Mp4Muxer; use std::fs::File; use std::io::Read; let mut muxer = Mp4Muxer::new(File::create("output.mp4").unwrap()); muxer.init_video(1280, 720, false, "video"); // Read raw H.264 data from file let mut h264_data = Vec::new(); File::open("input.264").unwrap().read_to_end(&mut h264_data).unwrap(); // Write video data at default 60 fps muxer.write_video(&h264_data); muxer.close(); ``` ### Response N/A (Appends data to the muxer) ### Response Example N/A ``` -------------------------------- ### Mp4Muxer::write_video_with_fps Source: https://context7.com/darkskygit/minimp4.rs/llms.txt Writes raw video NAL unit data to the MP4 file with a custom framerate. ```APIDOC ## Mp4Muxer::write_video_with_fps ### Description Writes raw video NAL unit data to the MP4 file with a custom framerate. This provides more control over the output video timing compared to the default `write_video` method. ### Method `write_video_with_fps` ### Endpoint N/A (Rust function) ### Parameters - **nalu_data** (&[u8]) - Required - A byte slice containing the raw NAL unit data. - **fps** (f64) - Required - The custom framerate for the video. ### Request Example ```rust use minimp4::Mp4Muxer; use std::fs::File; use std::io::Read; let mut muxer = Mp4Muxer::new(File::create("output.mp4").unwrap()); muxer.init_video(1920, 1080, false, "h264 stream"); // Read raw H.264 data let mut h264_data = Vec::new(); File::open("input.264").unwrap().read_to_end(&mut h264_data).unwrap(); // Write video at 25 fps (common for PAL video) muxer.write_video_with_fps(&h264_data, 25.0); // Or at 30 fps (common for NTSC video) // muxer.write_video_with_fps(&h264_data, 30.0); muxer.close(); ``` ### Response N/A (Appends data to the muxer) ### Response Example N/A ``` -------------------------------- ### Add Metadata Comment Source: https://context7.com/darkskygit/minimp4.rs/llms.txt Sets a text comment metadata field in the MP4 file. Use this to add descriptive information, copyright notices, or other metadata to the output file. ```rust use minimp4::Mp4Muxer; use std::io::Cursor; let mut muxer = Mp4Muxer::new(Cursor::new(Vec::new())); muxer.init_video(1280, 720, false, "video"); muxer.write_video(&[0u8; 100]); // sample data // Add metadata comment to the MP4 file muxer.write_comment("Created with minimp4.rs - https://github.com/darkskygit/minimp4.rs"); muxer.close(); ``` -------------------------------- ### Encode AAC Audio Source: https://context7.com/darkskygit/minimp4.rs/llms.txt Encodes PCM samples to AAC using the FDK-AAC wrapper. Requires the aac feature and supports constant or variable bit rate configurations. ```rust // Enable in Cargo.toml: minimp4 = { version = "0.2", features = ["aac"] } use minimp4::enc::{BitRate, Encoder, EncoderParams}; // Configure encoder parameters let params = EncoderParams { bit_rate: BitRate::Cbr(128000), // 128 kbps constant bit rate // Or use VBR: BitRate::VbrMedium sample_rate: 44100, // 44.1 kHz channel_count: 2, // Stereo }; // Create encoder let encoder = Encoder::new(params).expect("Failed to create AAC encoder"); // Get encoder info (contains ASC for MP4 muxing) let info = encoder.info().expect("Failed to get encoder info"); // Encode PCM samples (16-bit signed integers) let input_samples: Vec = vec![0; 2048]; // 2048 samples for stereo let mut output_buffer = vec![0u8; 2048]; match encoder.encode(&input_samples, &mut output_buffer) { Ok(encode_info) => { println!("Input consumed: {} samples", encode_info.input_consumed); println!("Output size: {} bytes", encode_info.output_size); let aac_frame = &output_buffer[..encode_info.output_size]; // Use aac_frame data... } Err(e) => { eprintln!("Encoding error: {}", e); } } ``` -------------------------------- ### Mp4Muxer::write_comment Source: https://context7.com/darkskygit/minimp4.rs/llms.txt Sets a text comment metadata field in the MP4 file. ```APIDOC ## Mp4Muxer::write_comment ### Description Sets a text comment metadata field in the MP4 file. This can be used to add descriptive information, copyright notices, or other metadata to the output file. ### Method `write_comment` ### Endpoint N/A (Rust function) ### Parameters - **comment** (&str) - Required - The text comment to add. ### Request Example ```rust use minimp4::Mp4Muxer; use std::io::Cursor; let mut muxer = Mp4Muxer::new(Cursor::new(Vec::new())); muxer.init_video(1280, 720, false, "video"); muxer.write_video(&[0u8; 100]); // sample data // Add metadata comment to the MP4 file muxer.write_comment("Created with minimp4.rs - https://github.com/darkskygit/minimp4.rs"); muxer.close(); ``` ### Response N/A (Adds metadata to the muxer) ### Response Example N/A ``` -------------------------------- ### Mp4Muxer::close Source: https://context7.com/darkskygit/minimp4.rs/llms.txt Finalizes and closes the MP4 muxer, writing all remaining data and the MP4 file structure. This must be called to produce a valid MP4 file. ```APIDOC ## Mp4Muxer::close ### Description Finalizes and closes the MP4 muxer, writing all remaining data and the MP4 file structure. This must be called to produce a valid MP4 file. Returns a reference to the underlying writer. ### Method `close` ### Endpoint N/A (Rust function) ### Parameters None ### Request Example ```rust use minimp4::Mp4Muxer; use std::io::Cursor; let buffer = Cursor::new(Vec::new()); let mut muxer = Mp4Muxer::new(buffer); muxer.init_video(1280, 720, false, "video"); muxer.write_video(&[0u8; 100]); muxer.write_comment("test"); // Close the muxer and get the final MP4 data let writer = muxer.close(); // Access the underlying buffer if using Cursor // let mp4_bytes = writer.into_inner(); ``` ### Response Returns the underlying writer (`W` where `W: Write + Seek`). ### Response Example N/A ``` -------------------------------- ### Close MP4 Muxer Source: https://context7.com/darkskygit/minimp4.rs/llms.txt Finalizes and closes the MP4 muxer, writing the necessary file structure. This method must be called to produce a valid MP4 file and returns a reference to the underlying writer. ```rust use minimp4::Mp4Muxer; use std::io::Cursor; let buffer = Cursor::new(Vec::new()); let mut muxer = Mp4Muxer::new(buffer); muxer.init_video(1280, 720, false, "video"); muxer.write_video(&[0u8; 100]); muxer.write_comment("test"); // Close the muxer and get the final MP4 data let writer = muxer.close(); // Access the underlying buffer if using Cursor // let mp4_bytes = writer.into_inner(); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.