MATLAB: Use of vision.VideoFileWriter and vision.VideoFileReader - matlab

I'm trying to convert a .avi file with audio to a .mp4 file. I wrote this script 'avi2mp4.m' using the Computer Vision System Toolbox v7.2 with the MATLAB R2016b.
vfr = vision.VideoFileReader('Cris Drift vs Patrick.avi', 'AudioOutputPort',true);
vfw = vision.VideoFileWriter('Cris Drift vs Patrick.mp4', 'FileFormat','MPEG4', 'AudioInputPort',true, ...
'FrameRate',vfr.info.VideoFrameRate, 'Quality',90);
while ~isDone(vfr)
[frame, audio] = vfr(); % [frame, audio] = step(vfr);
vfw(frame, audio); % step(vfw, frame, audio);
end
release(vfr);
release(vfw);
but i get this error:
Error using vision.VideoFileWriter/parenReference
Too many input arguments; expected 1 (in addition to the object handle), got 2.
Error in avi2mp4 (line 16)
vfw(frame, audio);
I don't know why? I have to pass the audio data as an argument to write it with the video data. It's the same syntax as described in the MATLAB Documentation
Documentation for Video File Writer Object
Documentation for Video File Reader Object

With vision.VideoFileWriter you can write both audio and video only when the format is AVI or WMV. If you received a warning about AudioInputPort property not relevant when you set that property that means audio is not supported in that configuration.

Related

MATLAB *.pcm to *.wav converting

How can I convert *.pcm audio file to *.wav audio File in MATLAB-Code?
I just need to insert a header, but how it is work?
Thank you very much!
Since you didn't specify it, I'm assuming you are using Matlab 2018b, so I will point you to the most recent documentation about audioread:
As you can see, PCM is not on the supported format list.
You should try to look if you can parameter your AudioRecorder to record your audio to another format within the ones in the supported list: .wav, .ogg, .flac, .au, .aif, .aifc, mp3, .mp4...
An alternative option, without using audioread, would be to import pcm data like any other data file, then convert it to 16 bit wav. I assume sample rate is 44100 Hz.
fid = fopen('audioFile.pcm'); % Open raw pcm file
audio = int16(fread(fid, Inf, 'int16')); % Convert data into 16 bit
fclose(fid); % Close pcm file
audiowrite('audioFile.wav', audio, 44100,'BitsPerSample', 16); % Write wav

How to create a grayscale video

I wrote a code in Matlab, in which I have to read a color video, retrieve the frames from the video perform some operations with the frames and then again make a video from the operated frames i wrote a simple code for converting each frame to gray-scale and thus generating the gray-scale video and it showed the following error:
Warning: No video frames were written to this file. The file may b invalid.
In VideoWriter.VideoWriter>VideoWriter.close at 307
In VideoWriter.VideoWriter>VideoWriter.delete at 256
In getFrames2Image at 9
Following is the code i wrote
a=VideoReader('test.mp4');
images=cell(a.NumberOfFrames,1);
for img=1:a.NumberOfFrames
b=read(a,img);
c=rgb2gray(b);
images{img}=b;
end
outp=VideoWriter('output');
outp.FrameRate=30;
open(outp);
for i=1:length(images)
frame=im2frame(images{i});
writeVideo(outp,frame);
end
close(outp);
As requested here are the details of my matlab version:
Matlab R2013a(8.1.0.604)
64 bit(win64)
and link of the sample video
https://drive.google.com/open?id=0B-MZ8myQc4ZKU0E1dURPRUhwd1k

Unable to create audio compressor filter

Function takes as input a string, the name of the video. It's read the video with the vision.VideoFileReader function and returns the same video, using thevision.VideoFileWriter function. Both the input video that the output videos have audio. Processing of a video about 6 MB, i have the output of a video more than 1 GB. The function has no errors, but i have to compress. Using the VideoCompressor, can compress the video up to 350 MB, i would use theAudioCompressor, but by obtaining an error.
This is my code, the following is the error returned.
function [ nFrames ] = showMovie( video )
v = VideoReader(video);
videoFReader = vision.VideoFileReader(video);
videoFWriter = vision.VideoFileWriter('FrameRate',v.FrameRate,'AudioInputPort',1,'VideoCompressor', 'MJPEG Compressor','AudioCompressor','MJPEG Compressor');
[audio,fs] = audioread(video);
op=floor(fs/v.FrameRate);
nFrames = 0;
while ~isDone(videoFReader)
nFrames=nFrames+1;
frame=step(videoFReader);
audios=audio( (nFrames-1)*op + 1 : nFrames*op , : );
step(videoFWriter,frame,audios);
end
release(videoFReader);
release(videoFWriter);
end
I can't use the property AudioCompressor. I tried both the Compressor MJPEG and the DV Video Encoder value, but I get this error:
Error using VideoFileWriter / step
Unable to create audio compressor filter
Error in showMovie (line 15)
step (videoFWriter, frame, audios);
You're trying to specify a video compressor for your audio which is resulting in the error. Different compression algorithms are used for the audio and video components. You need to specify a valid audio compressor.
To get a list of available options on your machine, you can use tab completion in the command window:
videoWriter.AudioCompressor = ' % <tab> key
As Rotem has noted, this list can also include Video compression algorithms, but it should also include any valid audio compression algorithms for which you have the correct codecs installed.
The only AudioCompressor compressor that works in my system is: 'None (uncompressed)'
I tried it on 64 bit version of Matlab (R2014b).
The reason for that is that my Windows system lacks x64 (64 bit) audio codec supported by Matlab.
Note: 64 bit Matlab requires x64 codecs, and 32 bit Matlab requires x86 codecs.
When I use videoWriter.AudioCompressor = ' % <tab> key as Suever mentioned,
When I tried the same code using 32 bit version of Matlab (R2013b), I got the following list:
AC-3 ACM Codec
AC-3 ACM Extensible
CCITT A-Law
CCITT U-Law
GSM 6.10
IMA ADPCM
Microsoft ADPCM
None (uncompressed)
Note: The video codecs shown in 64 bit version, are not displayed in the 32 bit Matlab.
I guess displaying video codecs in AudioCompressor is a Matlab bug.
Just for the record, I tried the <tab> key before Suever posed his answer.
I read about it in Matlab documentation: http://www.mathworks.com/help/vision/ref/vision.videofilewriter-class.html
To launch the tab completion functionality, type the following up to the open quote.
y.VideoCompressor='
A list of compressors available on your system will appear after you press the Tab key
The following code sample works in system:
video = 'xylophone.mpg';
v = VideoReader(video);
videoFReader = vision.VideoFileReader(video);
videoFWriter = vision.VideoFileWriter('FrameRate',v.FrameRate,'AudioInputPort',1,'VideoCompressor', 'MJPEG Compressor','AudioCompressor', 'None (uncompressed)');
[audio,fs] = audioread(video);
op=floor(fs/v.FrameRate);
nFrames = 0;
while ~isDone(videoFReader)
nFrames=nFrames+1;
frame=step(videoFReader);
audios=audio( (nFrames-1)*op + 1 : min(nFrames*op, length(audio)) , : );
%Handle last audio sample.
if (length(audios) < op)
audios = [audios; audios(1:op - length(audios), :)];
end
step(videoFWriter,frame,audios);
end
release(videoFReader);
release(videoFWriter);
I was searching the Web for free x64 audio codec that works with Matlab, but I couldn't find one.

Big tiff read and view in Matlab

I have downloaded a btf file (big tiff) from the links below, how can I read it and "imshow" it? is there a way to convert it to tiff format as the btf is not that common?
Link:
https://drive.google.com/file/d/0ByhuP_NuuARtSW9aeTdPUTlRdWM/view?usp=drive_web
http://www.photomacrography.net/forum/viewtopic.php?t=28990&sid=cca737a2e0bc7ea3e2e41f0d6e75f5a9
I used this code:
t = Tiff('d:/Image_687.btf','w8');
imageData = read(t);
and got this error:
Error using tifflib
Unable to retrieve PhotometricInterpretation.
Error in Tiff/getTag (line 838)
tagValue = tifflib('getField',obj.FileID,Tiff.TagID.(tagId));
Error in Tiff/read (line 1487)
photo = obj.getTag('Photometric');
Error in Untitled2 (line 2)
imageData = read(t);
The real issue with your code is the second parameter that you have passed to Tiff. As the documentation states, the second parameter indicates in what mode to open the file. You have specified w8 which the documentation states is:
open TIFF file for writing a BigTIFF file; discard existing contents.
This means that it is deleting your image before you even start! If you want to use the Tiff class, you'll want to either use no second parameter or the r parameter to open the file for reading.
t = Tiff('Image_687.btf');
t = Tiff('Image_687.btf', 'r');
That being said, in general it is better to try to load it with a higher level function such as imread. The Tiff class is a much lower-level function that can be a little harder to manipulate but may provide some needed specialty functionality.
im = imread('Image_687.btf');
size(im)
3072 4080 3
I had to do a little manipulation for display because the RGB values weren't between 0 and 255
im = double(im);
im = uint8(255 * im ./ max(im(:)));
imshow(im);

video to frames in matlab

I have a code to extract frames from a video.The code is given below
addpath('E:\project\coding\wrk_ongoing\Images');
obj = mmreader('ace.mp4');
vid = read(obj);
frames = obj.NumberOfFrames; %Read the Total number of frames and displyed in command window
ST='.jpg';
cd frames
for x = 1:5 % extracting the 5 frames from video
Sx=num2str(x);
Strc=strcat(Sx,ST);
Vid=vid(:,:,:,x);
imwrite(Vid,Strc);
end
cd ..
This code works only for some videos.I tested for different videos with .mp4 extension.Some of them works well. But input videos shows error as
??? Error using ==> vid2frame at 6
Initialization failed. (No combination of intermediate filters could be found to make the
connection.)
how can I solve this ?
That error is due to your video file itself. From the looks of it, MATLAB has a problem reading that file probably because the file is badly encoded or the video was encoded with a codec that is not supported by MATLAB or does not exist on your computer.. See this question for a similar problem: no combination of intermediate filters could be found
This has nothing to do with MATLAB, but the error was what you encountered and the answer was to essentially re-encode the video file in a format that is compatible with your operating system and MATLAB.
Good luck!