How Do I Decode/Encode A Video To A Text File and then Back To Video?
Question:
I want to take a video – take the video contents – and turn it into base64. Then, I want to take that text file – decode it from base64 – and then turn it back into a video.
Currently, I have been able to turn the video into a text file, but when I try to convert it back into a video I get an empty text file instead of a video file.
How do I fix this?
import base64
with open("al.mp4", "rb") as videoFile:
text = base64.b64encode(videoFile.read())
print(text)
file = open("textTest.txt", "wb")
file.write(text)
file.close()
fh = open("video.mp4", "wb")
fh.write(base64.b64decode(str))
fh.close()
Answers:
import base64
with open("al.mp4", "rb") as videoFile:
text = base64.b64encode(videoFile.read())
print(text)
file = open("textTest.txt", "wb")
file.write(text)
file.close()
fh = open("video.mp4", "wb")
fh.write(base64.b64decode(text))
fh.close()
This is the code that works.
You were trying to write str
to the file. Now str
in python is the name of the string class. You can do something like str = "assda"
but that is not recommended. And furthermore, str
is not the stuff you just read from the file. That is text
. Just write text
and you’re good.
I want to take a video – take the video contents – and turn it into base64. Then, I want to take that text file – decode it from base64 – and then turn it back into a video.
Currently, I have been able to turn the video into a text file, but when I try to convert it back into a video I get an empty text file instead of a video file.
How do I fix this?
import base64
with open("al.mp4", "rb") as videoFile:
text = base64.b64encode(videoFile.read())
print(text)
file = open("textTest.txt", "wb")
file.write(text)
file.close()
fh = open("video.mp4", "wb")
fh.write(base64.b64decode(str))
fh.close()
import base64
with open("al.mp4", "rb") as videoFile:
text = base64.b64encode(videoFile.read())
print(text)
file = open("textTest.txt", "wb")
file.write(text)
file.close()
fh = open("video.mp4", "wb")
fh.write(base64.b64decode(text))
fh.close()
This is the code that works.
You were trying to write str
to the file. Now str
in python is the name of the string class. You can do something like str = "assda"
but that is not recommended. And furthermore, str
is not the stuff you just read from the file. That is text
. Just write text
and you’re good.