Cut out scenes from a video
Show older comments
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);
Accepted Answer
More Answers (2)
Seyed Mohammadreza Fatemi
on 22 Apr 2019
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 Comment
Walter Roberson
on 22 Apr 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.
Yiris Lyden
on 14 Aug 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..
Categories
Find more on Audio and Video Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!