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.
Related
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.
I tried to decode the following base64 image
data:image/jpeg;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAAAyCAYAAACqNX6+AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAASwSURBVHhe7VoxaxsxFPYvye5sJZC5JRkcSoZCoMSFDIVOGWpSQ6FDqLN5CqGBEnAIoe7sKTSbF2fqmiWbMWTLkl+gPp2k89P56U7Syc7Z1Qdf6+9OTz70qvednlv78efwNWPsuyLo9HPUi9c1/sf57SH8NUXUL6eThPAPISfliNpPpwnhCDWpQtTumntImhCOEJNiRO2mo4cAqqSjh0hURUcPQaiCjh5SMR09BFAlHT1Eoio6eghCFXT0kIrp6CGAl9DDdo3VagThfkkPeWBntRP21okX7Oja9/sE3PSEXe7U2e/19Vl2RsmIsN9n0kPWppKACaPKechwQCx4ARsj9qjiEdz1R3bfnF3oO7HGcP89u6sbEpEhjyn/PGZt3BFZlvWQ4z1iwQvY6j3JaPfvw3rSIRa7vgX7QWHEriwTwjmfpBywXoNYeBMhpoSH4HI1YH/lVf/5BAr16Ra5oAmhBE3Hj8QO2dllz/IKR3p/1NFjm/1knPPzGPU4JxltKGAC57fvpqUMtL+H4HK190XeEfCaD0HT2YUzsd5k93J7WM+P56530t1lHS9BaWOZag9nxp+01sQ9+OztIbhcnUG6XeNtNVmaKJp2ggStJ1Mfqm+mCeWwi59C08P2bCIS4p1BjPf3kEWVK3tjvulPPOZHLwawwy77rvGUNpeq7a4hXiUEPvp5yCqUqwTI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ZtaI6nBI3ofrbh4ikacfexfp7gjVai9VrubqGQLO2nR4hHuBPUQvV/zQqOA/f4lypTUPA75NAXy10WM23oT3kG7rWNsd3ZLzJdq3XBGdXKvvQ3DTw8QTGr2x1Pr9fLMXbRUrD+GLbGPMWqkC7xjI5yqav0h7lyvbPhgmzOvdYDR2eIu4xlQOLTzkiQ0aapGB7YfkqjYeXnP300QIfjJ1NSVM2rrVTjHbi/JMiK/H+L3u6g3HYg/JnClsqHyDnA9hRvd3ydJky5leVN4viybavigAdC3KFb3oNKneVqGHaJ5QxJKe8dxv0otkw4V4RI52KVfGrq/L/8vKaSiGaSAa3qRsGah1zuGjC8tVo8eU1efNZ+EhAsumyaQ5MPTz2OpiD0FYJk0tsgs5Qj4Ph43O9RCqPPlSwechMVZd53oItbC+xHB9yP9Jr6yHLKteWQ/hWEY9p99DpojaTed6iELUi9PRQySqoqOHIFRBRw+pmI4eAqiOZuwfl74j6FwpdBsAAAAASUVORK5CYII=""")
print(base64.b64decode(x))
also with using encode("ascii") for x:
import base64
x = """iVBORw0KGgoAAAANSUhEUgAAAGQAAAAyCAYAAACqNX6+AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAASwSURBVHhe7VoxaxsxFPYvye5sJZC5JRkcSoZCoMSFDIVOGWpSQ6FDqLN5CqGBEnAIoe7sKTSbF2fqmiWbMWTLkl+gPp2k89P56U7Syc7Z1Qdf6+9OTz70qvednlv78efwNWPsuyLo9HPUi9c1/sf57SH8NUXUL6eThPAPISfliNpPpwnhCDWpQtTumntImhCOEJNiRO2mo4cAqqSjh0hURUcPQaiCjh5SMR09BFAlHT1Eoio6eghCFXT0kIrp6CGAl9DDdo3VagThfkkPeWBntRP21okX7Oja9/sE3PSEXe7U2e/19Vl2RsmIsN9n0kPWppKACaPKechwQCx4ARsj9qjiEdz1R3bfnF3oO7HGcP89u6sbEpEhjyn/PGZt3BFZlvWQ4z1iwQvY6j3JaPf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PTz2OpiD0FYJk0tsgs5Qj4Ph43O9RCqPPlSwechMVZd53oItbC+xHB9yP9Jr6yHLKteWQ/hWEY9p99DpojaTed6iELUi9PRQySqoqOHIFRBRw+pmI4eAqiOZuwfl74j6FwpdBsAAAAASUVORK5CYII=""".encode("ascii")
print(base64.b64decode(x))
I saved the result as PNG image and JPG.
the issue here that the files didn't open, and I dont understand how the whole thing works. I watched some tutorials and I failed to display the image on my computer.
try to write to the image file the binary of this string like this
f = open('img.png', 'wb')
f.write(base64.b64decode(x))
f.close()
You could do as follows:
import base64
image_64_decode = base64.decodestring(x)
image_result = open('filename.png', 'wb') # create a writable image and write the decoding result
image_result.write(image_64_decode)
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.
I am getting this error message:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte
From this code:
from PIL import Image
import os
image_name = "mypic"
count = 0
for filename in os.listdir('pictures'):
if filename.endswith('.jpg'):
image_file = open('pictures/' +filename)
image = Image.open(image_file)
# next 3 lines strip exif
data = list(image.getdata())
image_without_exif = Image.new(image.mode, image.size)
image_without_exif.putdata(data)
image_without_exif.save('new-pictures/' + image_name + str(count) + '.jpg')
count = count + 1;
Not sure why, as this was working yesterday...
I think you need to open the file in binary mode:
image_file = open('pictures/' +filename, 'rb')
This happens because open is trying to read the file as text. You can resolve this by opening the path directly with Image.open()
img = Image.open('pictures/' + filename)
This works because PIL does the related handling for you internally; take a look at its documentation here for more!
https://pillow.readthedocs.io/en/latest/reference/Image.html#PIL.Image.open
Further, it probably makes even more sense to use Image.open as a context manager to handle opening and closing your image when done (there's a good explanation here)
with Image.open('pictures/' + filename) as img:
# process img
# image file closed now after leaving context scope
When using the open(filename) function without any further arguments, you open the file in "text" mode.
Python will assume that the file contains text when reading it. When it finds a byte with the value of 255 (0xFF), it is confused because no text character matches that byte.
To fix this, open the file in bytes mode:
open(filename, "b")
This tells python to not assume it contains text and the file handle will just give out the raw bytes instead.
Because this is a common use-case, PIL already has opening images by filename built in:
Image.open(filename)
I'm using AWS Lex to generate a response to my sound (http://boto3.readthedocs.io/en/latest/reference/services/lex-runtime.html).
The response audioStream is a StreamingBody object from boto3 (https://botocore.readthedocs.io/en/latest/reference/response.html#botocore.response.StreamingBody).
The question is how can I turn the returned byte array to a wav file that I can play with sox?
I have tried the following:
audio_stream = response['audioStream'].read()
f = open('response.wav', 'wb')
f.write(audio_stream)
f.close()
But then I get an error with sox and aplay that the format is invalid (RIFF header was not found)
I also tried to use the wave library with the following code
audio_stream = response['audioStream'].read()
f = wave.open('response.wav', 'wb')
f.setnchannels(1)
f.setsampwidth(2)
f.setframerate(16000)
f.writeframesraw(audio_stream)
f.close()
But then I only get white noise when I play the file and the length is very short.
The answer was to close the stream before writing it to the file. The working code looks like this:
audio_stream = response['audioStream'].read()
response['audioStream'].close()
f = wave.open(self.response_fname, 'wb')
f.setnchannels(2)
f.setsampwidth(2)
f.setframerate(16000)
f.setnframes(0)
f.writeframesraw(audio_stream)
f.close()