Shamoon
Shamoon

Reputation: 43531

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.

Upvotes: 0

Views: 11276

Answers (3)

Adam Liter
Adam Liter

Reputation: 925

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')

Upvotes: 4

razimbres
razimbres

Reputation: 5015

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.

Upvotes: 2

DustyPosa
DustyPosa

Reputation: 463

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.

Upvotes: 3

Related Questions