Code covered by the BSD License  

Highlights from
mpgwrite

4.05263

4.1 | 77 ratings Rate this file 157 Downloads (last 30 days) File Size: 868 KB File ID: #309

mpgwrite

by

 

05 Apr 1999 (Updated )

The MPEG converter takes a MATLAB movie matrix and writes the movie to disk as an MPEG file.

| Watch this File

File Information
Description

MPEG is an ISO standard movie encoding format. The MPEG converter for MATLAB takes a MATLAB movie matrix and writes the movie to disk as an MPEG file with file extension '.mpg' (MPGWRITE). It also takes an MPEG file and converts it into a MATLAB movie matrix (MPGREAD).

Acknowledgements

This file inspired Movie Editor.

MATLAB release MATLAB 6.0 (R12)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (115)
27 Jan 2014 peter

Hi,
This is a great code, thank you! But what is the fps rate of the mpg video? I assumed it was 30fps, however, the videos are always quick that they are suppose to be. i.e. I put in 5 seconds of data (at 30 Hz) and the video is then 7/8 seconds long.
Thanks,
Pete

02 Nov 2013 Santosh

Did any one get this working with Matlab_R2012a on Mac OS 10.8.4? I followed the instructions provided by Axel, and it did compile correctly(after applying a patch) but then when I run the mpgwrite I get an error - "Error using mpgwrite
MPGWRITE returns nothing". Can someone please help?

09 May 2013 Danh Nguyen

This solution is very helpful.
And I need help in Win7 64bit and Matlab 2012b.

10 Jul 2012 Matija  
22 Jun 2012 Paul Christian Sanamizi

can some1 tell me how to use this file and where to copy it so that Matlab can actually read the file

11 Jun 2012 Peter O'Connor

Great function.

Problem:In Linux crashes matlab after writing the file.

Solution: As Dan Russ said (above), the way to fix this is go into the mpeg.c file in src and comment out the line: "fclose(ofp);" in GenMPEGStream

05 Apr 2012 Nathan Orloff

One more suggestion for people using this code. I ended up using FFMPEG and then running it through the command line. It is much faster than everything else I have used, and it has all the codecs for all the different formats. Email me if you want the command like calls.

15 Mar 2012 Peter T. S. DeVore

Just got it to work!

Specs:
Windows XP Professional x64 Edition
MATLAB R2007b 64-bit

Steps:
1) Install Visual C++ 2008 Express http://www.microsoft.com/visualstudio/en-us/products/2008-editions/express
- It doesn't come with the x64 bit compilers (thanks cheap-o Microsoft)

2) Install Microsoft SDK 6.1
http://www.microsoft.com/downloads/details.aspx?FamilyId=F26B1AA4-741A-433A-9BE5-FA919850BDBF&displaylang=en
- Make sure that the x64 compilers option is selected _before_ installing.

3) In "mpgwrite/src/Makefile"
change "$(MCC) $(MCFLAGS) " to "-v -DWIN32"

4) Open "C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin\Visual Studio 2008 x64 Win64 Command Prompt"

5) Navigate to the mpgwrite/src folder

6) Run nmake -f Makefile

7) Move mpgwrite.mexw64 to mpgwrite folder (out of the src folder)

8) Add mpgwrite folder to your MATLAB path

Sources:
* http://www.mathworks.com/support/solutions/en/data/1-6IJJ3L/?solution=1-6IJJ3L
* http://grantnz.blogspot.com/2010/07/compiling-matlab-mpgwrite-with-vs2010.html
* Andrea's post above

01 Mar 2012 Jens Munk Hansen

Tried your program running 64-bit linux and Matlab 2010b and Matlab2011a both with the gcc shipped with Ubuntu and with the versions supported by Matlab. They all crashes due a double free or stack corruption.

This is my sample program
figure(1)
numframes=100;
A=moviein(numframes); % create the movie matrix
set(gca,'NextPlot','replacechildren')
axis equal % fix the axes
for i=1:numframes
plot(fft(eye(i+16)));
A(:,i)=getframe;
end

for i = 1:length(A)
if (i==1)
[r1,c1,s] = size(A(i).cdata);
rk = r1;
ck = c1;
else
[rk,ck,s] = size(A(i).cdata);
end
r1 = min(r1,rk);
c1 = min(c1,ck);
end

for i = 1:length(A)
[rk,ck] = size(A(i).cdata);
if ((rk~= r1) || (ck ~=c1))
A(i).cdata = A(i).cdata(1:r1, 1:c1,:);
end
end
% compare frame sizes
for i = 1:numframes
fprintf(1,'%d %d %d\n',i,size(A(i).cdata,1),size(A(i).cdata,2));
end

mpgwrite(A,jet,'movie.mpg');

23 Feb 2012 Nathan Orloff

You should include a simple set of instructions or write an installer. It took me a minute to figure out how to run the dll to install the mex files.

One simple trick to decreasing the frame rate that has a negligible effect on increasing the file size is to duplicate the frames.

dupframes = 3;
ii = 1;
for jj = 1:(length(imagefiles)*dupframes)
[im4D(:,:,:,jj),map] = imread([imfiles]);
if mod(jj,dupframes) == 1 && jj ~=1
ii = ii + 1;
end
end

19 Jan 2012 Julia

Seems to work so far. Just wanted to say I'm running Windows and was having the same trouble as many people, but Andrea's detailed advice on installing and using Visual Studio as a compiler made all the difference.

I'm copy-pasting the key part from the website Andrea linked, even though it's long, since this was the key part for me.
http://grantnz.blogspot.com/2010/07/compiling-matlab-mpgwrite-with-vs2010.html

Compiling Matlab mpgwrite with VS2010 for 64 bit Windows
I was having trouble producing Matlab movies of reasonable quality on my 64 bit Windows 7 laptop until I was able to compile and run mpgwrite (source available from Matlab Central http://www.mathworks.com/matlabcentral/fileexchange/309-mpgwrite).

It wasn't obvious how to get this to compile and run so in case someone else would like to do this I've documented what I did:

1. Ensure Visual Studio 2010 installation includes the "X64 Compilers and Tools" (and option under the Visual C++ feature in the Visual Studio 2010 installer).

2. Configure the mex script for VS2010.
Matlab R2009b and earlier doesn't directly support Visual Studio 2010 but you can still use it by running
mex -setup
from Matlab.
a. Answer n to the first question (Would you like mex to locate installed compilers [y]/n? )
b. Select "Microsoft Visual C++ 2008 SP1 " (option 5)
c. The script will output:
The default location for Microsoft Visual C++ 2008 SP1 compilers is
C:\Program Files (x86)\Microsoft Visual Studio 9.0,
but that directory does not exist on this machine.
Use C:\Program Files (x86)\Microsoft Visual Studio 9.0 anyway [y]/n?

Answer n
d. Locate your Visual Studio 2010 installation folder and enter this in response to the next question. Visual Studio 2010 was probably installed to C:\Program Files (x86)\Microsoft Visual Studio 10.0
e. Finally, you will be asked to confirm the settings and Matlab will create a file mexopts.bat with the configuration information.

3. From Microsoft Visual Studio 2010\Visual Studio Tools open "Visual Studio x64 Win64 Command prompt (2010)".

4. Change to the src folder of the downloaded the mpgwrite source code using the "cd" command.

5. Edit the file makefile and replace $(MCC) $(MCFLAGS) after the call to mex with
-v -DWIN32
That is, your makefile should contain:

mpgread:
mex -v -DWIN32 mpgwrite.c mfwddct.c \
postdct.c huff.c bitio.c mheaders.c iframe.c \
pframe.c bframe.c psearch.c bsearch.c block.c \
mpeg.c subsampl.c jrevdct.c frame.c fsize.c

5. Run
nmake -f makefile

6. This should generate the file
mpgwrite.mexw64
Copy this file to a folder on the matlab path.

You should now be able to generate mpeg movies from matlab.

05 Jan 2012 Amir

To Dan Massie (03 Nov 2011) : the problem you noticed is due to the limitations of getframe command ( see http://www.mathworks.com/help/techdoc/ref/getframe.html ), and has nothing to do with mpgwrite.

14 Dec 2011 Yuanye Wang

Super! Just unzip and it runs!

14 Dec 2011 Yuanye Wang  
03 Nov 2011 Dan Massie

Works great on my 64-bit Windows XP Professional system. One thing I noticed: I get a black frames if my monitor's screen saver is on when the figure is created. Also, if the figure is behind another window when the figure is created, the window on top will become part of the mpg!

19 Sep 2011 Qianqian

It is very good, I can use it on Mac OS X 10.6.7 for MATLAB R2011a.

17 Sep 2011 Jason Stockmann

How exactly do I compile this on my Mac? Could someone post the exact Terminal command that needs to be typed?

19 Aug 2011 JD

This combination has problems compiling and running mpgwrite:
Win7-64bit
VS2008-64bit
Matlab R2010b-64bit

I got code working with these steps collected from this thread:
1. Download, unzip files, and run makedll.m residing in ./src directory created by zip extraction.
2. move mpgwrite.mexw64 ("the dll") to a directory that DOES NOT have mpgwrite.m in it.
3. Run this script in
directory with mpgwrite.mexw64 NOTE: The 3 programs above generate frames of different sizes and frames must be resized. Here is an example:

% adapted from http://www.math.canterbury.ac.nz/~c.scarrott/MATLAB_Movies/movies.html
figure(1)
numframes=100;
A=moviein(numframes); % create the movie matrix
set(gca,'NextPlot','replacechildren')
axis equal % fix the axes
for i=1:numframes
plot(fft(eye(i+16)));
A(:,i)=getframe;
end
%movie(A,10,3) % Play MATLAB movie
%save movie.mat A

%%% bug fix
% VS2008 and Matlab 7.11.0.584 generate 25% of frames with different
% dimensions. Assume first frame is correct.
[r1,c1,s] = size(A(1).cdata);
for i = 2:length(A)
[rk,ck] = size(A(i).cdata);
if (rk~= r1 | ck ~=c1)
A(i).cdata = A(i).cdata(1:r1, 1:c1,:);
end
end
% compare frame sizes
for i = 1:numframes
fprintf(1,'%d %d %d\n',i,size(A(i).cdata,1),size(A(i).cdata,2));
end
mpgwrite(A,jet,'movie.mpg');

4. resulting .mpg video file playable in Windows 7 Media Player at about 24 frames/sec.

MANY thanks to author for a superb tool and other commentors for their very helpful suggestions.

30 Apr 2011 Emily

I love this program, and use it regularly. I'm using matlab 7.9.0 on linux. I used it in a previous version too.

I get a segmentation violation when I forget that the matrix that contains the movie must start with frame (index) 1.

I would like to be able to control the frame rate, but at least I can make my movies slower by writing two of each frame to the movie matrix.

At one point I tried to reinstall it, and now I have two copies of the directory, mpgwrite and mpgwriteOLD. The reinstalled version always crashes after it writes the mpg file. I don't remember now what I did differently the two times I installed it.

I am happy with this program, and thank the author for sharing it!

19 Mar 2011 Karthik MSwamy

Got it working on my Win Server 2003 x64 machine. Works like a charm. Nice work.

10 Jan 2011 Pete Wright

Hi, I was wondering if anyone had written a Matlab program involving the mpgwrite function and then compiled it using the compiler toolbox. Have you had any luck, as I get an error that a *.auth file is missing . . .?

Am running on Windows 64 bit machine.

30 Dec 2010 Andrea

Thank you for the program, it works very well once you figure out how to compile it. As somebody pointed out previously, a bit more documentation about the process would be welcome.

To try and help others, I will share my experience compiling for Mac OS X 10.6 and Windows XP 64.

For Mac follow the great instructions by Axel Tidemann posted above, they do the job admirably.

For Windows XP 64 I had a bit more problems. I am using Matlab 2020b (7.11) on Windows XP 64 bit. To compile I downloaded the Express version of Visual C++ 2010 (which is FREE, by the way, can be found at http://www.microsoft.com/express/Downloads/ ).

First of all, after reading all posts, I tried with mex -v -DWIN64 and I got the same error in bitio.c as Peter D reported. Found out it was related to a line in byteord.h, #ifdef WIN32, with specify which endian set to use. Since this is WIN64 the compiler would skip that definition block and default to UNIX trying to include the file netinet/in.h which simply does no exist on Windows. I added a def block with #ifdef WIN64 and compiling went through with no problems. However, for some reason, all movies produced where just plain black. A series of black frames.

After some more looking around I noticed that somebody here said there seemed to be no difference in using DWIN32 or DWIN64, so I tried recompiling with -v -DWIN32 and again it worked (the mex file is still called mpgwrite.mexw64 though). This time, however, the program works fine too.

I also found useful information here http://grantnz.blogspot.com/2010/07/compiling-matlab-mpgwrite-with-vs2010.html to have Visual C++ 2010 up and running with Matlab.

Thank you to all previous posters for their useful comments.

30 Dec 2010 Andrea  
29 Nov 2010 Jos

Perfect! Worked first time for me.

27 Nov 2010 Luis Felipe Giraldo

Great!

21 Sep 2010 craq

I had a few difficulties installing mpgwrite. Mostly just adjusting the path and /or current directory, but the error messages weren't very helpful. (The messages themselves were from mex, not mpgwrite.)

Then something more tricky:
"libmatlb.so: cannot open shared object file: No such file or directory."
The solution to this was on
http://www.mathkb.com/Uwe/Forum.aspx/matlab/37387/Can-t-use-mpgwrite
and is just to overwrite the mpgwrite.mexglx in the mpgwrite directory with the one in mpgwrite/src

It also caused matlab to crash after producing the file. But I am happy with my 8.5MB mpg instead of a 1.3GB avi!!

matlab2010a, ubuntu10.04, 32bit

29 Aug 2010 Jason

Thanks to Axel Tidemann for the steps to get the mex compiled on mac, 64 bit, os x, 10.6.4

18 Jun 2010 Peter D

Hi all,

Having trouble installing this on Windows Server 2008 R2 (just the server version of Win7) x64. Am running Matlab x64 and wish to compile for Matlab x64.

To get Visual Studio 2008, I followed the guides from here:

http://www.mathworks.com/support/solutions/en/data/1-6IJJ3L/index.html?solution=1-6IJJ3L
http://comkat.case.edu/comkat/comkat_wiki/index.php?title=Support:Tech_notes

However, it does not compile. When I set the makedll file to have -WIN32, then I get the following error upon compilation:

Microsoft (R) Incremental Linker Version 9.00.21022.08
Copyright (C) Microsoft Corporation. All rights reserved.

C:\Users\Peter\AppData\Local\Temp\5\mex_B85E032C-8380-4648-37B1-46C637B83C7B\mpgwrite.obj C:\Users\Peter\AppData\Local\Temp\5\mex_B85E032C-8380-4648-37B1-46C637B83C7B\mfwddct.obj C:\Users\Peter\AppData\Local\Temp\5\mex_B85E032C-8380-4648-37B1-46C637B83C7B\postdct.obj C:\Users\Peter\AppData\Local\Temp\5\mex_B85E032C-8380-4648-37B1-46C637B83C7B\huff.obj C:\Users\Peter\AppData\Local\Temp\5\mex_B85E032C-8380-4648-37B1-46C637B83C7B\bitio.obj C:\Users\Peter\AppData\Local\Temp\5\mex_B85E032C-8380-4648-37B1-46C637B83C7B\mheaders.obj C:\Users\Peter\AppData\Local\Temp\5\mex_B85E032C-8380-4648-37B1-46C637B83C7B\iframe.obj C:\Users\Peter\AppData\Local\Temp\5\mex_B85E032C-8380-4648-37B1-46C637B83C7B\pframe.obj C:\Users\Peter\AppData\Local\Temp\5\mex_B85E032C-8380-4648-37B1-46C637B83C7B\bframe.obj C:\Users\Peter\AppData\Local\Temp\5\mex_B85E032C-8380-4648-37B1-46C637B83C7B\psearch.obj C:\Users\Peter\AppData\Local\Temp\5\mex_B85E032C-8380-4648-37B1-46C637B83C7B\bsearch.obj C:\Users\Peter\AppData\Local\Temp\5\mex_B85E032C-8380-4648-37B1-46C637B83C7B\block.obj C:\Users\Peter\AppData\Local\Temp\5\mex_B85E032C-8380-4648-37B1-46C637B83C7B\mpeg.obj C:\Users\Peter\AppData\Local\Temp\5\mex_B85E032C-8380-4648-37B1-46C637B83C7B\subsampl.obj C:\Users\Peter\AppData\Local\Temp\5\mex_B85E032C-8380-4648-37B1-46C637B83C7B\jrevdct.obj C:\Users\Peter\AppData\Local\Temp\5\mex_B85E032C-8380-4648-37B1-46C637B83C7B\frame.obj C:\Users\Peter\AppData\Local\Temp\5\mex_B85E032C-8380-4648-37B1-46C637B83C7B\fsize.obj
Creating library C:\Users\Peter\AppData\Local\Temp\5\mex_B85E032C-8380-4648-37B1-46C637B83C7B\templib.x and object C:\Users\Peter\AppData\Local\Temp\5\mex_B85E032C-8380-4648-37B1-46C637B83C7B\templib.exp
MSVCRT.lib(crtdll.obj) : error LNK2019: unresolved external symbol __imp_Sleep referenced in function _CRT_INIT
MSVCRT.lib(gs_report.obj) : error LNK2019: unresolved external symbol __imp_TerminateProcess referenced in function __report_gsfailure
MSVCRT.lib(gs_report.obj) : error LNK2019: unresolved external symbol __imp_GetCurrentProcess referenced in function __report_gsfailure
MSVCRT.lib(gs_report.obj) : error LNK2019: unresolved external symbol __imp_UnhandledExceptionFilter referenced in function __report_gsfailure
MSVCRT.lib(gs_report.obj) : error LNK2019: unresolved external symbol __imp_SetUnhandledExceptionFilter referenced in function __report_gsfailure
MSVCRT.lib(gs_report.obj) : error LNK2019: unresolved external symbol __imp_IsDebuggerPresent referenced in function __report_gsfailure
MSVCRT.lib(gs_report.obj) : error LNK2019: unresolved external symbol RtlVirtualUnwind referenced in function __report_gsfailure
MSVCRT.lib(gs_report.obj) : error LNK2019: unresolved external symbol RtlLookupFunctionEntry referenced in function __report_gsfailure
MSVCRT.lib(gs_report.obj) : error LNK2019: unresolved external symbol __imp_RtlCaptureContext referenced in function __report_gsfailure
MSVCRT.lib(dllmain.obj) : error LNK2019: unresolved external symbol __imp_DisableThreadLibraryCalls referenced in function DllMain
MSVCRT.lib(gs_support.obj) : error LNK2019: unresolved external symbol __imp_QueryPerformanceCounter referenced in function __security_init_cookie
MSVCRT.lib(gs_support.obj) : error LNK2019: unresolved external symbol __imp_GetTickCount referenced in function __security_init_cookie
MSVCRT.lib(gs_support.obj) : error LNK2019: unresolved external symbol __imp_GetCurrentThreadId referenced in function __security_init_cookie
MSVCRT.lib(gs_support.obj) : error LNK2019: unresolved external symbol __imp_GetCurrentProcessId referenced in function __security_init_cookie
MSVCRT.lib(gs_support.obj) : error LNK2019: unresolved external symbol __imp_GetSystemTimeAsFileTime referenced in function __security_init_cookie
mpgwrite.mexw64 : fatal error LNK1120: 15 unresolved externals

C:\MATLAB\R2007B\BIN\MEX.PL: Error: Link of 'mpgwrite.mexw64' failed.

??? Error using ==> mex at 208
Unable to complete successfully.

Error in ==> makedll at 3
mex -DWIN32 mpgwrite.c mfwddct.c postdct.c huff.c bitio.c mheaders.c iframe.c ...

When I change it to -WIN64, I get the following error:

bitio.c
c:\users\peter\desktop\mpgwritex64\src\byteord.h(74) : fatal error C1083: Cannot open include file: 'netinet/in.h': No such file or directory

C:\MATLAB\R2007B\BIN\MEX.PL: Error: Compile of 'bitio.c' failed.

??? Error using ==> mex at 208
Unable to complete successfully.

Error in ==> makedll at 3
mex -DWIN64 mpgwrite.c mfwddct.c postdct.c huff.c bitio.c mheaders.c iframe.c ...

After some Googling of netinet/in.h, I found out that this is a Linux specific header library! Thus, I am surprised that this code works in Windows AT ALL. I am very confused as to why some previous people got this to work on Windows 64 (e.g. Cassandra and Anas) and yet I cannot. Any help would be sorely appreciated; I have already sunk about 5 hours into figuring this out...

26 May 2010 Dan

I had no problem with this. I'm using v.7 on XP. I just unzipped the .m file and the .dll file (don't think you even need the .m file), and it ran beautifully. I used the directions at http://www.math.canterbury.ac.nz/~c.scarrott/MATLAB_Movies/movies.html. Thank you for sharing your work!

12 May 2010 Chris Kraus

Had the same problem on my 64 bit linux. It matlab crashed when it closed the file. How I fixed it:
- in the file src/mpeg.h:
--- orig/src/mpeg.h 1999-02-15 22:39:16.000000000 +0100
+++ src/mpeg.h 2010-05-12 14:33:42.325128732 +0200
@@ -62,7 +62,7 @@

void SetFramePattern _ANSI_ARGS_((char *pattern));
int32 GenMPEGStream _ANSI_ARGS_((int whichGOP, int frameStart, int frameEnd,
- int numFrames, FILE *ofp,
+ int numFrames, FILE **ofp,
char *outputFileName));
extern void PrintStartStats _ANSI_ARGS_((int firstFrame, int lastFrame));
extern void IncrementTCTime _ANSI_ARGS_((void));

- in src/mpeg.c:
--- orig/src/mpeg.c 1999-02-15 22:39:16.000000000 +0100
+++ src/mpeg.c 2010-05-12 14:34:05.242486919 +0200
@@ -246,7 +246,7 @@
int frameStart;
int frameEnd;
int numFrames;
- FILE *ofp;
+ FILE **ofp;
char *outputFileName;
{
BitBucket *bb;
@@ -344,7 +344,7 @@
*/

if ( frameStart == -1 ) {
- bb = Bitio_New(ofp);
+ bb = Bitio_New(*ofp);
} else {
bb = NULL;
}
@@ -430,8 +430,10 @@

if ( frameStart == -1 ) {
Bitio_Flush(bb);
- fclose(ofp);
-
+ if ( fclose(*ofp) == NULL ) {
+ *ofp = NULL;
+ }
+
time(&timeEnd);
diffTime = (int32)(timeEnd-timeStart);

- and finally in src/mpgwrite.c:
--- orig/src/mpgwrite.c 2009-12-29 17:27:30.000000000 +0100
+++ src/mpgwrite.c 2010-05-12 14:34:10.445128932 +0200
@@ -475,14 +475,14 @@
}

totalTime = GenMPEGStream(whichGOP, frameStart, frameEnd,
- numInputFiles, ofp,
+ numInputFiles, &ofp,
outputFileName);

/* deallocate frame space */
Frame_Exit();

if (ofp != NULL) {
- fclose(ofp);
+ fclose(ofp);
}
}

27 Apr 2010 Wai Lee

Maybe a problem related to Matthew Toohey posted on 02 Mar 2010:

I ran mpgwrite on a 64 bit linux. I got the mpeg file which is pretty good, but the problem is that my Matlab (2010a) crashed immediately afterwards. Does anyone else using 64 bit linux encounter similar problems?

13 Apr 2010 Andrew Fung

Andrea B. -

The mpgwrite.m file contains only help info. Your MATLAB is finding the .m file before it finds the mpgwrite mex file. You need to change your working directory or add the mex file directory to the MATLAB path before the directory which contains the mpgwrite.m file.

Adam H. -

Like the error message says, something is changing the size of your frames while they are being created. Use size() to check the dimensions of the frames in your A matrix.

29 Mar 2010 Andrea Bertoldo

Hi there!
I have MATLAB 2009b, Win7 x64 and Visual Studio 2008 as a compiler.
I can compile the source, even without -v, but...when i try to call the function, i am prompted the following message:

??? Attempt to execute SCRIPT mpgwrite as a function:
*folder*

The hell? When i type help mpgwrite i get the correct informations, just can't use it! Any ideas?

05 Mar 2010 Adam Hartshorne

Using Windows 7, Matlab 7.9.0 and Visual Studio 2008, manage to get compiled using -v hack, although end of compile get strange error,

??? Attempt to reference field of non-structure array.

but file gets made.

Try test run as follows,

fig1 = figure(1) ;
movegui(fig1) ;
winsize = get(fig1,'Position');
numframes=16;
A=moviein(numframes,fig1,winsize);
set(gca,'NextPlot','replacechildren')
axis equal % fix the axes
for i=1:numframes
plot(fft(eye(i+16)));
A(:,i)=getframe;
end

mpgwrite(A,jet,'movie.mpg');

and I get following error,

??? Error using ==> mpgwrite
All movie frames must have the same size

Any ideas?

02 Mar 2010 Matthew Toohey

On a x86_64-suse-linux box...

was finally able to compile (Axel Tidemann's comment above helped the most), and the function works, although Matlab crashes as the function completes. I find the quality of the video to be quite poor.

This would be an excellent function with 1. some detailed installation instructions, 2. control of the compression/quality and 3. control of the fps.

18 Feb 2010 Anas A.

@Cassandra (22nd Jan 2010): Yes indeed. Had the same problem - wont compile on Win7 64bit MatlabR2009b Visual Studio Compiler 64 bit.
Simply added -v so it looks like this:
mex -v -DWIN32 (the rest as indicated) and it compiled :)!!!
the exclamation marks are because -v is simply meant to dispaly the compilation process and hence it shouldnt really be the problem solver!!

Good luck for all the 64 bit guys (P.S. seems no need to change -DWIN32 to -DWIN64)

22 Jan 2010 Cassandra

Thank you for creating this fantastic tool, David!

I also had trouble compiling at first. I am running Windows 7, 64 bit, R2009b. The included .dll files did not work for me.

To compile, I first made sure my 64 bit C++ compiler was operational. To do this with Visual Studio and SDK, I recommend the following website:
http://jenshuebel.wordpress.com/2009/02/12/visual-c-2008-express-edition-and-64-bit-targets/

Second, I compiled the files in the src folder in matlab, modifying the provided command to be mex -v (etc)

Thank you again, David, and good luck to everyone else.

30 Nov 2009 Dean

Anyone that wishes to help, I am having trouble compiling (via makedll.m) the source code. I am attempting to do this on a Windows Server 2003 R2 64-bit computer running Matlab 2009a 64-bit and using the Visual Studio 2008 Express and Microsoft Platform SDK R2 as the compiler. Can the source code as written be used to create a 64-bit mex-file version of mpgwrite? If not, are there any mpeg creating functions that can run on 64-bit windows?

Hope someone can help me! Thanks so much in advance!

20 Nov 2009 Josh Inya

Dear Mercer,

Im a new user too and wanted to write out how to use this mpg add on. Im using Matlab 7.7 2008b. in Windows XP.
1. download the .zip file with all the .dll and the .m file and the readme and what not
2. extract this to the same directory as where your m-files are saved to. for me this is: C:/ ... my documents/matlab
I just dropped all the files in there.
3. I opened the .m file from matlab. File->open-> mpgwrite.m
4. in the command window I just Ran: >>mpgwrite(F,jet,'movie.mpg');
Where, F is my matrix movie file created already, you should have this already if you see a movie playing. jet is just this Red green blue setting thing that works well, and movie.mpg is the file name of the output movie that is saved to: C:/program files/MATLAB/r2008b/movie.mpg

Hope this helps!

22 Jul 2009 Babita Ubhoo

where can i get .avi files.
I have been looking but all those .avi files are not working.

22 Jul 2009 Mercer

Hello,

I am new with programming and Matlab. I have created a movie in matlab (using the movie command), and it runs fine, but I'd like to put it into mpeg format. I've downloaded the mpgwrite program, but have no idea how to compile it so that I can run it and convert my movie. All help is appreciated, thanks.

23 Jun 2009 Carrie Weidner

this would be my goto function for making movies in matlab if the quality were better, plus i would like to be able to control the framerate

27 May 2009 Tristan Desrues

My bad, I think it crashed because i left an empty frame

24 Mar 2009 DanF211  
26 Feb 2009 ill will

Is there anyway to increase the speed of the movie playback (i.e. change the fps)?

06 Feb 2009 Keith

Add MPG directory to myToolBox. Add to path with subdirectory. Windows only needs the dll. The sample file plox needs a fixed axis in addition to equal to keep images at same dimensions. Working fine with 2008b with these minor tweaks.

10 Nov 2008 Mary Manning

I haven't been able to compile it for R2008a on a linux machine -- I can't use it right out of the box because I get a library linking error, and when I compile it myself and run mpgwrite in MATLAB I get a seg fault with the following note, " *** glibc detected *** /usr/local/matlab-R2008a/bin/glnx86/MATLAB: double free or corruption (!prev): "
Anybody get this to work with R2008a on a linux?

06 Aug 2008 Mohammad Hafezi

I haven't been able to compile it on my MAC OS X Leopard. I have followed instructions by Axel for MAC user but I get this error:

mex: link of 'mpgwrite.mexmaci' failed.

make: *** [mpgwrite] Error 1

Could somebody send me a complied version? Thanks.

09 Jul 2008 hyperalimentosis hyperalimentosis

http://www.skope.ox.ac.uk/PublicationAbstract.asp?PublicationID=156 hyperalimentosis illuminating orogenesis
http://www.cvs.saude.sp.gov.br/com_ler.asp?gt_codigo=&cm_codigo=911 convenient eulogistic menarche
http://www.skope.ox.ac.uk/PublicationAbstract.asp?PublicationID=155 exclusion archaeopteryx malamide

07 Jul 2008 pcwzdlra pcwzdlra

[URL=http://mesiscti.com]haxxclvn[/URL] <a href="http://ovuvfutk.com">lfbwgfcy</a> axmytihl http://wthqmkjb.com ccibvykk cmnewwia

28 Jun 2008 Dan Barker

Please disregard my last comment, there is no need to compile it. Just add the source code to the MATLAB path then you can use mpgwrite() as you wish :)

28 Jun 2008 Dan Barker

I am experiencing the same issue as Eck. Has anybody managed to overcome this?

FYI I am using MATLAB version 7.1.0246 on Windows Vista Business (32bit)

29 Feb 2008 Peter Frazier

I had to make one edit to pbmplus.h, removing the inclusion of malloc.h, in order to get it to compile on Mac OS, Leopard, but other than that it installed very easily, and it works great.

20 Jan 2008 Eck Lesi

the "bitio.c" file in mpgwrite doesn't seem to work properly. If I run makedll.m in MATLAB, I get the following error message:

>> makedll

Error bitio.c: 112 syntax error; found `IOtime' expecting `;'
Warning bitio.c: 308 Statement has no effect
Error bitio.c: 308 syntax error; found `tempTimeStart' expecting `;'
Error bitio.c: 308 undeclared identifier `tempTimeStart'
Error bitio.c: 308 undeclared identifier `tempTimeEnd'
Warning bitio.c: 308 Statement has no effect
Warning bitio.c: 506 Statement has no effect
Error bitio.c: 506 syntax error; found `tempTimeStart' expecting `;'
Error bitio.c: 506 undeclared identifier `tempTimeStart'
Error bitio.c: 506 undeclared identifier `tempTimeEnd'
Warning bitio.c: 506 Statement has no effect
7 errors, 4 warnings

C:\MATLAB7\BIN\WIN32\MEX.PL: Error: Compile of 'bitio.c' failed.

??? Error using ==> mex
Unable to complete successfully

Error in ==> makedll at 3
mex -DWIN32 mpgwrite.c mfwddct.c postdct.c huff.c bitio.c mheaders.c iframe.c ...

is there a way to repair it? Thanks!

04 Jan 2008 JK LEE

doesn't work on the Windows Vista64 bit platform? any idea?

17 Dec 2007 Vihang Patil

Can you tell me how I could change the FPS (frame per second) value? And yes a very nice function indeed

12 Dec 2007 Alison Chaiken

Works great. Installation instructions are clear. Thanks!

21 Nov 2007 Mimi Szeto

nevermind, i read the previous reviews.
works fine!

19 Nov 2007 Mimi Szeto

I'm very confused.
The mpgwrite.m file is just script. How do I "install" what I unzipped?

17 Oct 2007 Axel Tidemann

(This is posted again, because of the Mathworks formatting stripped away the number sign in my previous post.)

The file does as it says, but it would be nice to be able to control the fps. Even though this isn't supported in MPEG movies (supposedly its always 30fps), I guess it would be trivial to support this feature, i.e. multiply the same frame for 30/desiredFPS times to achieve the desired framerate. However, I'm no C programmer and don't want to spend time doing it. A few hints on how to compile would be nice. I see others have had the same problem, so I'll share how I managed to compile it under Mac OS X 10.4.8 running on an Intel Mac Pro. 1) First, you must change file pbmplus.h, at line 101 change to

include <malloc/malloc.h>

Remember to put the number sign (i.e. the hash key on your phone) before the include statement.

2) Locate where you mex compiler is. In my case it is /Applications/MATLAB73/bin/mex. The full path must be written in the Makefile, i.e. it says simply "mex" on line 8, however it must be the full path to the mex compiler (as written above). 3) Type make at the command line (not in MatLab) - of course you must be in the src/ directory to do this. 4) Enjoy your .mex*-file! Hope this helps.

02 Oct 2007 Corinne B.

i try to run it, but it does not work.. i changed the mex to the directory where my mexfile is, but when i run it i get the error:Invalid MEX-file '/opt/matlab/extern/src/mpgwrite/mpgwrite.mexglx': libmatlb.so: cannot open shared object file: No such file or directory.

What am I doing wrong?

25 Sep 2007 George W

rock

05 Sep 2007 Jessica Tan

It is good. However, seems it puts the whole movie matrix to mpg file and cannot do it frame by frame.

31 Jul 2007 Ala Trusina  
11 Jul 2007 Lance Locey

I cannot run this on a Windows XP64 bit platform. When I try to compile the source code, based on makedll.m, the .mexw64 file that I get only creates the first dozen frames or so and then the mpeg file hangs. If I change the makedll.m file from "mex -DWIN32 mpgwrite.c ... " to
"mex -DWIN64 mpgwrite.c ... "
I get an unresolved symbol htonl referenced in function Bitio_Flush

Any ideas?

11 Jul 2007 vivek dudhabale  
28 Jun 2007 Francisco Gordillo  
17 Jun 2007 Geoff Mercer

Worked 'straight out of the box' for 2006a
under XP. no compilation just copy the files to where MATLAB can see them. Great program! A bit slow on really big jobs at high resolution. very tunable parameters and adequate help/doc.

Great stuff! thanks!

13 Jun 2007 manoj v

Works fine in R2007a. Just don't recompile. Add the dll to your Matlab search path and use as described in the documentation

22 May 2007 Ryan Molecke

Can anyone tell me how to compile this under windows xp? Do I need a special "lcc for windows" comiler or something? I downloaded and placed all files under the matlab path, but it seems to want it under your working dir to even find mpgwrite.m, and then says mpgwrite() is an undefined funct...

17 May 2007 Scott B.

Great work, thanks.

10 May 2007 David Young

I couldn't get this to work with 2007a under Windows XP. With my mex setup switched to the Lcc compiler, the link failed because the function write() couldn't be found. When I switched to a Microsoft compiler, the mex file was generated without errors. However, when I call mpgwrite, I get a segmentation violation error. I can give details if anyone can try to fix this. A shame, as it sounds useful.

14 Mar 2007 Richard van Rijn

Works fine with Matlab R14SP3. I go with all the requests for framerate control though.

@ Kody Law
In the Makefile you have to change the word mex on the into were the Matlab mex is on your system. Then it works (at least it did for me). My makefile now has
/sware/com/matlab_r14sp3/bin/mex instead of mex.

25 Feb 2007 Kody Law

I am running version 7.1.0.183 (R14) Service Pack 3. Perhaps this is part of the problem...

25 Feb 2007 Kody Law

Hi everyone,

This is quite an intriguing program and would be an excellent addition in a sort of somewhat user freindly format. Unfortunately for me, I fear I will never be able to enjoy as you all, lest I can get to the bottom of these errors... First, I get :

/tmp/ccTKCGGm.s:149: Internal error, aborting at ../../gas/config/tc-i386.c line 3500 in output_imm
Please report this bug.

mex: compile of 'mpgwrite.c' failed.

Then, I uncomment the lines

MCC = CC=gcc
MCFLAGS = CFLAGS=-ansi

And now I get merely :

make: *** [mpgread] Error 1
mex: compile of 'mpgwrite.c' failed.

Any advice on this would truly make my day.
Many thanks in advance for any input !

08 Jan 2007 Zhijun Wang

Very Good

26 Oct 2006 Axel Tidemann

The file does as it says, but it would be nice to be able to control the fps. Even though this isn't supported in MPEG movies (supposedly its always 30fps), I guess it would be trivial to support this feature, i.e. multiply the same frame for 30/desiredFPS times to achieve the desired framerate. However, I'm no C programmer and don't want to spend time doing it.

A few hints on how to compile would be nice. I see others have had the same problem, so I'll share how I managed to compile it under Mac OS X 10.4.8 running on an Intel Mac Pro.

1) First, you must change file pbmplus.h, at line 101 change <malloc.h> to <malloc/malloc.h>

2) Locate where you mex compiler is. In my case it is /Applications/MATLAB73/bin/mex. The full path must be written in the Makefile, i.e. it says simply "mex" on line 8, however it must be the full path to the mex compiler (as written above).

3) Type make at the command line (not in MatLab) - of course you must be in the src/ directory to do this.

4) Enjoy your .mex*-file!

Hope this helps.

18 Oct 2006 João Eyder Queiroz de Figueirêdo  
28 Sep 2006 Dragana Veljkovic

I had problems with movie2avi function, movies wouldn't look right from media player and wouldn't play in power point presentations. The mpeg movies solved my problem. More detailed manual on configuring the encoder would be nice, I would prefer a higher quality videos. Thanks!

22 Sep 2006 viet la

Excellent. I have to compile a stand-alone application but the Matlab's function movie2avi can not be complied. Now I am very happy because the function mpgwrite has been compiled and run well. Thank the author for his function. However, could anyone modify the function to control the frame rate.

12 Sep 2006 Skip Albertson

It would be excellent if you could modify mpgwrite and give us control over the number of frames per second (fps) as with movie2avi; nice job!

08 Sep 2006 Richard Garner

Excellent. Worked first time with no problems and little effort. I had run into problems when using Matlab's built-in movie2avi. The resulting AVI would not run on many computers that was not the original computer on which the AVI had been created. Did not have this problem with the MPG file created using this routine.

06 Sep 2006 Gianluca Antonelli

really excellent. someone did modify the frame rate? if so please send me an e-mail.

24 Aug 2006 hector rodriguez

Good

11 Aug 2006 Ryan Clary

cannot compile on mac osx; output of make command is:
mex mpgwrite.c mfwddct.c \
postdct.c huff.c bitio.c mheaders.c iframe.c \
pframe.c bframe.c psearch.c bsearch.c block.c \
mpeg.c subsampl.c jrevdct.c frame.c fsize.c
This is pdfeTeX, Version 3.141592-1.20a-2.2 (Web2C 7.5.3)
kpathsea: Running mktexfmt mex.fmt
fmtutil: no info for format `mex'.
I can't find the format file `mex.fmt'!
make: *** [mpgread] Error 1

Can anyone help?

11 Jul 2006 Mike McDonald

Thanks Benjamin for the 'addpath' advice. With that bit of help, this file worked just as I hoped in MATLAB 7, R14 on a Windows PC. Compression is greater than 100x compared to the movie2avi built-in function in MATLAB.

05 Jul 2006 m amar nath

amar

26 Apr 2006 olivier PROT

Work well on matlab6.5 linux. Easy to compile the mex file. Thanks a lot !

25 Mar 2006 Benjamin Sanderse

forget about the readme (windows users). just put the files in a folder (e.g. c:/matlab/mpeg) and include the folder in the path of matlab by using addpath c:/matlab/mpeg.

05 Jan 2006 Mark Ebden

Following the readme file, I could not get this to work on Matlab 7 with Windows XP.

"Error bitio.c: 112" etc.

07 Dec 2005 Anas Orwani

I was able to make the DLL for the mpgread, but not for mpgwrite.
When I run makedll, it is giving this error:
Any Help?

Error bitio.c: 112 syntax error; found `IOtime' expecting `;'
Warning bitio.c: 308 Statement has no effect
Error bitio.c: 308 syntax error; found `tempTimeStart' expecting `;'
Error bitio.c: 308 undeclared identifier `tempTimeStart'
Error bitio.c: 308 undeclared identifier `tempTimeEnd'
Warning bitio.c: 308 Statement has no effect
Warning bitio.c: 506 Statement has no effect
Error bitio.c: 506 syntax error; found `tempTimeStart' expecting `;'
Error bitio.c: 506 undeclared identifier `tempTimeStart'
Error bitio.c: 506 undeclared identifier `tempTimeEnd'
Warning bitio.c: 506 Statement has no effect
7 errors, 4 warnings

C:\MATLAB7\BIN\WIN32\MEX.PL: Error: Compile of 'bitio.c' failed.

??? Error using ==> mex
Unable to complete successfully

Error in ==> makedll at 3
mex -DWIN32 mpgwrite.c mfwddct.c postdct.c huff.c bitio.c mheaders.c iframe.c ...

02 Dec 2005 kumar pbv

i am using mpgread().i was downloaded that tools .but i am doubted abt where i want to place.

18 Oct 2005 Anindya Sarkar

Hi
I am using mpgwrite to write some frames into a mpg file..say I use aviread command to read a avi file and say I get a movie with 500 frames.. now when I write the 500 frames into a mpg file using mpgwrite, hou can I ensure that the mpgfile will have 500 frames?..I first worked with all the defaults in mpgwrite and I got 496 frame from 500. then to better the picture quality, I changed the Q scale values and also the search range values.. this time I got 430 frames.. The number of frames that I am quoting has been got after reading off the frames from mpg file using mpgread command.. do tell me a method by which I can maintain the same number of frames (obtained using mpgread) even after using the mpgwrite commnad.. basically I am trying to do the avi to mpg conversion without losing out on number of frames in both the original avi AND RECONSTRRUCTED (MPG) FILE.. thanks a lot

28 Sep 2005 Anne Staples

It's great--thanks!

22 Jun 2005 Adam M

I have a problem with compiling dll

Matlab 6.1.0.450 - R12.1

Borland C++ 5.5 for Win32 Copyright (c) 1993, 2000 Borland
mpgwrite.c:
Error E2219 pbmplus.h 88: Wrong number of arguments in call of macro 'atoi'
Error E2092 pbmplus.h 88: Storage class 'extern' is not allowed here
*** 2 errors in Compile ***

C:\MATLAB\BIN\WIN32\MEX.PL: Error: Compile of 'mpgwrite.c' failed.

30 May 2005 Egemen Kolemen

Perfect!!!

Thanks a lot.

03 May 2005 Dan Russ

We were having Segmentation violations when we ran it (FC4, glibc 2.3.5, OS not officially support by matlab but still runs well). Glibc reports a "double free/corrupted memory". I believe this is cause by closing the mpeg FILE twice. Once in GenMPEGStream and Once in run_encoder. I commented out the fclose() in GenMPEGStream and the Segmentation Violation went away. This may be a new "feature" in glibc/gcc.

17 Jan 2005 Random Guy

Had the same problem as other with bitio.c. It compiles after adding the line #include "time.h" as suggested by Crai Aarseth. Now it works well. Thanks Crai.

It would be nice to be able to change the coding pattern (IBBPBBP...). Editing mpgwrite.c and re-compiling works but is not convenient. Also, it would be nice to have something similar to FORCE_ENCODE_LAST_FRAME, as used by the Berkeley MPEG-1 encoder.

05 Nov 2004 Edward Grace

Excellent work! Is there a way to modify the frame rate and indicate it should be looped? That would be very handy.

21 Oct 2004 Justin Lacombe

It works like a dream. I have yet to figure out how to change the frame rate but I will look into the code for that.

14 Sep 2004 Niels Kjaergaard

A really nice alternative to movie2avi!!!

26 Jul 2004 Jim Price

Worked great, first time, with a huge compression compared with avi (x 1/100) and very good image quality. The documentation seems complex compared to what I did; mpgwrite(M, colormap, 'moviename.mpg'), i.e., I used no options and got fine results. Thanks!!!

12 Mar 2004 Ross McKerracher

Using Redhat Linux 8
Resulting mpg file is ~100x smaller than avifile that movie2avi makes.

However, matlab crashes soon after I've run it once in a matlab session.

Have tried recompiling - still get the same problem.

Has anyone else worked out how to fix this problem?

09 Feb 2004 susanne menden

Haven't been able to use this.
A line or two on how to get it to work would be much appreciated.
Unfortunately, matlab just crashes so I can't be specific about what is wrong.

28 Jan 2004 Crai Aarseth

I had the same problem as vinaya gohokar. I found that adding #include <time.h> to the other includes in bitio.c allowed me to compile.

06 Jan 2004 Bob Bob

Crashed on R13 (segmentation violation).

19 Dec 2003 vinaya gohokar

I have a problem. i can´t execute the makeddll because bitio.c has errors. Please, i need a solution.

12 Nov 2003 Håvard Berland

Crashed on R13 (segmentation violation).

06 Nov 2003 Javier Pedrera

I have a problem. i can´t execute the makeddll because bitio.c has errors. Please, i need a solution.

19 Sep 2003 ©P §Ó©¾  
10 Sep 2003 Jan Horacek

Fortunatelly no mex needed since the mpgwrite.m worked itself on Matlab 6.5, linux.
Compared to the only available movie2avi.m the mpeg
is 100x smaller file which is great!

28 Mar 2003 David Adams

Worked great for me with little effort. Windows XP, MATLAB 5.3, VC++ compliler

19 Mar 2003 Richard Samms

Ugh! This didn't work, but I suspect it's my combination of MatLab 5.3 over VC++ 6.0 that's the problem... Wish I knew more about mexfiles. Especially debugging them... I think I've something to practice on.

13 Jan 2003 Matt Wilson

It compiles into a stand-alone application and runs fine (Windows 98, R12, Borland compiler). I didn't manage to get movie2avi to run in stand-alone mode, so am using this instead. The only minor thing is that the output video seems to flicker just at the start, so it is best suited to animations more than a few frames long. Thanks.

20 Feb 2002

doesn't work with matlab 6.0 / linux

matlab segfaults some time after the
mpg is written- this looks like a mex
error rather than a mpgwrite error

Updates
20 May 2002

Update MPGWRITE for MATLAB 6.0. Changed a call to mxGetPr to mxGetData. Recompiled binaries for platforms that are supported for MATLAB 6.0. This will allow the MEX-file to run in IBMRS now.

23 May 2002

Include the rest of the original source with the archive, in addition to the update.

24 Jul 2009

Updated description and copyright

07 Jan 2010

Updated license/copyright information.

Contact us