MATLAB Answers

Joel
0

How to convert a video file to a bit stream in matlab?

Asked by Joel
on 9 Sep 2013
Latest activity Answered by Florian Enner on 14 May 2016
I'm trying to extract the motion vectors from a h.264 coded video. For this, I need to convert the video to its a respective bit stream first. Any idea or lines of code to do this will be appreciated.

  2 Comments

Is the h.264 video "live" or is it in a file? MATLAB does not currently have any facilities to read streaming h.264 but it can decode it from a file.
When you say "its respective bitstream", do you mean a sequence of fully-decoded image frames, or do you mean the encoded h.264 ?
The h.264 video is in a file. And its an encoded h.264 mp4 file. Is it possible to convert it to its bit stream??

Sign in to comment.

3 Answers

Answer by Walter Roberson
on 11 Sep 2013

If you want the output to be the bits corresponding to the encoded file, without doing any decoding along the way, then
fid = fopen('YourFileNameHere.h264', 'r');
bytelist = fread(fid, '*uint8');
fclose(fid);
bitexpanded = dec2bin(bytelist(:), 8) - '0';
bitstream = reshape( bitexpanded.', [], 1);

  0 Comments

Sign in to comment.


Answer by Jan
on 11 Sep 2013

What exactly is a "bit stream" in your case. Note that reading the file in any format is a kind of bit stream already, because any file access is a stream of bits.

  2 Comments

Actually I'm not very clear on that. I need to extract the motion vectors from the video, and since in h.264 encoding the motion vectors are already extracted, I figured I can extract them by referring to the h.264 syntax. So I'm trying to read the file encoded in h.264 in its bit format and then extract itsmotion vectors.
Use fread() and fseek() to read your file byte by byte (or skip places you do not need to read) until you get to the location that you need the bit structure of.
A lot of the time working at the byte level does fine, but if you have a uint8 that you read in you can use bitget() and bitand() and bitshift to extract particular bits. Or you can dec2bin() the byte and work on that.
You can also use a bit-level fread() -- read about the "precision" option for fread()

Sign in to comment.


Answer by Florian Enner on 14 May 2016

I've uploaded a submission that supports streaming h264 (among other formats) from ip cameras.
% Connect to stream
cam = HebiCam('<address>');
% Continously display latest image
figure();
fig = imshow(getsnapshot(cam));
while true
set(fig, 'CData', getsnapshot(cam));
drawnow;
end
Let me know if this works for you.

  0 Comments

Sign in to comment.