简体   繁体   中英

With Python, how can I convert a flac stream to a wav stream?

I know that I can use PyAudio to convert a .flac file to a .wav file. But I'm wondering if I can somehow do it as a stream and not have to save the .wav file?

Currently, I have:

stream = open('84-121123-0000.flac', 'rb')

But I want to convert that stream to a wav file. Any help would be greatly appreciated. Just to be clear, I don't want to save a .wav file. Instead, I want to keep a stream of the wav converted content.

You can do this with pydub without needing to save a file, by using io.BytesIO :

import io
from pydub import AudioSegment


flac = AudioSegment.from_file('/path/to/84-121123-0000.flac', format='flac')
stream = io.BytesIO()
flac.export(stream, format='wav')

You can use the pydub lib that it's easy to do the function what you need:

from pathlib import PurePath
from pydub import AudioSegment

file_path = PurePath("test.flac")

flac_tmp_audio_data = AudioSegment.from_file(file_path, file_path.suffix[1:])

flac_tmp_audio_data.export(file_path.name.replace(file_path.suffix, "") + ".wav", format="wav")

The Documentation .

In Linux, you can install ffmpeg :

sudo apt update
sudo apt install ffmpeg

In Windows: download ffmpeg at: FFMPEG Download , set up environment variables at Edit the system environment variables , Path , New , C:\ffmpeg\bin\

Then run in Python:

import os
os.system('ffmpeg -i inputfile.flac output.wav')

You can use this output as a temp file, with a 3-5 seconds delay.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM