File Exchange

image thumbnail

FFmpeg Toolbox

version 2.2 (188 KB) by

A set of wrapper functions to run FFmpeg directly from Matlab

33 Downloads

Updated

View License

FFmpeg (http://ffmpeg.org/) is a complete, cross-platform solution to record, convert and stream audio and video. FFmpeg Toolbox is aimed to bring FFmpeg features to Matlab. While FFmpeg supports a vast number of multimedia formats and codecs, FFmpeg Toolbox brings a selected few (MP3, AAC, mpeg4, x264, animated GIF) closer to the user with its output option parameters.
INSTALLATION
To use this toolbox, FFmpeg binaries must already be downloaded and made available on the PC. (E.g., in Windows, ffmpeg.exe must be available). In MATLAB, run the toolbox setup m-file (ffmpegsetup.m) and follow its instructions. Note that if a newer version of FFmpeg is placed elsewhere on the PC, you must rerun ffmpegsetup to specify which ffmpeg executable is used with the toolbox.
Example 1. Convert an AVI movie file to MP4 with AAC audio codec and x264 video codec:
ffmpegtranscode('input.avi', 'output.mp4', 'AudioCodec', 'aac', 'VideoCodec', 'x264');

Example 2. To create animated GIF of a running sinusoid:

t = linspace(0,1,1001);
phi = linspace(0,2*pi,21);
figure;
for n = 1:numel(phi)
plot(t,sin(2*pi*t+phi(n)))
print('-dpng',sprintf('test%02d.png',n)); % create an intermediate PNG file
end
ffmpegimages2video('test%02d.png','sinedemo.gif','InputFrameRate',5,...
'VideoCodec','gif','DeleteSource','on');

Example 3: Overlay a transparent mask to a video

videofile = 'video.mp4';
maskfile = 'mask.png'; % same size as video.mp4 frame
filtgraph = [ffmpegfilter.head ffmpegfilter.overlay ffmpegfilter.tail];
filtgraph(1).link(filtgraph(2),'0:v'); % video.mp4 as the main
filtgraph(1).link(filtgraph(2),'1:v',true); % mask.png as overlayed
filtgraph(2).link(filtgraph(3));
ffmpegcombine({'videofile.mp4' 'maskfile'},'output.mp4',filtgraph);

TOOLBOX FUNCTION AND CLASS LIST:

Toolbox Setup
ffmpegsetup - Run this first to use this toolbox

FFmpeg feature list functions
ffmpegcodecs - Gets supported video codecs
ffmpegcolor - Convert color expression from MATLAB to FFmpeg
ffmpegcolors - Gets FFmpeg color names and their RGB values
ffmpegformats - Gets multimedia file formats
ffmpegpixfmts - Gets supported video pixel formats

FFmpeg wrapper functions
ffmpegextract - Extract a stream from a media file
ffmpegimage2video - Create video file from a series of images
ffmpeginfo - Retrieves media file information
ffmpegtranscode - Transcode media file (supports croping & scaling)
ffmpegcombine - Marge multiple media files with a filtergraph

FFmpeg filtergraph generator functions
ffmpegfiltersvideotform - To apply a series of spatial transformations
ffmpegfilterspalette - To generate and apply 256-color palette

FFmpeg filters (ffmpegfilter package)
ffmpegfilter.crop - Crop video
ffmpegfilter.hflip - Flip video horizontally
ffmpegfilter.histeq - Apply global color histogram equalization
ffmpegfilter.null - Pass through
ffmpegfilter.overlay - Overlay a video on top of another
ffmpegfilter.pad - Pad video
ffmpegfilter.palettegen - Generate a 256-color palette for a video
ffmpegfilter.paletteuse - Use a palette to reduce colors in video
ffmpegfilter.rotate - Rotate video
ffmpegfilter.scale - Scale or resize video
ffmpegfilter.setdar - Change display-aspect-ratio (DAR) setting
ffmpegfilter.setsar - Change sample-aspect-ratio (SAR) setting
ffmpegfilter.split - Split into several identical outputs
ffmpegfilter.transpose - Transpose rows of video with columns
ffmpegfilter.vflip - Flip video vertically

ffmpegfilter.head - Start of filtergraph (one per filtergraph)
ffmpegfilter.tail - End of filtergraph (one per filtergraph)
ffmpegfilter.base - Base class for all ffmpegfilter classes

Low-level FFmpeg wrapper functions
ffmpegexecargs - Run FFmpeg with custom option structs
ffmpegexec - Run FFmpeg with custom argument string

Comments and Ratings (9)

Is there a way to add functionality for conversion of .mov files to .ogg or .ogv files?

Kesh Ikuma

Kesh Ikuma (view profile)

@Ganesh - Just updated the submission with the missing files. Lemme know if it fixed it.

When I run this code,

ffmpegtranscode('rhinos.avi', 'output.mp4', 'AudioCodec', 'aac', 'VideoCodec', 'x264');

I am getting the error like this.

Undefined function 'isfullpath' for input arguments of type 'char'.

Please guide me.

Mohamed ata

I cant find exe file in the extracted file so where is the exe file

Kesh Ikuma

Kesh Ikuma (view profile)

@Dinesh - The PixelType bug has been fixed in the latest version; thanks for reporting it.

The AVI file header issue is beyond my control, and you ought to report it to FFmpeg dev team (and use their most recent release with this toolbox). To find out the exact ffmpeg command line called by a ffmpeg toolbox function, place a breakpoint on Line 39 ('system' call) of ffmpegexec.m and check the string value of the 'cmd' variable.

Dinesh Iyer

When I attempt to extract video stream in file to raw AVI, I notice that the biCompression field in the stream format header is not BI_UNCOMPRESSED but set to "BGR ". This prevents it from being opened in any player like VLC or Media Player. I opened the output AVI file using VirtualDub and edited the value using a hex editor and it fixed the issue.

Additionally, setting PixelType to rgb444le when extracting video stream results in error.
ffmpegextract(fullfile(pwd, 'myfile.mov'),fullfile(tempdir, 'myrgbfile.avi'), 'video', 'VideoCodec', 'raw', 'Range', [0 1], 'Units', 'seconds', 'PixelFormat', 'rgb444le')

Kesh Ikuma

Kesh Ikuma (view profile)

@Evegeny - That's peculiar. I just updated my ffmpeg to the latest version and it still works but I don't run Matlab in Mac, so you need to help me a little here (of course only if you are interested). Could you run ffmpegsetup with a breakpoint set on Line 46 and tell me what "fail" and "msg" values are returned from system call?

Thanks

Evgeny

Evgeny (view profile)

I get an error when running ffmpegsetup.m after specifying the executable binary

>> ffmpegsetup
/usr/local/Cellar/ffmpeg/2.4.2/bin/ffmpeg -version: Trace/breakpoint trap
Invalid FFMPEG executable specified.
Error using ffmpegsetup (line 58)
FFMPEG executable not found.

At the same time when I run the same command in bash terminal, I get the correct output.

OS X 10.9.5, Matlab R2014a 64bit, ffmpeg 2.4.2

bharath

Updates

2.2

(r8) Added missing files (private/isfullpath.m, private/rel2fullname.m, & private/relfile.m)

2.2

- Added ffmpegfilter.setdar & ffmpegfilter.setsar classes
- Fixed an issue with incorrect interaction between OutputFrameRate and Range options
- Other misc. small bug fixes

2.1

Added ffmpegcombine function, which let you marge media files using a filter graph

2.0.2

Edited the Description

2.0.2

Fixed a bug in PixelFormat option handling

1.4

Fixed a couple bugs: Range option handling and default progress display function

1.3

Major Release
- added animated GIF support
- added ffmpegimage2video function to convert images to video
- added ffmpegfiltergraph function and +ffmpegfilter class package to construct FFmpeg filter command with a linked ffmpeg filter objects.

1.2

Added VideoFlip option to ffmpegtranscode

1.1

bug fix

MATLAB Release
MATLAB 8.1 (R2013a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video