Open In App

How To Add Subtitle To Video in MATLAB?

Improve
Improve
Like Article
Like
Save
Share
Report

MATLAB stands for Matrix Laboratory. It is a high-performance language that is used for technical computing. In this article, we will see how to add subtitles to videos in MATLAB.

The steps to adding subtitles to a video in MatLab are as follows:

  • Observe a video file.
  • In a video file, read the information for each video frame.
  • Each frame can have text added using the syntax “text(x,y, string)”.
  • Imshow can be used to display the image.
  • Take the frame from the image of the person.

Example 1:

Matlab




% Input a Video file
obj=mmreader('Different Sports.avi');
X=read(obj);
i=1;
  
% Prioritize the structure of the frame.
Frame=struct('cdata',1,'colormap',cell([1 100]));
  
% Take a picture of the first frame.
% Add text
% To obtain the figure, use "getframe"
Caption={'How to add Caption/Subtitles';
'to video in MATLAB';};
J=X(:,:,:,1);
imshow(J),hold on
text(70,90,Caption{1},'Color','w',
'FontWeight','Bold','FontSize',30);
text(140,190,Caption{2},'Color','w',
'FontWeight','Bold','FontSize',30);
text(200,400,Caption{3},'Color','w',
'FontWeight','Bold','FontSize',30);
Frame(i:i+9)=getframe;
i=i+10;
  
% Get an image of the second frame. j.eX(:,:,:,2);
J=X(:,:,:,2);
imshow(J), hold on
text(30,110,Title_{1},'Color','k',
'FontWeight','Bold','FontSize',30);
text(300,500,Title_{2},'Color','k',
'FontWeight','Bold','FontSize',30);
Frame(i:i+9)=getframe;
i=i+10;
%Use the text versus to create a cell array.
Txt={'Basketball is a game played between two
teams of five players each on a rectangular court,
usually indoors. Each team tries to score by
tossing the ball through the opponent's goal,
an elevated horizontal hoop and net called a basket';
'The game of football develops in the players
a fair mind, team effort, and competitive spirit. 
It is regarded as one of the most disciplined 
sports in the world. Football is quite popular in all countries';
'Hockey is the national game of India. Also,
it is a popular game and an interesting game that
many countries play. The game has many rules that
need to be followed by each team so that a fair
play can be conducted. There are different varieties 
of this game played across the globe';
'It is easily one of the most prevalent sports
in the world. This game consists of two teams that 
include 11 players each. The main aim of the game
is to score the highest number of runs. It 
is played on a pitch in a field that is well-maintained
for the same purpose. Cricket is 
particularly famous in England and India';};
  
%enlarge the image
%Include the Text lines.
%Seize the frame.
%There are 4 lines totally
%In each frame, alternately add three and two lines.
inc=0;
for j=3:size(X,4)
m=mod(j,2);  
J=X(:,:,:,i);
imshow(J), hold on
for k=1:2+m
text70,400+(k*40),Txt{k+inc},'Color',
'w','FontWeight','Bold','FontSize',30);
end
inc=inc+2+m;
Frame(i:i+19)=getframe;
i=i+20;
end


Output:

Input Video

Make a file for a video:

Matlab




obj=  avifile('Different Sports.avi','FPS',4);
obj= addframe(obj,Frame);
obj=  close(obj);


Output:



Last Updated : 28 Jan, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads