Change data in intra frame of MPEG file in matlab

367 views Asked by At

I need to read a MPEG video in matlab, and change the data in the Intra frame and save it again as a new MPEG file. Can someone please suggest me a way of doing this? Is this a possible task or is this impossible to do? Please help me.. Thanks in advance

I have read a mpeg file in matlab and I separated it into frames by the below code. can you please tell me how to identify the Intra frames? I'm going to change the pixel values of that frame and I have to make a new mpeg file with those frames.

%%Extracting & Saving of frames from a Video file through Matlab Code%%
clc;
close all;
clear all;

mov = VideoReader('song.mpg');
opFolder = fullfile(cd, 'pics');
%if  not existing 
if ~exist(opFolder, 'dir')
  %make directory & execute as indicated in opfolder variable
  mkdir(opFolder);
end

%getting no of frames
numFrames = mov.NumberOfFrames;

%setting current status of number of frames written to zero
numFramesWritten = 0;

%for loop to traverse & process from frame '1' to 'last' frames 
for t = 1 : numFrames
  currFrame = read(mov, t);    %reading individual frames
  opBaseFileName = sprintf('%3.3d.png', t);
  opFullFileName = fullfile(opFolder, opBaseFileName);
  imwrite(currFrame, opFullFileName, 'png');   %saving as 'png' file

  %indicating the current progress of the file/frame written
  progIndication = sprintf('Wrote frame %4d of %d.', t, numFrames);
  disp(progIndication);
  numFramesWritten = numFramesWritten + 1;
end      %end of 'for' loop

progIndication = sprintf('Wrote %d frames to folder "%s"',numFramesWritten, opFolder);
disp(progIndication);
1

There are 1 answers

0
Dinesh Iyer On

I am assuming that by Intra-frame you mean I-frames. There is no way to determine whether a frame is an I, P or B-frame after it has been decoded. One has to look at the raw encoded bitstream to do this. A tool such as ffprobe can help you with this.

You can do this round-about thing if you really want to:

  1. Download ffmpeg tools. It has a function or utility called ffprobe. On the system terminal, execute:

    ffprobe -show_frames -select_streams v:0 song.mpg

  2. This will generate a text file with information about each frame. You can do some clever text processing on this file using TEXTSCAN and look for PICT_TYPE=I and identify which frames are I-frames.

  3. Use these indices to read frames using VIDEOREADER.

  4. Write out these frames using VIDEOWRITER.

Hope this helps.