How to convert a video file to a bit stream in matlab?
14 views (last 30 days)
Show older comments
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
Walter Roberson
on 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 ?
Answers (3)
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);
4 Comments
Hudson Romualdo
on 23 Nov 2022
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
on 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
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
Walter Roberson
on 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()
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
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!