mmwrite

write AVI/ASF/WMV/WMA file from movies read by mmread
12.5K Downloads
Updated 14 May 2010

View License

function list = mmwrite(filename,...options...)

mmwrite is able to write AVI,WMV,WMA,ASF files. For AVI files you can choose from the available codecs to compress the audio and video streams.
For WMV,WMA and ASF the encoding defaults to Windows Media 9 44100Hz 16bit stereo 98% quality for the audio and Windows Media 9 Video with 98
quality. The quality can be specified for both audio and video.
Surround sound only seems to work with AVI and multi-pass encoding is not supported. Writing any other file type is not supported. This uses Windows DirectX infrastructure, so other OSs are out of luck.

INPUT:
filename: This must be the first parameter and specifies the filename to write.

video structure: The video structure matches the output of mmread. At a minimum it must have 4 fields "frames", "times", "height" and "width". The "frames" field must be a struct array with a field "cdata" that contains the raw frame data encoded as height by width by color(3) as UINT8s. The "times" field contains the time stamps of the data stored in frames. "times" and "frames.cdata" must be the same length.

audio structure: The audio structure matches the output of mmread. At a minimum it must have 3 fields "data", "rate" and "times". The "data" field is a matrix nrSamples by nrChannels (the same format was wavread/wavplay). The field "rate" is the sampling rate of the data in Hz, eg. 44100. The field "frames" is used to specify the time that audio should start, the rest of the time is extrapolated based upon the "rate" and the nrSamples.

AVI config structure:
videoCompressor Specify which video compressor/codec to use. Use 'ListAviVideoEncoders' to determine what are valid codecs on your machine.
audioCompressor Specify which audio compressor/codec to use. Use
'ListAviAudioEncoders' to determine what are valid codecs on your machine.

WMV/WMA/ASF config structure: can have any of the following fields
videoQuality the quality of the video, between 0 and 100 default 98.
audioQuality the quality of the audio, between 0 and 100 default 98.
outputHeight the height of the video to be generated.
outputWidth the width of the video to be generated.
outputFrameRate the frame rate of the video to be generated.
prxFile Specify a custom encoding file. The settings here overwide the all of the other config options. To create a custom file, use Windows Media Encoder and use the Export feature of the Compression tab.

'ListAviVideoEncoders': Use this option to list availble video encoders
Eg. list = mmwrite('','ListAviVideoEncoders');

'ListAviAudioEncoders': Use this option to list availble audio encoders
Eg. list = mmwrite('','ListAviAudioEncoders');

'Continue': Keep the encoding going so that more data can be added by a later call to mmwrite. Defaults to false. To have a usable output file, you must later call mmwrite with only the 'Initialized' option.

'Initialized': Indicates that mmwrite has already been initialized (by a call with 'Continue') and to just add the data specified. Warning, the order of audio and video structures must be the same as the first 'Continue' command otherwise the streams will get mixed.

OUTPUT
list: Only 'ListAviVideoEncoders' and 'ListAviAudioEncoders' have an output, which is the list of encoders installed on the system.

EXAMPLES:

% write a simple WMV file with audio and video
mmwrite('blah.wmv',audio,video);

% make a WMA file from the audio taken from another video
[video, audio] = mmread('your movie');
mmwrite('blah.wma',audio);

% make a video progressively
mmwrite('blah.wmv',audio,video,'Continue'); %initialize the movie
...
% the "times" fields for both audio and video must start after the last
"times" in the previous call to mmwrite.
mmwrite('blah.wmv',audio2,video2,'Continue','Initialized'); %don't initialize or stop
...
mmwrite('blah.wmv',audio3,video3,'Initialized'); %don't initialize and STOP

% make an AVI with custom compressors.
audioList = mmwrite('','ListAviAudioEncoders');
videoList = mmwrite('','ListAviVideoEncoders');
if ~any(ismember(list,'ffdshow video encoder'))
conf.videoCompressor = 'Cinepak Codec by Radius'; % default to this if ffdshow isn't installed...
else
conf.videoCompressor = 'ffdshow video encoder';
end
mmwrite('balh.avi',audio,video,conf);

% write just a subsample (the time range 10 to 20s) of a movie using mmread
[video, audio] = mmread('your file',[],[10 20]);

subtract 10 seconds off the time stamps so that the audio and video
will start at the beginning of the movie.
video.times = video.times - 10;
audio.times = audio.times - 10;

mmwrite('blah.wmv',audio,video); % make the movie...

Cite As

Micah Richert (2024). mmwrite (https://www.mathworks.com/matlabcentral/fileexchange/15881-mmwrite), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R13
Compatible with any release
Platform Compatibility
Windows macOS Linux
Acknowledgements

Inspired by: mmread

Inspired: audio, Audio Watermarking by selvakarna

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
1.1.0.0

Fixed the reduced frame rate issue mentioned by Monte Owens

1.0.0.0

The code would crash when the "times" field was missing from the struct.