mp4-rs

April 10, 2026 · View on GitHub

shiguredo_mp4 Documentation GitHub Actions License

About Shiguredo's open source software

We will not respond to PRs or issues that have not been discussed on Discord. Also, Discord is only available in Japanese.

Please read https://github.com/shiguredo/oss before use.

時雨堂のオープンソースソフトウェアについて

利用前に https://github.com/shiguredo/oss をお読みください。

概要

Rust で実装された依存 0 かつ Sans I/O な MP4 ファイルの mux/demux ライブラリです。no_std 環境でも利用できます。

特徴

対応コーデック

  • 音声
    • AAC (mp4a)
    • Opus (Opus)
    • FLAC (fLaC)
  • 映像
    • VP8 (vp08)
    • VP9 (vp09)
    • AV1 (av01)
    • H.264 / AVC (avc1)
    • H.265 / HEVC (hev1, hvc1)

使い方

MP4 ファイルのデマルチプレックス

Mp4FileDemuxer を使って MP4 ファイルからトラック情報やサンプルを取得できます。

use shiguredo_mp4::demux::{Input, Mp4FileDemuxer};

// デマルチプレクサーを生成する
let mut demuxer = Mp4FileDemuxer::new();

// 必要なデータを段階的に供給する (Sans I/O)
while let Some(required) = demuxer.required_input() {
    // required.position と required.size に基づいてデータを読み込む
    let data: &[u8] = read_data(required.position, required.size);
    demuxer.handle_input(Input {
        position: required.position,
        data,
    });
}

// トラック情報を取得する
let tracks = demuxer.tracks()?;

// サンプルを時系列順に取得する
while let Ok(Some(sample)) = demuxer.next_sample() {
    // sample.track, sample.timestamp, sample.data_offset, sample.data_size
}

fMP4 セグメントの生成と読み戻し

Fmp4SegmentMuxer でメディアセグメントを生成し、Fmp4SegmentDemuxer で読み戻せます。

use shiguredo_mp4::mux::{Fmp4SegmentMuxer, SegmentSample};
use shiguredo_mp4::demux::Fmp4SegmentDemuxer;

// Muxer でメディアセグメントを生成する
let mut muxer = Fmp4SegmentMuxer::new()?;
let segment = muxer.create_media_segment(&samples)?;
let init_segment = muxer.init_segment_bytes()?;

// Demuxer で読み戻す
let mut demuxer = Fmp4SegmentDemuxer::new();
demuxer.handle_init_segment(&init_segment)?;
let demuxed = demuxer.handle_media_segment(&segment)?;

サンプル

demux

MP4 ファイルのトラック情報とサンプル情報を表示します。

cargo run --example demux -- <mp4_file>

fmp4

fMP4 の mux/demux を行うサンプルです。引数なしで実行できます。

cargo run --example fmp4

WebAssembly サンプル

WebAssembly を使ったサンプルを GitHub Pages に用意しています。

規格書

ロードマップ

ライセンス

Apache License 2.0

Copyright 2024-2026, Takeru Ohta (Original Author)
Copyright 2024-2026, Shiguredo Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.