mp4parser

repository·master·Indexed 25 days ago

https://github.com/sannies/mp4parser

A Java API for reading, writing, and creating MP4 containers, focusing on container manipulation such as muxing, metadata, and appending. It supports track formats including H264, AAC, AC3, and EC3, and provides tools like CroppedTrack for audio alignment and ESDescriptor for ISO 14496 Elementary Stream descriptors. The library is available as the isoparser artifact (version 1.9.27) and requires aspectj-rt.jar.

Tokens
1.2K
Snippets
3
Records
7
Agent score
34%

What's inside mp4parser

  1. Mux audio and video into an MP4 file

    master

    To mux raw audio and video files into an MP4 container, follow these steps:

    1. Wrap raw format files into Track objects (e.g., H264TrackImpl, AACTrackImpl).
    2. Add the Track objects to a Movie object.
    3. Use DefaultMp4Builder to build the Container from the Movie.
    4. Write the Container to a FileChannel.

    Supported track formats include H264, AAC, AC3, and EC3.

    // 1. Wrap raw format files into Track objects
    H264TrackImpl h264Track = new H264TrackImpl(new FileDataSourceImpl("video.h264"));
    AACTrackImpl aacTrack = new AACTrackImpl(new FileDataSourceImpl("audio.aac"));
    
    // 2. Add Track objects to a Movie object
    Movie movie = new Movie();
    movie.addTrack(h264Track);
    movie.addTrack(aacTrack);
    
    // 3. Feed the Movie object into an MP4Builder to create the container
    Container mp4file = new DefaultMp4Builder().build(movie);
    
    // 4. Write the container to an appropriate sink
    FileChannel fc = new FileOutputStream(new File("output.mp4")).getChannel();
    mp4file.writeContainer(fc);
    fc.close();
  2. Install the MP4 Parser library

    master

    The library is available via Maven and Gradle. Note that the project requires the aspectj-rt.jar library as a dependency.

    ### Gradle
    ```gradle
      compile 'org.mp4parser:isoparser:1.9.27'

    Maven

      <dependency>
        <groupId>org.mp4parser</groupId>
        <artifactId>isoparser</artifactId>
        <version>1.9.27</version>
      </dependency>
  3. Shorten audio tracks using CroppedTrack

    master

    If audio starts before video, you can shorten the audio track by omitting samples from the beginning using CroppedTrack.

    For AAC, each sample's duration is calculated as 1000 * 1024 / samplerate milliseconds. For example, at 48KHz, one sample is approximately 21.3ms. Removing the first sample can help align audio with video timing.

    AACTrackImpl aacTrackOriginal = new AACTrackImpl(new FileDataSourceImpl("audio.aac"));
    // removes the first sample and shortens the AAC track by ~22ms
    CroppedTrack aacTrackShort = new CroppedTrack(aacTrackOriginal, 1, aacTrack.getSamples().size());
  4. Configure ESDescriptor properties

    master

    You can configure an ESDescriptor using the following methods to define the elementary stream's characteristics:

    • setEsId(int esId): Sets the ES ID.
    • setStreamDependenceFlag(int streamDependenceFlag): Sets the flag indicating if the stream depends on another.
    • setURLFlag(int URLFlag): Sets the flag indicating if a URL is present.
    • setURLString(String URLString): Sets the URL string (used if URLFlag is 1).
    • setoCRstreamFlag(int oCRstreamFlag): Sets the flag for OCR streams.
    • setoCREsId(int oCREsId): Sets the OCR ES ID.
    • setStreamPriority(int streamPriority): Sets the stream priority.
    • setDecoderConfigDescriptor(DecoderConfigDescriptor decoderConfigDescriptor): Attaches a decoder configuration.
    • setSlConfigDescriptor(SLConfigDescriptor slConfigDescriptor): Attaches an SL configuration.