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

14 Ansichten (letzte 30 Tage)
Joel
Joel am 9 Sep. 2013
Kommentiert: Walter Roberson am 23 Nov. 2022
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 Kommentare
Walter Roberson
Walter Roberson am 9 Sep. 2013
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 ?
Joel
Joel am 11 Sep. 2013
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??

Melden Sie sich an, um zu kommentieren.

Antworten (3)

Walter Roberson
Walter Roberson am 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);
  4 Kommentare
Hudson Romualdo
Hudson Romualdo am 23 Nov. 2022
Thanks for sharring @Walter Roberson.
But calculating the number of bits by hand I'm getting a diferente number of bits than bitstream variable.
I'm using this code to get the bitstream for this video: 3DShootGame 1 second cut
It's a 1 second cut of a video with 30 frames per second, each frame it's a 1280 x 720 pixel image and each pixel it's formed by 24 bits. Total = 663.552.000 bits
But when I execute the code the size of bitstrem is only 4.491.104 bits.
Do you know what I'm doing wrong?
Walter Roberson
Walter Roberson am 23 Nov. 2022
The code takes the bitstream of the file. The file contains a compressed representation of the video. It might potentially also contain additional information such as copyright or audio or subtitles.
Sending the bitstream of the file is appropriate for the situation where you were planning to write the transferred video to storage anyhow, but is not necessarily appropriate for broadcast work. There is a slightly different video encoding that is used for broadcast work

Melden Sie sich an, um zu kommentieren.


Jan
Jan am 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 Kommentare
Joel
Joel am 13 Sep. 2013
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.
Walter Roberson
Walter Roberson am 13 Sep. 2013
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()

Melden Sie sich an, um zu kommentieren.


Florian Enner
Florian Enner am 14 Mai 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.

Community Treasure Hunt

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

Start Hunting!

Translated by