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.
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);
for n = 1:numel(phi)
print('-dpng',sprintf('test%02d.png',n)); % create an intermediate PNG file
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
TOOLBOX FUNCTION AND CLASS LIST:
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
Kesh Ikuma (2021). FFmpeg Toolbox (https://www.mathworks.com/matlabcentral/fileexchange/42296-ffmpeg-toolbox), MATLAB Central File Exchange. Retrieved .
In R2018b, isfullpath does not work. The final line is expecting a cell as output from regex, but gets a double. I'm not sure of the intent for multiple paths, but changing line 32 to:
tf = ~isempty(regexp(path,cond,'once'));
sir i need H.265 matlab code
Thanks for making this!
ffmpegimages2video doesn't accept VideoScale as an input in the latest version
@maharani meidy - Unfortunately, I'm not planning on implementing any other built-in codec options. But you can run whatever you want ffmpeg by using ffmpegexec. Look up the command line arguments to perform x265 coding online and build the command line string in Matlab and run ffmpegexec with it.
how to use it for x265 ?
@Luca - Unfortunately no... at this point. I'm actively developing an FFmpeg-based VideoReader class as a new addition to this class, but it's release date is still TBD. Come back in a few month to see if it's out.
First of all many thanks for having developed such a useful interface for ffmpeg!
I was wondering if would it be possible to extract a specific frame from a video?
I simply need to seek over a video file and extract the RGB content of specific frames, aiming to mantain the result onto the Matlab workspace, avoiding the need to write the result to a file and reading it afterward (in order to have a faster execution).
Many thanks in advance.
@Arnaud - AFAIK, No, FFmpeg (the executable not the API library) is just an offline transcoder. But who knows, it could very well be possible. You need to visit www.ffmpeg.org to see if ffmpeg supports what you want to do and if so how to run ffmpeg from the command prompt/terminal. Once you figure that out, then you can do the same from MATLAB using this toolbox's ffmpegexec function.
I mean, convert real time video 10 bits in 8 or 16 bits to display it without a black screen in the video player
Is it possible to convert a real time video with this toolbox ?
@Sajad - Yes, FFmpeg binary supports it AFAIK. However, it'll be a while before I can get to it. You can look into how to use ffmpegexec or ffmpegexecargs to execute H.265 transcoding from MATLAB.
Is it possible to make it handle H.265 in windows OS?
Is there a way to add functionality for conversion of .mov files to .ogg or .ogv files?
@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.
I cant find exe file in the extracted file so where is the exe file
@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.
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')
@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?
I get an error when running ffmpegsetup.m after specifying the executable binary
/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
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!