UserAnas
Posts: 26
Joined: Fri Aug 24, 2018 7:03 am

Convert video to bit stream

Fri Aug 24, 2018 7:38 am

I am currently working on VLC (Visible Light Communications) project where I need to transmit a video from one Raspberry Pi 3 B+ to another using an LED. The way this works is, in the first Pi, I need to convert the video to a bit stream, then switch the LED on and off in correspondence with the 1s and 0s, respectively. A photodiode on the other side detects the switching and reconverts the bit stream to a video.

I am still in the first stage, where I need to figure out how to convert an .mp4 or .h264 video to a stream of bits. Any ideas/advice on how to achieve this are highly appreciated. Thanks in advance.

EDIT: I've already managed to solve this. Thanks everyone for the help.
Last edited by UserAnas on Sun Aug 26, 2018 12:37 pm, edited 1 time in total.

User avatar
thagrol
Posts: 1722
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Convert video to bit stream

Fri Aug 24, 2018 12:15 pm

An .mp4 or .h264 video is already a stream of bits as is any digital file.
This space unintentionally left blank.

User avatar
topguy
Posts: 5627
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Convert video to bit stream

Fri Aug 24, 2018 12:25 pm

Your first stage should be to make sure you are able to transfer any bitpattern with the leds, and to figure out how fast you can do it and still receive correct data at the other end.

Any data stream is usually a stream of bytes so you have to send each bit in the byte. The typical would be to send the most significant bit first.
So the byte 0xB3 would be sent as 1,0,1,1,0,0,1,1 for example. This is the easy part of your project ( https://stackoverflow.com/questions/881 ... -in-python )

If you not already know why things like "startbits" "stopbits" and "parity" etc. are quite important in serial communication, I would suggest you also read up on that.

PiGraham
Posts: 3568
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Convert video to bit stream

Fri Aug 24, 2018 1:30 pm

You could look at old file transfer protocols like zmodem (also xmodem, ymodem) that can send files over serial data links (e.g. UART)


If you connect the LED to UART Tx and a photodiode at the other side to Rx (via an amplifier) of a receiving device you may be able to send files over a light-coupled serial connection.

zmodem does error correction that you will need for files of anything more than a few bytes.


You may find that the baud rates you can achieve with ordinary LEDs and photo diodes make this much less than realtime (a 1 minute video may take much more than 1 minute to transmit). I don't know, but a risk to consider.

Maybe you could adapt fibre optic devices to project over short distances using lenses. They have plenty of speed and network protocols to take care of data tranfer and error correction.

User avatar
thagrol
Posts: 1722
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Convert video to bit stream

Fri Aug 24, 2018 6:05 pm

Does it have to be visible light? If you're permitted to switch to infr-red you could use the already existing (but largely obsolete) IRDA standard. I'd expect linux to have drivers and I'd be surprised if there aren't circuits available on the internet for the hardware.
This space unintentionally left blank.

User avatar
topguy
Posts: 5627
Joined: Tue Oct 09, 2012 11:46 am
Location: Trondheim, Norway

Re: Convert video to bit stream

Fri Aug 24, 2018 6:29 pm

Would the circuit or the IrDa drivers even care what frequency of the electromagnetic spectrum the LEDs produce ?

But the IrDa standard maxes out at 115.2 kbps, you could stream decent audio on that but probably not video.

User avatar
mikronauts
Posts: 2710
Joined: Sat Jan 05, 2013 7:28 pm
Contact: Website

Re: Convert video to bit stream

Fri Aug 24, 2018 6:30 pm

Sorry, it won't work - unless you choose a really really low resolution greyscale video.

The GPIO is not fast enough. Simple calculation for 320x240 8 bit greyscale (256 shades):

60fps * 320 * 240 = 4,608,000 bytes per second

Call it 5MBytes after adding sync info (note this does not include any audio)

8 bits per byte = 40Mbits/sec

Good luck toggling an LED at 40M per second.

Forget about reading an input at 40M per second and reconstructing the video.

To be fair, you can cut the above in half by going to 16 level grey scale, and even more by compressing the video.

If you go to 160x120 16 level gray scale you can get it down to about 5Mbps, which may be possible. Cut the frame rate to 20fps, and it ends up being just under 2Mbps, which begins to be doable.

The easy way:

Get some optical transceivers, fiberoptic cable, and use the Ethernet on the Pi's for the transfer.
UserAnas wrote:
Fri Aug 24, 2018 7:38 am
I am currently working on VLC (Visible Light Communications) project where I need to transmit a video from one Raspberry Pi 3 B+ to another using an LED. The way this works is, in the first Pi, I need to convert the video to a bit stream, then switch the LED on and off in correspondence with the 1s and 0s, respectively. A photodiode on the other side detects the switching and reconverts the bit stream to a video.

I am still in the first stage, where I need to figure out how to convert an .mp4 or .h264 video to a stream of bits. Any ideas/advice on how to achieve this are highly appreciated. Thanks in advance.
http://Mikronauts.com - home of EZasPi, RoboPi, Pi Rtc Dio and Pi Jumper @Mikronauts on Twitter
Advanced Robotics, I/O expansion and prototyping boards for the Raspberry Pi

User avatar
thagrol
Posts: 1722
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Convert video to bit stream

Fri Aug 24, 2018 6:56 pm

mikronauts wrote:
Fri Aug 24, 2018 6:30 pm
Sorry, it won't work - unless you choose a really really low resolution greyscale video.
You're assuming the OP wants to do the transfer live/in real time. They haven't said whether or not this is the case. Though I can't argue with your numbers.

If this were my project I'd be looking at low resolution, low colour depth video and doing the decoding at the receiving end rather than on the transmitter.
This space unintentionally left blank.

User avatar
thagrol
Posts: 1722
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Convert video to bit stream

Fri Aug 24, 2018 7:07 pm

topguy wrote:
Fri Aug 24, 2018 6:29 pm
But the IrDa standard maxes out at 115.2 kbps, you could stream decent audio on that but probably not video.
Not according to wikipedia (https://en.wikipedia.org/wiki/Infrared_ ... ifications) 1Gbit/s is possible, though I can't imagine that being cheap to impliment. Especially as it'l cost you $100 for the spec.
This space unintentionally left blank.

User avatar
OutoftheBOTS
Posts: 711
Joined: Tue Aug 01, 2017 10:06 am

Re: Convert video to bit stream

Fri Aug 24, 2018 8:47 pm

I assume this your using an LED and photo-resistor because it is for a school project or interest project to simulate something like how optic fiber works.

To simply send a file as a styream of bits os quite easy.

1. open the file as a read binary file
2. read 1 byte at a time
3. peel off 1 bit at a time of each byte and send it to the gpio pin (something like how a shift register works)

Code could look something like this:

Code: Select all

video_file = open("video.mp4", "rb") #open file for read in biniary


while True:
  byte = video_file.read(1) #read 1 byte
  if byte == b'' : break #check for end of file
  for i in range(8):
  	GPIO.output(LED_Pin, (byte & 1)) #update status for LED to last bit_length
	byte = byte >> 1 #shift byte 1 bit to the right
	time.sleep(baud_delay)
	 
video_file.close()
I would also suggest adding a clock line to sync the transmission of data(synchronous serial transmission) as this is much easier than asynchronous.

User avatar
OutoftheBOTS
Posts: 711
Joined: Tue Aug 01, 2017 10:06 am

Re: Convert video to bit stream

Fri Aug 24, 2018 9:08 pm

You could also look at how an IR remote works as this is exactly what it does : send data (stream of bits) via an IR LED to an IR photo-diode at the receiver.

IR remote are a little more complex as the use a 38khz flashing IR LED to send the signal and the receiver and the reciver rejects reading and IR light that isn't flashing at 38KHz

You can also buy off the shelf cheap UART to serial IR links that convert your Uart serial into flashing IR LED. I have never used 1 but have seen them see https://www.ebay.com/p/5v-IR-Infrared-R ... 0005.m1851

User avatar
thagrol
Posts: 1722
Joined: Fri Jan 13, 2012 4:41 pm
Location: Darkest Somerset, UK
Contact: Website

Re: Convert video to bit stream

Fri Aug 24, 2018 11:28 pm

OutoftheBOTS wrote:
Fri Aug 24, 2018 9:08 pm
You can also buy off the shelf cheap UART to serial IR links that convert your Uart serial into flashing IR LED. I have never used 1 but have seen them see https://www.ebay.com/p/5v-IR-Infrared-R ... 0005.m1851
Don't use that one it's a 5 volt device so will damage your Pi when used to recieve signals.
This space unintentionally left blank.

User avatar
OutoftheBOTS
Posts: 711
Joined: Tue Aug 01, 2017 10:06 am

Re: Convert video to bit stream

Sat Aug 25, 2018 2:06 am

thagrol wrote:
Fri Aug 24, 2018 11:28 pm
OutoftheBOTS wrote:
Fri Aug 24, 2018 9:08 pm
You can also buy off the shelf cheap UART to serial IR links that convert your Uart serial into flashing IR LED. I have never used 1 but have seen them see https://www.ebay.com/p/5v-IR-Infrared-R ... 0005.m1851
Don't use that one it's a 5 volt device so will damage your Pi when used to recieve signals.
it is something to look at fro ideas and looking for a solution to the problem.

If you used this module a logic level shifter will easily shift it from 5v to 3v here they r selling 5 sets of level shifter with 5 channels on each for a $1.29USD see https://www.ebay.com/itm/5-Set-4-Channe ... SwP4FaS02h

UserAnas
Posts: 26
Joined: Fri Aug 24, 2018 7:03 am

Re: Convert video to bit stream

Sat Aug 25, 2018 2:16 am

thagrol wrote:
Fri Aug 24, 2018 12:15 pm
An .mp4 or .h264 video is already a stream of bits as is any digital file.
Yes, I'm aware. I meant how do I actually extract the bit stream from the digital file using Python?
Sorry I wasn't clear.

UserAnas
Posts: 26
Joined: Fri Aug 24, 2018 7:03 am

Re: Convert video to bit stream

Sat Aug 25, 2018 2:21 am

topguy wrote:
Fri Aug 24, 2018 12:25 pm
Your first stage should be to make sure you are able to transfer any bitpattern with the leds, and to figure out how fast you can do it and still receive correct data at the other end.

Any data stream is usually a stream of bytes so you have to send each bit in the byte. The typical would be to send the most significant bit first.
So the byte 0xB3 would be sent as 1,0,1,1,0,0,1,1 for example. This is the easy part of your project ( https://stackoverflow.com/questions/881 ... -in-python )

If you not already know why things like "startbits" "stopbits" and "parity" etc. are quite important in serial communication, I would suggest you also read up on that.
The thing is, I can't really do that yet, as I don't have my LEDs yet. In the meantime though, I was hoping I could start figuring out how to extract a bit stream from a video.

UserAnas
Posts: 26
Joined: Fri Aug 24, 2018 7:03 am

Re: Convert video to bit stream

Sat Aug 25, 2018 2:23 am

thagrol wrote:
Fri Aug 24, 2018 6:56 pm
mikronauts wrote:
Fri Aug 24, 2018 6:30 pm
Sorry, it won't work - unless you choose a really really low resolution greyscale video.
You're assuming the OP wants to do the transfer live/in real time. They haven't said whether or not this is the case. Though I can't argue with your numbers.

If this were my project I'd be looking at low resolution, low colour depth video and doing the decoding at the receiving end rather than on the transmitter.
Yep, not real time transfer. It can take as long as it needs (for a start, at least). The idea is to be able to achieve the transfer.

UserAnas
Posts: 26
Joined: Fri Aug 24, 2018 7:03 am

Re: Convert video to bit stream

Sat Aug 25, 2018 2:24 am

thagrol wrote:
Fri Aug 24, 2018 6:05 pm
Does it have to be visible light? If you're permitted to switch to infr-red you could use the already existing (but largely obsolete) IRDA standard. I'd expect linux to have drivers and I'd be surprised if there aren't circuits available on the internet for the hardware.
Yep, it has to be visible light. :/

UserAnas
Posts: 26
Joined: Fri Aug 24, 2018 7:03 am

Re: Convert video to bit stream

Sat Aug 25, 2018 2:26 am

OutoftheBOTS wrote:
Fri Aug 24, 2018 8:47 pm
I assume this your using an LED and photo-resistor because it is for a school project or interest project to simulate something like how optic fiber works.

To simply send a file as a styream of bits os quite easy.

1. open the file as a read binary file
2. read 1 byte at a time
3. peel off 1 bit at a time of each byte and send it to the gpio pin (something like how a shift register works)

Code could look something like this:

Code: Select all

video_file = open("video.mp4", "rb") #open file for read in biniary


while True:
  byte = video_file.read(1) #read 1 byte
  if byte == b'' : break #check for end of file
  for i in range(8):
  	GPIO.output(LED_Pin, (byte & 1)) #update status for LED to last bit_length
	byte = byte >> 1 #shift byte 1 bit to the right
	time.sleep(baud_delay)
	 
video_file.close()
I would also suggest adding a clock line to sync the transmission of data(synchronous serial transmission) as this is much easier than asynchronous.
Thanks! I'll try this out and report back.

PiGraham
Posts: 3568
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Convert video to bit stream

Sat Aug 25, 2018 8:58 am

UserAnas wrote:
Sat Aug 25, 2018 2:21 am

The thing is, I can't really do that yet, as I don't have my LEDs yet. In the meantime though, I was hoping I could start figuring out how to extract a bit stream from a video.
As I wrote earlier, look at zmodem or any serial comms file transmission. Use pralell to serial hardware like the UART or maybe SPI or i2c port to send locks of bytes.

You can read a file byte by byte and use bit shift functions to serialise it and then wiggle a gpio but that will be slow and unnecessary.

What is your objective, to code parallel to serial conversion, to implement file transfer protocol on a serial link or to send video files over an optical link. I assume it's the last one. User the UART and zmodem to start with. You can try that out with direct electrical connection initially then connect to LEDs / photosensor.
You will need error correction and retry functionality and you really don't need to write that yourself.

Maybe check out serialXfer, modem or any of a load of libraries for ancient serial communications that are available for Python.

unix77
Posts: 7
Joined: Wed Oct 17, 2018 11:03 am

Sat Jan 05, 2019 3:26 pm

Hello UserAnas, can you please share what worked for you?
Last edited by unix77 on Mon Jan 07, 2019 3:10 pm, edited 1 time in total.

unix77
Posts: 7
Joined: Wed Oct 17, 2018 11:03 am

Re: Convert video to bit stream

Mon Jan 07, 2019 11:32 am

UserAnas wrote:
Fri Aug 24, 2018 7:38 am
I am currently working on VLC (Visible Light Communications) project where I need to transmit a video from one Raspberry Pi 3 B+ to another using an LED. The way this works is, in the first Pi, I need to convert the video to a bit stream, then switch the LED on and off in correspondence with the 1s and 0s, respectively. A photodiode on the other side detects the switching and reconverts the bit stream to a video.

I am still in the first stage, where I need to figure out how to convert an .mp4 or .h264 video to a stream of bits. Any ideas/advice on how to achieve this are highly appreciated. Thanks in advance.

EDIT: I've already managed to solve this. Thanks everyone for the help.
Please do reply if you can help in anyway with code or something. Thanks and regards.

UserAnas
Posts: 26
Joined: Fri Aug 24, 2018 7:03 am

Re: Convert video to bit stream

Sat Jan 12, 2019 1:22 pm

unix77 wrote:
Mon Jan 07, 2019 11:32 am
UserAnas wrote:
Fri Aug 24, 2018 7:38 am
I am currently working on VLC (Visible Light Communications) project where I need to transmit a video from one Raspberry Pi 3 B+ to another using an LED. The way this works is, in the first Pi, I need to convert the video to a bit stream, then switch the LED on and off in correspondence with the 1s and 0s, respectively. A photodiode on the other side detects the switching and reconverts the bit stream to a video.

I am still in the first stage, where I need to figure out how to convert an .mp4 or .h264 video to a stream of bits. Any ideas/advice on how to achieve this are highly appreciated. Thanks in advance.

EDIT: I've already managed to solve this. Thanks everyone for the help.
Please do reply if you can help in anyway with code or something. Thanks and regards.
Sorry for the late reply. If you still didn't figure it out, try this code:

Code: Select all

fvid = open("video.mp4", "wb+")
binary = ""

unicodedata = fvid.read()

for byte in unicodedata:
                byte =  bin(ord(byte))
                byte = byte[2:len(byte)]
                padding = "0" * (8 - len(byte))
                binary += padding + byte
Change "video.mp4" to the name of your video file. The "binary" string should then have the entire bit stream.
(This isn't exactly my code, I removed all the extra stuff that is of no use to you, so let me know if it doesn't work and I'll check if I missed something.)

Return to “Python”