Savingsame video several times with while loop

6 views (last 30 days)
Laurine
Laurine on 26 Oct 2023
Commented: Laurine on 26 Oct 2023
Hello,
I have some video that i have to cut several times at different moment of the video. But when it tries to verify if the "filename_1.mp4" exist it just stop saying that it exist and cannot save the next one.
I tried this but still doesn't work :
%% video editing
%save current location
disp('outputing videos')
outputfile = [namevids '_1.mp4'];
i=1;
while exist(outputfile)
i=i+1;
outputfile = [namevids '_' num2str(i) '.mp4'];
end
comm = ['ffmpeg -i "' VideoDataPath '" -vf crop=' num2str(cropdim(1)) ':' num2str(cropdim(2)) ':' num2str(cropTL(1)) ':' num2str(cropTL(2)) ' -ss 00:' num2str(tstartmin,'%02.f') ':' num2str(tstartsec,'%.3f') ' -to 00:' num2str(tendmin,'%02.f') ':' num2str(tendsec,'%.3f') ' "' pathvid '\' outputfile '"'];
disp('Converting Video')
[status,cmdout]=system(comm);
if status~=0
error(['ffmpeg failed render, status = ' num2str(status) ' ' cmdout]);
end
fprintf('\nend\n');

Answers (1)

Dyuman Joshi
Dyuman Joshi on 26 Oct 2023
When checking for a file with an extension using exist(), you need to compare the output to 2.
%% Example
y = primes(50);
writematrix(y, 'file.xlsx')
exist('file.xlsx')
ans = 2
Refer to the description section of exist for more information.
Do you have to repeat this operation for multiple video files?
  3 Comments
Dyuman Joshi
Dyuman Joshi on 26 Oct 2023
I assume the all files are saved in the same folder.
Instead of checking for each file, a better approach is to get all the files in the folder and perform operations accordingly.
If there are files with .mp4 extension in the folder, that you do not want to work with, you can either relocate them or check the filename if it includes the phrase "Filename_" or not, using contains
%Read all the files in the folder that end with .mp4 extension
arr = dir('*.mp4');
n = numel(arr);
%loop through the files
for k=1:n
%this will give the name of the kth file
outputfile = arr.name(k);
%performing operations accordingly
comm = ['ffmpeg -i "' VideoDataPath '" -vf crop=' num2str(cropdim(1)) ':' num2str(cropdim(2)) ':' num2str(cropTL(1)) ':' num2str(cropTL(2)) ' -ss 00:' num2str(tstartmin,'%02.f') ':' num2str(tstartsec,'%.3f') ' -to 00:' num2str(tendmin,'%02.f') ':' num2str(tendsec,'%.3f') ' "' pathvid '\' outputfile '"'];
[status,cmdout]=system(comm);
if status~=0
error(['ffmpeg failed render, status = ' num2str(status) ' ' cmdout]);
end
end
Laurine
Laurine on 26 Oct 2023
i resolved by changing everything :
%% video editing
% save current location
disp('outputing videos')
outputfile = [namevids '_1.mp4'];
i = 1;
while i <= 100
% Check if the file with the current name exists
if ~exist(fullfile(pathvid, outputfile), 'file')
break; % File doesn't exist, exit the loop
else
i = i + 1;
outputfile = [namevids '_' num2str(i) '.mp4']; % Construct the next filename
end
end
% Check if we reached the maximum iterations without finding a suitable filename
if i > 100
error('Unable to find a suitable filename.');
end
% Continue with your existing code to process and save the file
comm = ['ffmpeg -i "' VideoDataPath '" -vf crop=' num2str(cropdim(1)) ':' num2str(cropdim(2)) ':' num2str(cropTL(1)) ':' num2str(cropTL(2)) ' -ss 00:' num2str(tstartmin,'%02.f') ':' num2str(tstartsec,'%.3f') ' -to 00:' num2str(tendmin,'%02.f') ':' num2str(tendsec,'%.3f') ' "' fullfile(pathvid, outputfile) '"'];
disp('Converting Video')
[status,cmdout] = system(comm);
if status ~= 0
error(['ffmpeg failed render, status = ' num2str(status) ' ' cmdout]);
end
fprintf('\nend\n');

Sign in to comment.

Products


Release

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!