Mux Audio and Video Into MP4

This topic describes how to use the Transcoder.run method to mux AAC audio and H.264 video into an MP4 file.

The code snippets in this article are from the mux_mp4_file Python sample.

Code

This code multiplexes AAC audio and H.264 video streams into an MP4 container file.

Initialize AVBlocks

Initialize the AVBlocks library before creating the transcoder, and shut it down after the muxing operation is complete.

Library.initialize()

# Set license information. To run AVBlocks in demo mode, comment the next line out
# Library.set_license("<license-string>")

result = mp4_mux(audio_files, video_files, output_file)

Library.shutdown()

sys.exit(0 if result else 2)

Create the MP4 Output Socket

The output socket is configured as StreamType.MP4. The sample adds one output pin for each input audio or video stream.

# Create output socket
output_socket = MediaSocket()
output_socket.file = output_file
output_socket.stream_type = StreamType.MP4

# audio
for audio_file in audio_files:
    output_pin = MediaPin()
    asi = AudioStreamInfo()
    asi.stream_type = StreamType.AAC
    output_pin.stream_info = asi

    output_socket.pins.add(output_pin)

    input_socket = MediaSocket()
    input_socket.file = audio_file
    input_socket.stream_type = StreamType.MP4
    transcoder.inputs.add(input_socket)

    print(f"Muxing audio input: {audio_file}")

# video
for video_file in video_files:
    output_pin = MediaPin()
    vsi = VideoStreamInfo()
    vsi.stream_type = StreamType.H264
    output_pin.stream_info = vsi

    output_socket.pins.add(output_pin)

    input_socket = MediaSocket()
    input_socket.file = video_file
    input_socket.stream_type = StreamType.MP4
    transcoder.inputs.add(input_socket)

    print(f"Muxing video input: {video_file}")

transcoder.outputs.add(output_socket)

Run the Muxer

Open and run the transcoder to perform the muxing operation.

# Open transcoder
if not transcoder.open():
    print_error("Open Transcoder", transcoder.error)
    return False

# Run transcoder
if not transcoder.run():
    print_error("Run Transcoder", transcoder.error)
    return False

transcoder.close()

print(f"Output file: {output_file}")

return True