MP4 (.mp4, .m4a, ...)

Background & Context

    • MIME types: video/mp4
    • MPEG-4 Part 14 video format.
    • Multimedia container format.
    • Can contain multiple video, audio and subtitle tracks.
    • Extended from QuickTime file format and MPEG-4 Part 12.
    • Follows the ISO/IEC 14496-14 standard.
    • Commonly referred to as MPEG-4 file format, version 2.
    • Commonly used for archiving and exchanging video content.
    • Stores audio and video in a format suitable for synchronous playback.
    • Binary format.
    • Supports a variety of video and audio codecs.
    • Developed by ISO in 2001.

Import & Export

  • Import["file.mp4"] imports an MP4 file and returns a Video object.
  • Import["file.mp4",elem] imports the specified elements.
  • The import format can be specified with Import["file","MP4"] or Import["file",{"MP4",elem,}].
  • Supported video decoders are listed in $VideoDecoders.
  • Export["file.mp4",expr] exports expr to the MP4 format.
  • The following expressions can be exported to an MP4:
  • videoan arbitrary Video object
    AnimatedImage[]frames of an AnimatedImage object
    Manipulate[]frames generated from a Manipulate expression
    {expr1,expr2,}a list of images or graphics or rasterized expressions
  • When exporting a list of arbitrary expressions, each expression is rasterized and has the same raster size as its displayed form.
  • The duration of the MP4 video is determined from the number of frames and the specified frame rate.
  • If bookmarks are set in the Manipulate object, Export will create an MP4 file by interpolating between the bookmarks while preserving the speed and duration of the Manipulate.
  • Export["file.mp4",{elem1expr1,elem2expr2,},"Rules"] uses rules to specify the elements to be exported.
  • Supported video encoders are listed in $VideoEncoders.
  • See the following reference pages for full general information:
  • Import, Exportimport from or export to a file
    CloudImport, CloudExportimport from or export to a cloud object
    ImportString, ExportStringimport from or export to a string
    ImportByteArray, ExportByteArrayimport from or export to a byte array

Import Elements

  • General Import elements:
  • "Elements" list of elements and options available in this file
    "Summary"summary of the file
    "Rules"list of rules for all available elements
  • Video representation elements:
  • "ImageList"frames represented as a list of images
    "Video"video file represented as a Video object
    "VideoData"array of raw bitmap data for each frame
    "VideoStream"a handle to the video file as a VideoStream object
  • Audio representation elements:
  • "Audio"in-core Audio object
    "AudioData"array of audio samples
    "AudioFile"out-of-core Audio object
  • Import by default uses the "Video" element if the file has a video track and otherwise uses the "Audio" element.
  • A video file may contain multiple video, audio or subtitle tracks. Elements that return the number of tracks:
  • "AudioTrackCount"number of audio tracks
    "SubtitleTrackCount"number of subtitle tracks
    "VideoTrackCount"number of video tracks
  • Video metadata elements:
  • "BitDepth"bits used to represent each color channel in the file
    "ColorSpace"color encoding used in the file
    "FrameCount"number of frames in the file
    "FrameDurations"list of display durations given for each frame
    "FrameRate"number of frames to be displayed per second
    "RasterSize"raster dimensions
    "VideoEncoding"video codec used in the file
  • Audio metadata elements:
  • "AudioChannels"number of audio channels
    "AudioBitDepth"bits used to represent each audio sample
    "AudioEncoding"audio codec used in the file
    "AudioLength"EscEscnumber of audio samples
    "BitRate"number of bits per second
    "SampleRate"number of audio samples per second
  • Subtitle-related elements:
  • "RawSubtitle"subtitle data imported as a raw string
    "SubtitleRules"returned as a list of styled text in the form of {int1text1,int2text2,}
    "SubtitleString"returned as a single string of concatenated subtitle texts
    "SubtitleStringRules"returned as a list of plaintext in the form of {int1string1,int2string2,}
    "SubtitleEncoding"subtitle codec used in the file
  • Additional metadata elements:
  • "ExactDurations"an association of exact durations for all video, audio and subtitle tracks
    "ExactTimeBoundaries"an association of exact start and end times for all video, audio and subtitle tracks
    "Duration"approximate duration of the video given in seconds
    "MetaInformation"metadata interpreted as quantities, locations, dates, numbers, etc.
    "RawMetaInformation"uninterpreted metadata as strings and numbers
    "Summary"summary of the file
    "TrackStartTimes"an association of start times for each track
  • $VideoDecoders and $AudioDecoders give a list of decoders available on your computer system.

Export Elements

  • Export["file.mp4",{elem1->expr1,},"Rules"] uses rules to specify the elements to be exported.
  • Available Export elements:
  • "Audio"an audio track
    "Frames"video frames provided as a list, a Manipulate or AnimatedImage object

Options

Examples

open allclose all

Basic Examples  (4)

Import an MP4 file as a Video object:

Import a video property such as duration:

Import a summary of a video file:

Import an audio-only MP4 file:

Export a Video object to an MP4:

Scope  (6)

Import  (2)

Import a list of video frames:

Import the first 10 seconds of audio data:

Export  (4)

Export a list of images to MP4:

Get three frames from the video:

Export a Manipulate to MP4:

Summary of the exported file:

Export an AnimatedImage object:

Export a Manipulate with audio:

Summary of the exported file:

Import Elements  (33)

Available Elements  (1)

List of available elements:

Video Elements  (3)

"Video"  (1)

Import as a Video object:

This is the default import element when at least one video track is available:

"VideoData"  (1)

Import video data corresponding to specified frames:

Convert video data stored in a NumericArray object to an image:

"VideoStream"  (1)

Import as a VideoStream object:

Play the video stream:

Get the current frame from the stream:

Number of Tracks  (3)

"AudioTrackCount"  (1)

Import the number of audio tracks:

"SubtitleTrackCount"  (1)

Import the number of subtitle tracks:

"VideoTrackCount"  (1)

Import the number of video tracks:

Metadata Elements  (7)

"Duration"  (2)

Import approximate duration of the video:

To import exact track durations, use the "ExactDurations" element:

"ExactDurations"  (2)

Import exact track durations:

Import approximate duration of the video:

Computing an approximate duration is typically much faster than exact durations:

"MetaInformation"  (1)

Import metadata from a video file:

"RawMetaInformation"  (1)

Import unprocessed metadata from a video file:

"Summary"  (1)

Import a summary of a video file:

Video Tracks  (7)

"BitDepth"  (1)

Import the bit depth:

"ColorSpace"  (1)

Import the color space:

"FrameCount"  (1)

Import the number of video frames:

"FrameDurations"  (1)

Import the duration of selected video frames:

"FrameRate"  (1)

Import video frame rate:

"RasterSize"  (1)

Import raster size of video frames:

"VideoEncoding"  (1)

Import video codec:

Supported decoders on each platform can be extracted using $VideoDecoders:

Audio Tracks  (7)

"Audio"  (1)

Import Audio from an MP4 file:

"AudioChannels"  (1)

Import the number of audio channels:

"AudioData"  (1)

Import audio data from an MP4 file as a NumericArray:

Convert audio data stored to an Audio object:

"AudioEncoding"  (1)

Import audio encoding:

Available audio decoders can be extracted from $AudioDecoders:

"AudioFile"  (1)

Import the out-of-core Audio object:

Note that out-of-core Audio does not hold data in memory. Compare with in-core Audio:

"BitRate"  (1)

Get the bit rate of an audio track:

"SampleRate"  (1)

Get the sample rate of an audio track:

Subtitle Tracks  (5)

"RawSubtitle"  (1)

Get raw subtitles from an MP4 file:

None is returned if subtitles are not present:

"SubtitleEncoding"  (1)

Import subtitle encoding:

Available subtitle encodings can be extracted using $SubtitleDecoders:

"SubtitleRules"  (1)

Get subtitles as a list of styled text:

None is returned if subtitles are not present:

"SubtitleString"  (1)

Get subtitles as a single string:

None is returned if subtitles are not present:

"SubtitleStringRules"  (1)

Get subtitles as a list of plaintext:

None is returned if subtitles are not present:

Import Options  (11)

Generate a Multitrack MP4  (1)

Generate a multitrack MP4 file:

"AudioChannels"  (1)

By default, all audio channels are imported:

Import the second channel only:

AudioTrackSelection  (1)

By default, Import extracts data only from the first audio track:

Use the AudioTracks option to extract data from specified audio tracks:

"ImageTopOrientation"  (1)

By default, video frames are imported with the native orientation stored in the file:

Specify the side that is supposed to be on the top:

IncludeMetaInformation  (3)

By default, all available metadata is imported:

Avoid importing any metadata:

Import only specified classes of metadata:

RasterSize  (1)

By default, video frames are imported in their native size:

Specify the raster size of each video frame:

SampleRate  (1)

By default, video frames are imported in their native size:

Specify the sample rate of the imported sound:

SubtitleTrackSelection  (1)

By default, Import extracts data only from the first subtitle track:

Use the SubtitleTrackSelection option to extract data from specified subtitle tracks:

VideoTrackSelection  (1)

By default, Import extracts data only from the first video track:

Use the VideoTrackSelection option to extract data from specified video tracks:

Export Options  (23)

"AnimationDuration"  (1)

Specify the duration of exported Manipulate expressions:

AudioEncoding  (1)

Specify the encoding of exported audio:

AudioTrackSelection  (1)

By default, all audio tracks are exported:

Specify the audio tracks to export:

Export no audio track:

Background  (1)

By default, a white background is used for transparent graphics and images and also when images of different sizes are padded before export:

Specify a different padding:

BitRate  (4)

By default, automatic bit rate is used when exporting to video files:

Check an overall approximate bit rate:

Suggest an approximate bit rate:

Check the approximate bit rate that might slightly differ from the suggested one:

Separately specify the approximate video and audio bit rates:

Check an overall approximate bit rate:

Smaller bit rate results in smaller file size but also lowers the quality of the generated video:

CompresionLevel  (2)

Use the CompressionLevel option to control the file size and the quality of generated video:

When BitRate for both video and audio is specified, the value of CompressionLevel is ignored:

"ControlAppearance"  (1)

An animation exported with the default setting "ControlAppearance"Automatic will look different depending on the computer system on which it was created:

With "ControlAppearance"None, no user controls are included in the exported video:

FrameRate  (1)

By default, the frame rate is preserved when exporting Video or VideoStream objects:

Specify a different frame rate:

IncludeMetaInformation (3)

By default, all existing metainformation from an Audio object is exported:

Specify the metadata to include:

Import metadata:

Do not export metadata such as "Language" or "Title":

MetaInformation (1)

By default, all supported metadata from the input video file is exported:

Compare with the metadata from the original file:

Export specified metadata; only selected tags are supported for "MP4":

RasterSize  (1)

By default, the raster size is preserved when exporting Video or VideoStream objects:

Specify a different raster size:

SampleRate  (1)

By default, the sample rate is preserved when exporting Video, VideoStream or Audio objects:

Specify a different sample rate:

SubtitleEncoding  (1)

Specify the encoding of exported subtitles:

SubtitleTrackSelection  (1)

By default, all subtitle tracks are exported:

Use the subtitle tracks to export:

Export no subtitle tracks:

VideoEncoding  (1)

Specify the encoding of exported video:

VideoTrackSelection  (1)

By default, all video tracks are exported:

Specify the video tracks to export:

Export no video tracks:

VideoTransparency  (1)

Generate a video with an alpha channel:

By default Export removes the transparency:

Export a video with an alpha channel: