I have a situation where a wav
blob will be posted to my django project and I will have to convert it to an mp3. I have the process down, but it seems a little verbose and I would like to cut it down a bit but IDK how to go about doing it.
wav_path = os.path.join(full_path, "%s%s.wav" % (word.name, file_number))
mp3_path = wav_path[:-3] + "mp3"
with open(wav_path, 'w') as f:
f.write(request.FILES['audio_file'].read())
os.popen("lame %s --comp 40 %s" % (wav_path, mp3_path))
os.popen("rm %s" % wav_path)
lame says it can take something from stdin instead of a named file with -
so I tried this
lame - --comp 40 outfile.m3p << infile.wav
something infile.wav | lame - --comp 40 outfile.mp3
Sorry for adding something
there but I am unsure of what to use in that spot. It seems like writing the blob to a file > converting it and then deleting the blob can be shortened up
You could replace 'something' with 'cat' in your example.
cat infile.wav | lame - --comp 40 outfile.mp3
But in case you already have the blob in python, you could directly pipe it via stdin, instead of going the detour of writing it to a .wav file. See How do I write to a Python subprocess' stdin?