動画をmp4で保存
videoWriter()の第2引数で'MPEG-4'と指定する
ビデオ ファイルを書き込むオブジェクトの作成 - MATLAB - MathWorks 日本
Linuxでは対応していない模様…
aviをffmpegでmp4に変換する関数
ffmpegをinstallし、パスを通しておく必要がある
code:matlab
function mp4Path = avi2mp4(aviPath, deleteOriginal)
% Convert AVI to MP4 using ffmpeg
%
% Input:
% aviPath - string, path to the input AVI file
% deleteOriginal - boolean, whether to delete the original AVI file (default: false)
%
% Output:
% mp4Path - string, path to the output MP4 file
% Set default value for deleteOriginal if not provided
if nargin < 2
deleteOriginal = false;
end
% Generate output MP4 filename
mp4Path = regexprep(aviPath, '\.avi', '.mp4');
% Construct ffmpeg command based on the operating system
if isunix % for linux
cmd = sprintf('ffmpeg -i %s -y -movflags faststart -pix_fmt yuv420p -crf 9 -preset slow %s', aviPath, mp4Path);
elseif ispc % for windows
cmd = sprintf('ffmpeg.exe -i %s -y -movflags faststart -pix_fmt yuv420p -crf 9 -preset slow %s', aviPath, mp4Path);
else
error('Unsupported operating system');
end
% Execute ffmpeg command
status, cmdout = system(cmd);
% Check if the conversion was successful
if status ~= 0
error('FFmpeg conversion failed with error: %s', cmdout);
end
% Delete the original AVI file if requested
if deleteOriginal
delete(aviPath);
end
end
動画