Reputation: 2075
I'm working on a tensorflow project that learns from an audio stream. I'm trying to open an audio file and store the data in an array using FFMPEG. I'm following the tutorial here
My code looks like this:
import subprocess as sp
FFMPEG_BIN = "ffmpeg"
try:
if image_file != 'train/rock/.DS_Store':
command = [FFMPEG_BIN,
'-i', image_file,
'-f', 's16le',
'-acodec', 'pcm_s16le',
'-ar', '44100',
'-ac', '2',
'output.png']
pipe = sp.Popen(command, stdout=sp.PIPE, bufsize=10**8)
# pipe = sp.Popen(command, stdout=sp.PIPE)
raw_audio = pipe.proc.stdout.read(88200*4)
But I'm getting the error:
AttributeError: 'Popen' object has no attribute 'proc'
Upvotes: 0
Views: 5099
Reputation: 101
I am working with ffmpeg
and pyaudio
. This code works for me.
import pyaudio
import subprocess as sp
import numpy
command = [ 'ffmpeg',
'-i', "Filename", # I used a url stream
'-loglevel','error',
'-f', 's16le',
'-acodec', 'pcm_s16le',
'-ar', '44100', # ouput will have 44100 Hz
'-ac', '2', # stereo (set to '1' for mono)
'-']
pipe = sp.Popen(command, stdout=sp.PIPE, bufsize=10**8)
p = pyaudio.PyAudio() #PyAudio helps to reproduce raw data in pipe.
stream = p.open(format = pyaudio.paInt16,
channels = 2,
rate = 44100,
output = True)
while True:
raw_audio = pipe.stdout.read(44100*2) #get raw data
stream.write(raw_audio) # reproduce
# Convert raw data in array with numpy
audio_array = numpy.fromstring(raw_audio, dtype="int16")
audio_array = audio_array.reshape((len(audio_array)/2,2))
stream.stop_stream()
stream.close()
In ubuntu you can install pyaudio
with:
sudo apt-get install python-pyaudio python3-pyaudio
or
pip install pyaudio
Upvotes: 1
Reputation: 87
As suggested in the comments, this worked for me:
import subprocess as sp
import numpy as np
command = [ 'ffmpeg',
'-i', 'song.mp3',
'-f', 's16le',
'-acodec', 'pcm_s16le',
'-ar', '22050',
'-ac', '1',
'-']
pipe = sp.Popen(command, stdout=sp.PIPE)
stdoutdata = pipe.stdout.read()
audio_array = np.fromstring(stdoutdata, dtype="int16")
I'm not quite sure however why you're trying to convert an "image_file" into a ".png" using audio conversion?
Upvotes: 0