I am merging videos in python.(ffmpeg,moviepy)
But it is very slow.
So, I am trying to encode a 1.mp4 file and a 2.mp4 file using base64 and combine them.
I have the code below.
import base64
with open('1.mp4', "rb") as videoFile:
text = base64.b64encode(videoFile.read())
with open("2.mp4", "rb") as videoFile:
texts = base64.b64encode(videoFile.read())
fh = open("dfghhffdssdf.mp4", "wb")
fh.write(base64.b64decode(text+texts))
fh.close()
I tried running the code and the video didn't merge.
So I created a new code like below.
import base64
with open('1.mp4', "rb") as videoFile:
text = base64.b64encode(videoFile.read())
with open("2.mp4", "rb") as videoFile:
texts = base64.b64encode(videoFile.read())
text = str(text).replace("=","") + str(texts)
fh = open("dfghhffdssdf.mp4", "wb")
fh.write(base64.b64decode(text+texts))
fh.close()
Then, the following error is displayed.
can only concatenate str (not "bytes") to str
Therefore, if you replace the "text" variable with bytes with the bytes function, you will get the following error:
string argument without an encoding
What should I do?
If that's not possible, please tell me how to quickly merge video files.
Related
I have audio recordings saved on a server as base64, webm format and want to decode them with python into a wav file. I tried both suggested ways from a simliar question found here: How to decode base64 String directly to binary audio format. But I'm facing different problems with both suggestions:
The version using file.write resulted in a wav file that I could play with the VLC player and which included the expected content. But I got an error message when I tried to read it with matlab or python saying "unknown format" or "missing riff".
fin = open(dirName + file, "r")
b64_str = fin.read()
fin.close()
# decode base64 string to original binary sound object
decodedData = base64.b64decode(b64_str)
webmfile = (outdir + file.split('.')[0] + ".webm")
wavfile = (outdir + file.split('.')[0] + ".wav")
with open(webmfile , 'wb') as wm:
wm.write(decodedData)
with open(webmfile, 'rb') as wm:
webmdata = pcm.read()
with open(wavfile, 'wb') as file:
file.write(webmdata)
The version using writeframes with setting the parameters result in a file I could read with matlab or python but this one does not contain the expected content and is way shorter than expected.
with wave.open(wavfile, 'wb') as wav:
wav.setparams((1, 2, 48000, 0, 'NONE', 'NONE'))
wav.writeframes(webmdata)
Any ideas on how to solve this problem? The file itself is fine. Converting it with an online converter worked.
In case someone has the same problem at some point, here is the solution which worked for me:
The following code creates a webm file from the base64 str:
import base64
decodedData = base64.b64decode(b64_str)
webmfile = (outdir + file.split('.')[0] + ".webm")
with open(webmfile, 'wb') as file:
file.write(decodedData)
And for the conversion I used ffmpy:
from ffmpy import FFmpeg
ff = FFmpeg(
executable = 'C:/Program Files/ffmpeg-2020/bin/ffmpeg.exe',
inputs={file:None},
outputs = {outfile:'-c:a pcm_f32le'})
ff.cmd
ff.run()
After those two steps, I was able to read the resulting wav file with matlab or any other program.
I am extracting text from a Pdf file and I wanted to get a formatted output of the content. As the Object is made of a list sentences I thought that using textwrap.wrap would have had the job to work on the joined content.
I have tried with
for file in listoffiles
with open(file, "rb") as f:
pdf = pdftotext.PDF(f)
with open("filename" + ".txt") , 'w', encoding = 'utf-8') as f:
f.write(textwrap.wrap("\n\n".join(pdf), width = 70, ))
f.close()
I have also tried with {:<}".format("\n\n".join(pdf) instead of textwrap but it gives me back the dame type of result.
Is there any way to pass a clean file to the wrapper?
I have recorded one audio file and I am converting that file into base64 format.Now I want to write this audio file into a fifo file.
Code is written below:
import os
import base64
import select
os.system("mkfifo audio1.fifo")
with open("audio1.fifo") as fifo:
select.select([fifo],[],[fifo])
with open("out1.wav","rb") as audioFile:
str = base64.b64encode(audioFile.read())
fifo.write(str)
But above code only creating fifo file but not writing anything in it.Plz give any suggestions.
Use + model can at the same time support read and write
import base64
import select
os.system("mkfifo audio1.fifo")
with open("audio1.fifo", "wb") as fifo:
select.select([fifo],[],[fifo])
with open("out1.wav","rb") as audioFile:
str = base64.b64encode(audioFile.read())
fifo.write(str)
The same time read and write two different files:
# maker sure the be read file's has some data.
with open("a.file", "w") as fp:
fp.write("some data")
# now a.file has some data, we create a b.file for write the a.file's data.
with open("b.file", "w") as write_fp, open("a.file", "r") as read_fp:
write_fp.write(read_fp.read())
# for bytes data
with open("b.file", "wb") as write_fp, open("a.file", "rb") as read_fp:
write_fp.write(read_fp.read())
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.
So i'm trying to create a very simple program that opens a file, read the file and convert what is in it from hex to base64 using python3.
I tried this :
file = open("test.txt", "r")
contenu = file.read()
encoded = contenu.decode("hex").encode("base64")
print (encoded)
but I get the error:
AttributeError: 'str' object has no attribute 'decode'
I tried multiple other things but always get the same error.
inside the test.txt is :
4B
if you guys can explain me what I do wrong would be awesome.
Thank you
EDIT:
i should get Sw== as output
This should do the trick. Your code works for Python <= 2.7 but needs updating in later versions.
import base64
file = open("test.txt", "r")
contenu = file.read()
bytes = bytearray.fromhex(contenu)
encoded = base64.b64encode(bytes).decode('ascii')
print(encoded)
you need to encode hex string from file test.txt to bytes-like object using bytes.fromhex() before encoding it to base64.
import base64
with open("test.txt", "r") as file:
content = file.read()
encoded = base64.b64encode(bytes.fromhex(content))
print(encoded)
you should always use with statement for opening your file to automatically close the I/O when finished.
in IDLE:
>>>> import base64
>>>>
>>>> with open('test.txt', 'r') as file:
.... content = file.read()
.... encoded = base64.b64encode(bytes.fromhex(content))
....
>>>> encoded
b'Sw=='