Hi everyone. I have a large number of video files to process. What I want to do is extract parts from each video file. For example if I have a 10 minute Video, I want to cut away everything, so that I'm just left with for example minute 5 to 6, and save this as a new video file, with both video and audio!!!
What I did so far is read in the original Video File, split it up into Frames, select the specific frames, and save them as a new file, but that leaves me with no audio.
how can I get the audio as well? or is there possibly an easier way? some already existing function?
Thank you very much!
My code so far:
function [] = extractscene(nameOfFile, beginTime, endTime, SceneClassifier)
inputName = strcat(nameOfFile,'.mp4');
outputName = strcat(nameOfFile,'_',num2str(SceneClassifier),'_',num2str(beginTime),'_',num2str(endTime),'.avi');
a = VideoReader(inputName);
beginFrame = beginTime * a.FrameRate;
endFrame = endTime * a.FrameRate;
vidObj = VideoWriter(outputName);
open(vidObj);
for img = beginFrame:endFrame
b = read(a, img);
writeVideo(vidObj,b)
end
close(vidObj);

 Réponse acceptée

Walter Roberson
Walter Roberson le 22 Avr 2017

1 vote

vision.VideoFileReader() can read audio and video.
You would loop reading frames and audio. Drop those until you reach the time you want to start. Start outputing. When you reach the end of the time you want, break out of the loop.
Unfortunately there does not appear to be a good way to translate between frame count and time in the case of variable frame rate. Notice https://www.mathworks.com/help/vision/ref/vision.videofilereader.info.html
VideoFrameRate: Frame rate of the video stream in frames per second. The value may vary from the actual frame rate of the recorded video, and takes into consideration any synchronization issues between audio and video streams when the file contains both audio and video content. This implies that video frames may be dropped if the audio stream leads the video stream by more than 1/(actual video frames per second).

6 commentaires

ceinem
ceinem le 23 Avr 2017
Thanks a lot for the answer!! Just managed to get this running as well, but am still having some problems. Apparently it doesn't support audio output for MP4 files. Avi files are ok for me, but they appear not to be compressed, any idea on how to get the avi compression working??
Btw am on the latest Matlab on a Mac Machine.
Thanks a lot!!
Walter Roberson
Walter Roberson le 23 Avr 2017
If you are referring to Vision.VideoFileWriter, then notice this part:
"This object supports only uncompressed RGB24 AVI files on Linux® and Mac platforms."
ceinem
ceinem le 24 Avr 2017
thanks!
keshav poojari
keshav poojari le 20 Déc 2018
I used above code.. but audio will muted from the Video..What can i do?
ceinem
ceinem le 20 Déc 2018
i tried many things back then. It depends a lot on which OS you're working on, what's your input file type and what's your output file type. I think I got the audio working on windows in the end, but I don't remember the filetype I used.

Connectez-vous pour commenter.

Plus de réponses (2)

Seyed Mohammadreza Fatemi
Seyed Mohammadreza Fatemi le 22 Avr 2019

0 votes

You should use vision class from the computer vision toolbox to read and write video files with audio. The only format that you can use is .avi. You can use VideoReader object to obtain the framerate and duration of the video. Here is a code that opens a file and writes the first one tenth frames:
clc
clear all
Inputfilename = 'C:\1.avi';
vid = VideoReader(Inputfilename);
VidFrameRate = vid.FrameRate;
VidDuration = vid.Duration;
NumberofFrames = VidDuration*VidFrameRate;
videoFReader = vision.VideoFileReader(Inputfilename);
videoFReader.AudioOutputPort = 1;
OutputFilename = 'C:\2.avi';
videoFWriter = vision.VideoFileWriter(OutputFilename,'FileFormat','AVI','AudioInputPort',1);
% EOT = 0;
% while(~EOT)
% [Iframe,audio,EOT] = videoFReader();
% videoFWriter(Iframe,audio);
% end
EOT = 0;
for ii=1:NumberofFrames/10
[Iframe,audio,EOT] = videoFReader();
videoFWriter(Iframe,audio);
end
release(videoFReader);
release(videoFWriter);

1 commentaire

Walter Roberson
Walter Roberson le 22 Avr 2019
Computing number of rames as duration times frame rate is always wrong for variable frame-rate files. For fixed rate files, it is common for the calculation to be off by one due to floating point rounding issues. It is also common for the calculation to be off by more than 1 because recorded frame rates are often inaccurate. NTSC is 29.97 frames per second but is often listed as 30 frames per second, which makes a difference of 3 frames per 100 seconds. Some encoders (including, I understand, some used for television work) use an approximate ratio for frame rate instead of a more accurate ratio, which can make a difference over time.
Video editing equipment reads the entire file frame by frame, and uses frame numbers rather than timing for editing.

Connectez-vous pour commenter.

Yiris Lyden
Yiris Lyden le 14 Août 2020

0 votes

How can I make it as I have zero codec technology basis? Though, I don't want to admit. This tool is not so suitable for our beginners. Now, to shorten and cut out scenes from my large video, I use the lossless and fast Joyoshare Media Cutter, my friend asked me to use this one. It's simple and outputs 100% quality. Anyway, it seems better if you are a novice as me..

Community Treasure Hunt

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

Start Hunting!

Translated by