Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB   

Importing Audio and Video

Getting Information about Audio or Video Files

To get information about files that contain audio data, video data, or both, use mmfileinfo. The mmfileinfo function returns the duration, format, number of audio channels, and height and width of video, as applicable.

To get more information about files that contain only video data, such as the number of frames, create a multimedia object with mmreader and use the get function. For more information, see Getting Information about Video Files.

Reading or Recording Audio

The easiest way to read audio data from a file is to use the Import Wizard, a graphical user interface. To start the Import Wizard, select File > Import Data or double-click the file name in the Current Folder browser. To import WAV files without invoking a graphical user interface, use wavread.

To record data from an audio input device (such as a microphone connected to your system) for processing in MATLAB, use the audio recorder object. Create the object with the audiorecorder function, and use methods and properties of the object to record the data.

After you import audio data, MATLAB supports several ways to listen to the data. For the most flexibility, use the audioplayer function to create an audio player object, which you can play, pause, and resume. Alternatively, use the sound or soundsc function.

Reading Video Data from a File

To import video data from a file, construct a multimedia object with mmreader and read selected frames with the read function.

For example, import all frames in the demo file xylophone.mpg:

xyloObj = mmreader('xylophone.mpg');
vidFrames = read(xyloObj);

It is not necessary to close the multimedia object.

For more information, see:

Getting Information about Video Files

mmreader creates an object that contains properties of the video file, including the duration, frame rate, format, height, and width. To view these properties, or store them in a structure, use the get function. For example, get the properties of the demo file xylophone.mpg:

xyloObj = mmreader('xylophone.mpg');
info = get(xyloObj)

The get function returns:

info = 
          Duration: 4.7020
              Name: 'xylophone.mpg'
              Path: [1x75 char]
               Tag: ''
              Type: 'mmreader'
          UserData: []
      BitsPerPixel: 24
         FrameRate: 29.9700
            Height: 240
    NumberOfFrames: 141
       VideoFormat: 'RGB24'
             Width: 320

To access a specific property of the object, such as Duration, use dot notation as follows:

duration = xyloObj.Duration;

Processing Frames of a Video File

A typical video contains many frames. To save memory, process a video one frame at a time. For faster processing, preallocate memory to store the video data.

For example, convert the demo file xylophone.mpg to a MATLAB movie, and play it with the movie function:

xyloObj = mmreader('xylophone.mpg');

nFrames = xyloObj.NumberOfFrames;
vidHeight = xyloObj.Height;
vidWidth = xyloObj.Width;

% Preallocate movie structure.
mov(1:nFrames) = ...
    struct('cdata', zeros(vidHeight, vidWidth, 3, 'uint8'),...
           'colormap', []);

% Read one frame at a time.
for k = 1 : nFrames
    mov(k).cdata = read(xyloObj, k);
end

% Play back the movie once at the video's frame rate.
movie(mov, 1, xyloObj.FrameRate);

Reading Variable Frame Rate Video

Some files store video at a variable frame rate, including many Windows Media® Video files. For these files, mmreader cannot determine the number of frames until you read the last frame.

For example, consider a hypothetical file VarFrameRate.wmv that has a variable frame rate. A call to mmreader to create the multimedia object such as

obj = mmreader('VarFrameRate.wmv')

returns the following warning and summary information:

Warning: Unable to determine the number of frames in this file.

Summary of Multimedia Reader Object for 'VarFrameRate.wmv'.

  Video Parameters:  23.98 frames per second, RGB24 1280x720.
                     Unable to determine video frames available.

Counting Frames.   To determine the number of frames in a variable frame rate file, call read with the following syntax:

lastFrame = read(obj, inf);

This command reads the last frame and sets the NumberOfFrames property of the multimedia object. Because mmreader must decode all video data to count the frames reliably, the call to read sometimes takes a long time to run.

Specifying the Frames to Read.   For any video file, you can specify the frames to read with a range of indices. Usually, if you request a frame beyond the end of the file, read returns an error.

However, if the file uses a variable frame rate, and the requested range straddles the end of the file, read returns a partial result. For example, given a file with 2825 frames associated with the multimedia object obj, a call to read frames 2800 - 3000 as follows:

images = read(obj, [2800 3000]);

returns:

Warning: The end of file was reached before the 
requested frames were read completely.  
Frames 2800 through 2825 were returned. 

Supported Video File Formats

The mmreader function reference page lists file formats that mmreader usually can read, including AVI, MPEG-1, and Motion JPEG 2000. Sometimes mmreader can read files in unlisted formats, and sometimes it cannot read files in listed formats.

For video data, the term "file format" often refers either to the container format or the codec. A container format describes the layout of the file, while a codec describes how to code/decode the data. Many container formats support multiple codecs.

To read a video file, any application must:

To see the codec associated with a video file, use mmfileinfo and view the Format field. For example, given a hypothetical AVI file that uses the Indeo® 5 codec, the following code:

info = mmfileinfo('myfile.avi');
info.Video.Format

returns

ans = 
IV50
  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS