I am trying to write a function that take as input an .avi file and returned the same video but with a delayed sound track. I am using ffmpeg and I encountered a problem. this is the function:
function Delyed = Dely_Movie_Soundtrack(filename,delayed)
Wav_File_Name = strrep(filename, '.avi', '.wav'); %the output file
wav = ['ffmpeg -i',' ',filename,' ','-vn -acodec copy',' ',Wav_File_Name]; %the command
system(wav); %executes the commant
[signal, Fs] = wavread('Will.wav');
size(signal)
end
I get the following error
Data compression format (Format #85) is not supported.
I read about it on the net, but I didn't find useful (working) links.
any help would be much appreciated.
Thanks
The solution was to specificaly instruct the program to encode in a "raw" state, otherwise it just gives the name .wav, but in fact it remains mp3. This is the command:
wav = ['ffmpeg -i',' ',filename,' ','-vn -coder:a raw',' ',Wav_File_Name];
thanks
Related
I'm doing some image processing on several thousand small .avi files. A small subset of the files appear to be damaged.
One type of damage seems to be a particular frame of the video that can't be read in. I added a try-catch block for this and it works well.
Another type of damage, however, is, according to VLC, "Broken or missing AVI Index". When VideoReader attempts to open files with this type of damage it crashes Matlab completely with the error, "MATLAB has encountered an internal problem and needs to close." And details message, "Segmentation violation detected at Wed Apr ..."
So my question is, is there any way to error check/skip videos that would cause this crash?
You may use ffmpeg for checking the integrity of video file.
See: How can I check the integrity of a video file (avi, mpeg, mp4…)?
Download static build of ffmpeg, and put ffmpeg.exe in your working directory.
Execute ffmpeg within Matlab using system command, and check the return status.
If status is not zero, the video file is corrupted.
You may also parse the output error message for finer logic.
Here is a code sample:
filename = 'input.avi';
if (isunix)
[status, cmdout] = system(['ffmpeg.exe -v error -i ', filename, ' -f null - 2']);
else
[status, cmdout] = system(['ffmpeg.exe -v error -i ', filename, ' -f null - 2>&1']);
end
if (status ~= 0)
%Dispaly cmdout if file is damaged.
disp([filename, ' is corrupted. Error: ', cmdout]);
end
I have a code in matlab where voice is recorded and saved as .wav file with name say.wav.
But the problem which i am facing is, each time i run the code the .wav file gets rewritten. But I want the voice to be recorded into a new .wav file. How can i do this in matlab?
The code is:
Fs = 1E+4;
nBits = 24;
nChannels = 1;
sig = audiorecorder(Fs, nBits, nChannels);
recordblocking(sig,5);
sigsound = getaudiodata(sig);
t= linspace(0, size(sigsound,1), size(sigsound,1))/Fs;
cd F:\1hp_laptop\c\my_files
filename = 'say.wav';
audiowrite(filename, sigsound, Fs)
It is getting rewritten because you have used the constant file name.You need to make your .wav file unique to ensure that it is getting newly created. You can add current time in milliseconds to the file name to make it unique .
As Nilu said, your problem is the filename is constant in your script/function.
One option, as stated, would be to use some kind of timestamp, e.g. instead of
filename = 'say.wav';
you can use
filename = ['say_', datestr(now,'FFF'), '.wav'];
Alternatively, and depending upon your audio file length (if it is long enough), you can ask the user for a distinctive filename, being it by encapsulating all your code into a function and asking for a string to use as parameter filename or by using Matlab's input():
filename = input('give me a filename: ', 's');
I've been trying to write image files to the specified folder, but I always receive this error:
Unable to open file
"C:\Users\Dani\Desktop\code_version_1.0\myImages"
for writing. You may not have
write permission.
Is there a way to fix this? Thanks.
for i=1:numberOfFiles
filename=fileList{i};
img=imread(filename,'jpg');
image = imresize(img, [150,150]);
folder='C:\Users\Dani\Desktop\code_version_1.0\myImages';
if ~exist(folder,'dir')
mkdir(folder);
end
imwrite(image,folder,'jpg');
end
Your call to imwrite has an invalid second parameter. You gave it a folder when it is asking for a file path.
Here's a possible work-around:
outfile = fullfile(folder, 'output.jpg');
imwrite(image, outfile, 'jpg');
How about using the imwrite like this :
imwrite(image,'C:\Users\Dani\Desktop\code_version_1.0\myImages\image.jpg');
you can after append things as you like. check this link
I have several data log files (here: 34) for those I have to calculate some certain values. I wrote a seperate function to publish the results of the calculation in a pdf file. But I only can publish one file after another, so it takes a while to publish all 34 files.
Now I want to automize that with a loop - importing the data, calculate the values and publish the results for every log file in a new pdf file. I want 34 pdf files for every log file at the end.
My problem is, that I couldn't find a way to rename the pdf files during publishing. The pdf file is always named after the script which is calculating the values. Obviously the pdf is overwritten within a loop. So at the end everything is calculated, but I only have the pdf from the last calculated log file.
There was this hacky solution to change the Matlab publish script, but since I don't have admin rights I can't use that:
"This is really hacky, but I would modify publish to accept a new option prefix. Replace line 93
[scriptDir,prefix] = fileparts(fullPathToScript);
with
if ~isfield(options, 'prefix')
[scriptDir,prefix] = fileparts(fullPathToScript);
else
[scriptDir,~] = fileparts(fullPathToScript);
prefix = options.prefix; end
Now you can set options.prefix to whatever filename you want. If you want to be really hardcore, make the appropriate modifications to supplyDefaultOptions and checkOptionFields as well."
Any suggestions?
Thanks in advance,
Martin
Here's one idea using movefile to rename the resultant published PDF on each iteration:
for i = 1:34
file = publish(files(i)); % Replace with your own command(s)
[pathStr,fileName,ext] = fileparts(file);
newFile = [pathStr filesep() fileName '_' int2str(i) ext]; % Example: append _# to each
[success,msg,msgid] = movefile(file,newFile);
if ~success
error(msgid,msg);
end
end
Also used are fileparts and filesep. See this question for other ways to rename and move files.
I'm trying to convert a bunch of mp4 files to avis using the code below. I know that the conversion works as I've verified it on single files. However when I try to read in all mp4s from a directory the 'files' struct I've tried to create is empty. I'm relatively new to programming in MatLab so I'm sure I'm making many silly errors. How can I properly load in and iterate over multiple mp4 files in order to convert them all to avis? Thanks!
NB: I have tried different methods of conversion, but MatLab seems to be the best at ensuring no size or quality distortion.
files = dir(fullfile('E:\Heather\Summer2013\*.mp4'));
for i = 1:length(files)
filename = files(i).name;
%create objects to read and write the video
readerObj = VideoReader('files(i).name.mp4');
writerObj = VideoWriter('files(i).name.avi','Uncompressed AVI');
%open AVI file for writing
open(writerObj);
%read and write each frame
for k = 1:readerObj.NumberOfFrames
img = read(readerObj,k);
writeVideo(writerObj,img);
end
close(writerObj);
end
A call to dir produces a struct array where file names are stored as strings in the name field. That is, files(i).name contains a string. To use it,
readerObj = VideoReader(files(i).name); % no quotes, no extension
To write with a different file extension, determine the file base with fileparts and add the .avi extension. For example:
>> str = 'mymovie.mp4';
>> [~,fileBase,fileExt] = fileparts(str)
fileBase =
mymovie
fileExt =
.mp4
>> writerObj = VideoWriter([fileBase '.avi'],'Uncompressed AVI');
You are trying to read the file with the name ('files(i).name.mp4') which obviously not exists. filename should be the corret parameter for the reader, for the writer you have to replace the extension.