Information about video.MultimediaFileWriter - matlab

I have used video.MultimediaFileWriter to write frames from input AVI video file to an output AVI video file. After the output file is created the size of the output file is very large. I have used VideoCompressor's provided in MATLAB options. For example: for an input video size of 3.42MB after using compression techniques provided by MATLAB the output video size is 98.5MB.
Can anyone tell me how to bring the output AVI file to size of the input file?

The creation of videos using H.264 was added in R2012a. Older versions support the less efficient codecs MJPEG and DV, which probably explain your large files. However, this is not a major problem because you can recompress your videos using free tools, such as VirtualDub and x264. Here is a tutorial.

Related

How to pass a sequence of jpeg data from MATLAB to ffplay

I want to playback sensor data from different sensors in matlab synchronously. For most data I use updating plots. However, I have difficulties to find an efficient way to playback a sequence of jpeg data as video on screen (720p or 1080p).
One way i tried is to use the matlab function to convert the jpeg data to raw matlab images (https://nl.mathworks.com/help/robotics/ref/readimage.html) and then display it with the imshow function. This way is very slow and therefore not suitable.
Another way was to write the jpeg data to a file (fwrite) and using the matlab system command to execute ffplay:
ffplay -loglevel panic -framerate 30 -i output.jpg
This works for showing single .jpeg's, but is becoming tricky when I want to play a sequence of jpeg data as a movie. If I write all jpeg data sequentially to a file and run ffplay afterwards, I can playback the movie correctly. But what I want to playback on the fly (i.e. stream) and not wait until all data is written to the file before playback.
Is there a suitable way to share the jpeg data from matlab directly to ffplay in order to play a sequency of jpeg data on-the-fly? Any other suggestions to tackle this problem are also welcome.

MATLAB avi file read

Is there a way to read .avi video file frame by frame using mmread or other function, similar to using videoReader, and readFrame functions?
I used mmread but it took a long time to read each frame as it read all the frames before the specified frame.
In general the media and movie reading in Matlab is cumbersome. I changed to python because of it. I would recommend to split the movie into single image files using tools like avconv or ffmpg and then work on the images, as the image manipulation toolbox is much faster.
If you want to go forward and backward through any movie, especially backwards is very slow. Very often it is implemented, by rewinding and than seeking out your frame, so basically to go from frame 100 to 99, Matlab rewinds to 0 and then seeks through the binary stuff to 99.

Matlab vision.VideoFileWriter output is too big

I'm using Computer Vision System Toolbox in Matlab (R2015a, Windows7) to mask frames in the video file and write them into a new video file. By masking, I replace about 80% of the image with 0s and 1s:
videoFileReader = vision.VideoFileReader(fin);
videoFileWriter=vision.VideoFileWriter(fout, ...
'FileFormat', 'MPEG4', 'FrameRate', videoFileReader.info.VideoFrameRate);
frame = step(videoFileReader);
frame_new=mask(frame); %user function
step(videoFileWriter, frame_new);
The size (1080 x 1920 x 3) and the format (single) of the original and modified frames remain the same. Yet the masked videos are much bigger than the original ones, e.g. 1GB original video turns into nearly 4GB after masking. These large new files can not be opened (Windows 7, VLC media). Handbrake does not recognize them as a legit video file either.
When I mask only about 20% of the image, the masked video still come out large (up to 2.5Gb), but I have no problem opening these.
I tried adding 'VideoCompressor', 'MJPEG Compressor', but this gives a warning.
videoFileWriter=vision.VideoFileWriter(fin, 'FileFormat', 'MPEG4', ...
'FrameRate', videoFileReader.info.VideoFrameRate, 'VideoCompressor', 'MJPEG Compressor');
<...>
Warning: The VideoCompressor property is not relevant in this configuration of the System object.
We have TBs of video data to deidentify, so any suggestion would be much appreciated.
Thanks!
Larissa,
The size of the output MPEG-4 file can be controlled by adjusting the Quality parameter of the system object. This is a value from 0-100 which controls the output bitrate. So, higher the quality, larger the file. The default value is 75. The system object uses the Microsft API's to create MPEG-4 files.
Secondly, you need to call release(videoFileWriter) to complete writing the file. I just want to confirm that you are doing it and have just omitted it for the purposes of this code snippet.
The VideoCompressor property is not valid for MPEG-4 file format because the compressor to be used is fixed. You can choose that property only when you write out AVI files. However, you probably will not reach the same level of compression as MPEG-4.
Hope this helps.
Dinesh
Download ffmpeg here:https://git.ffmpeg.org/ffmpeg.git
For windows, open a bash terminal and run:
cat <path to folder with images>/*.png | <path to ffmpeg bin folder>/ffmpeg.exe -f image2pipe -i - output.mkv
For unix, do similar but download the appropriate build of ffmpeg.
I tried on a 7.90GB folder and got a 6.4MB .mkv-file. Works like a charm!

error using videoWriter in Matlab (asyncio)

I am trying to use videoWriter in Matlab 2013a on a Windows 7 64 bit machine. I also have Matlab 2009a installed (which could be the issue)
My code fails here:
vid = VideoWriter('TestVideo');
This gives me the following error:
Error using VideoWriter (line 240)
Undefined variable "asyncio" or class "asyncio.Channel".
Now if I do this:
vid = VideoWriter('TestVideo.mp4','MPEG-4');
I get this error:
Error using VideoWriter (line 168)
The specified profile is not valid.
I tried installing codecs, but that didn't help.
I read somewhere that someone had a similar problem, and it was due to a corrupt path and it could be caused by an older installation of Matlab (2007), but nothing about what was eventually done to fix it. I have tried to add the asyncio folder in the Matlab directory to the Matlab path, but again no luck.
I have further tried to find out what videoWriter does regarding asyncio, but cannot find any clues in the code, and I honestly don't really know what asyncio is.
Uninstalling Matlab 2009a is not an option.
So can anyone help me please?
BR Gorm
You need to use the second way of invoking VideoWriter.... so doing:
vid = VideoWriter('TestVideo.mp4','MPEG-4');
However, you are getting this error:
Error using VideoWriter (line 168) The specified profile is not valid.
This is due to the fact that you probably don't have the MPEG-4 codecs installed on your computer. You can verify this by doing this in MATLAB:
profiles = VideoWriter.getProfiles()
Run the above code in the Command Prompt. You get a list of all possible profiles that you can use for writing a video to file using VideoWriter. For example, I'm using MATLAB R2013a on Mac OS and this is what I get:
>> profiles = VideoWriter.getProfiles()
Summary of installed VideoWriter profiles:
Name Description
---------------- -----------------------------------------------------------------------
Archival Video file compression with JPEG 2000 codec with lossless mode enabled.
Grayscale AVI An AVI file with Grayscale Video Data
Indexed AVI An AVI file with Indexed Video Data
MPEG-4 A MPEG-4 file with H.264 Compression
Motion JPEG 2000 Video file compression with JPEG 2000 codec.
Motion JPEG AVI An AVI file with Motion JPEG compression
Uncompressed AVI An AVI file with uncompressed RGB24 video data
If you don't see MPEG-4 on your list of profiles, then you currently don't have the profile installed. As such, you will have to use one of the provided profiles that MATLAB has for you.
Since you're running Windows, one suggestion I have is to install the Windows 7 Codec Pack suite: http://www.windows7codecs.com/. I know you said that when you installed codecs, it didn't work but you can try the above link if you haven't done it already.
The above website contains all of the codecs you would need to play video files in Windows. This will hopefully allow you to use VideoWriter once you install the codecs.
So I found an answer to my own question. It was the path that was broken. Problem can be fixed easily by setting the Matlab search path to default.
This is an old question, but I also wanted to add my two cents. What solved the problem for me was looking at my Windows version. Many companies have a Windows N version installed on their computers. These versions don't have all of the codecs and software, needed for playing and creating media files. So if you are using Matlab at work this might be the problem.
The fix is downloading the Windows Media Feature Pack for N Versions and to make sure that no other codec packs, like K-Lite are installed.

Force MATLAB mmreader or avireader to use a different codec?

How do I force MATLAB functions to use a different codec when I need to play an AVI file?
I am using windows7 and I find that indeo5 codec is not working properly because of the OS.
All code works fine on XP.
Thanks!
If your question is about how to play an avi using a codec other than the one used to create it, you'll have to use some kind of image conversion software ( RAD video tools aka bink and smacker is popular and free ) to recompress the movie using the new codec. There may be some loss of video quality.
If your question is how to tell MATLAB to create an avi using a different codec, type help avifile to see the full documentation.
If your problem is that MATLAB is choking on a codec it says it supports and if you're using 64 bit MATLAB, I've had this problem. The short answer, unfortunately, is that there are no 64 bit codecs that work with MATLAB. There are two workarounds, and they both suck. One is to install 32 bit MATLAB and use that when you need to access an avi. The other is to use some kind of image conversion software to convert your avi to an image stack, then load that with MATLAB.
(EDIT: In newer versions of MATLAB, you can use the VideoWriter object to write MP4 and other compressed formats in 64 bit windows)