1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
|
clear; clc;
inputMp4 = './Video Project 2.mp4';
outputGif = fullfile(pwd, 'output_compressed_mwc_PC.gif');
startTimeSec = 0; clipDurationSec = 30; gifLoopTimeSec = 10; gifFps = 8;
outputWidth = 720; colorCount = 128; ditherMode = 'nodither'; loopCount = Inf;
method = 'auto'; ffmpegExe = 'C:\Users\ASUS\AppData\Local\Microsoft\WinGet\Packages\Gyan.FFmpeg_Microsoft.Winget.Source_8wekyb3d8bbwe\ffmpeg-8.1.2-full_build\bin\ffmpeg.exe';
if ~isfile(inputMp4) error('Input MP4 not found: %s', inputMp4); end
if startTimeSec < 0 error('startTimeSec must be >= 0.'); end
if clipDurationSec <= 0 || gifLoopTimeSec <= 0 || gifFps <= 0 error('clipDurationSec, gifLoopTimeSec, and gifFps must all be > 0.'); end
if colorCount < 2 || colorCount > 256 error('colorCount must be between 2 and 256 for GIF output.'); end
if ~isempty(outputWidth) && outputWidth <= 0 error('outputWidth must be [] or a positive number.'); end
videoObj = VideoReader(inputMp4);
sourceStartSec = min(startTimeSec, videoObj.Duration); sourceEndSec = min(startTimeSec + clipDurationSec, videoObj.Duration); actualClipDurationSec = sourceEndSec - sourceStartSec;
if actualClipDurationSec <= 0 error('The selected segment is empty. Check startTimeSec and clipDurationSec.'); end
gifFrameCount = max(1, round(gifLoopTimeSec * gifFps)); delayTimeSec = gifLoopTimeSec / gifFrameCount; speedRatio = actualClipDurationSec / gifLoopTimeSec;
fprintf('Input video duration: %.3f s\n', videoObj.Duration); fprintf('Source segment: %.3f s to %.3f s, duration %.3f s\n', ... sourceStartSec, sourceEndSec, actualClipDurationSec); fprintf('GIF loop time: %.3f s, GIF FPS: %.3f, frames: %d\n', ... gifLoopTimeSec, gifFps, gifFrameCount); fprintf('Playback speed ratio: %.3fx\n', speedRatio);
method = lower(method); if ~any(strcmp(method, {'auto', 'ffmpeg', 'matlab_fast'})) error('method must be ''auto'', ''ffmpeg'', or ''matlab_fast''.'); end
ffmpegAvailable = false; if strcmp(method, 'auto') || strcmp(method, 'ffmpeg') ffmpegAvailable = isFfmpegAvailable(ffmpegExe); end
if strcmp(method, 'ffmpeg') && ~ffmpegAvailable error('FFmpeg was requested but is not available: %s', ffmpegExe); end
useFfmpeg = strcmp(method, 'ffmpeg') || (strcmp(method, 'auto') && ffmpegAvailable);
if useFfmpeg fprintf('Using FFmpeg backend.\n'); runFfmpegGif(ffmpegExe, inputMp4, outputGif, sourceStartSec, ... actualClipDurationSec, gifLoopTimeSec, gifFps, outputWidth, ... colorCount, ditherMode, loopCount); else fprintf('FFmpeg not found. Using MATLAB sequential VideoReader backend.\n'); runMatlabFastGif(videoObj, outputGif, sourceStartSec, sourceEndSec, ... actualClipDurationSec, gifFrameCount, delayTimeSec, ... outputWidth, colorCount, ditherMode, loopCount); end
fprintf('GIF saved to: %s\n', outputGif); outputInfo = dir(outputGif); if ~isempty(outputInfo) fprintf('Output GIF size: %.2f MB\n', outputInfo.bytes / 1024 / 1024); end
function ok = isFfmpegAvailable(ffmpegExe) [status, ~] = system(sprintf('%s -version', quoteCmdArg(ffmpegExe))); ok = (status == 0); end
function runFfmpegGif(ffmpegExe, inputMp4, outputGif, startTimeSec, ... clipDurationSec, gifLoopTimeSec, gifFps, outputWidth, colorCount, ... ditherMode, loopCount)
ptsScale = gifLoopTimeSec / clipDurationSec;
if isempty(outputWidth) scaleFilter = ''; else scaleFilter = sprintf(',scale=%d:-1:flags=lanczos', round(outputWidth)); end
if strcmpi(ditherMode, 'nodither') ffmpegDither = 'none'; else ffmpegDither = 'sierra2_4a'; end
filterText = sprintf(['[0:v]setpts=%.12g*PTS,fps=%.12g%s,split[s0][s1];' ... '[s0]palettegen=max_colors=%d[p];[s1][p]paletteuse=dither=%s'], ... ptsScale, gifFps, scaleFilter, round(colorCount), ffmpegDither);
if isinf(loopCount) ffmpegLoopCount = 0; else ffmpegLoopCount = max(0, round(loopCount - 1)); end
cmd = sprintf('%s -y -ss %.6f -t %.6f -i %s -filter_complex %s -loop %d %s', ... quoteCmdArg(ffmpegExe), startTimeSec, clipDurationSec, quoteCmdArg(inputMp4), ... quoteCmdArg(filterText), ffmpegLoopCount, quoteCmdArg(outputGif));
[status, cmdout] = system(cmd); if status ~= 0 error('FFmpeg failed:\n%s', cmdout); end end
function runMatlabFastGif(videoObj, outputGif, sourceStartSec, sourceEndSec, ... actualClipDurationSec, gifFrameCount, delayTimeSec, ... outputWidth, colorCount, ditherMode, loopCount)
targetTimes = sourceStartSec + ... (0:gifFrameCount - 1) * actualClipDurationSec / gifFrameCount;
videoObj.CurrentTime = sourceStartSec; lastFrameRgb = [];
progressStep = max(1, floor(gifFrameCount / 10));
for k = 1:gifFrameCount targetTime = targetTimes(k);
while hasFrame(videoObj) && videoObj.CurrentTime <= targetTime && ... videoObj.CurrentTime < sourceEndSec lastFrameRgb = readFrame(videoObj); end
if isempty(lastFrameRgb) if hasFrame(videoObj) lastFrameRgb = readFrame(videoObj); else error('Could not read any frame from the selected segment.'); end end
frameRgb = lastFrameRgb;
if ~isempty(outputWidth) scale = outputWidth / size(frameRgb, 2); outputHeight = round(size(frameRgb, 1) * scale); frameRgb = imresize(frameRgb, [outputHeight round(outputWidth)], 'bicubic'); end
[frameIndexed, colorMap] = rgb2ind(frameRgb, colorCount, ditherMode);
if k == 1 imwrite(frameIndexed, colorMap, outputGif, 'gif', ... 'LoopCount', loopCount, ... 'DelayTime', delayTimeSec); else imwrite(frameIndexed, colorMap, outputGif, 'gif', ... 'WriteMode', 'append', ... 'DelayTime', delayTimeSec); end
if k == 1 || k == gifFrameCount || mod(k, progressStep) == 0 fprintf('Written %d/%d GIF frames.\n', k, gifFrameCount); end end end
function q = quoteCmdArg(s) s = char(s); q = ['"' strrep(s, '"', '\"') '"']; end
|