【发布时间】:2021-04-03 12:44:13
【问题描述】:
我有一个函数,它当前接收字节,将其保存到磁盘上的音频 WEBM 文件,然后将其转换为磁盘上的另一个音频 WAV 文件。
我正在寻找一种在不将 WEBM 文件保存到磁盘的情况下使用 FFMPEG 进行上述转换的方法。
FFMPEG 能否使用内存中的字节而不是磁盘中文件的路径来处理此类转换?
我现在在做什么(Python 3.8.8 64Bit):
# audio_data = bytes received
def save_to_webm(audio_data, username):
mainDir = os.path.dirname(__file__)
tempDir = os.path.join(mainDir, 'temp')
webm_path = os.path.join(tempDir, f'{username}.webm')
with open(webm_path, 'wb') as f:
f.write(audio_data)
return webm_path
# webm_path = input path in FFMPEG
def convert_webm_to_wav(webm_path, username):
mainDir = os.path.dirname(__file__)
tempDir = os.path.join(mainDir, 'temp')
outputPath = os.path.join(tempDir, f'{username}.wav')
if platform == 'win32':
ffmpeg_path = os.path.join(mainDir, 'ffmpeg.exe')
else:
os.chdir("/ffmpeg")
ffmpeg_path = './ffmpeg'
command = [ffmpeg_path, '-i', webm_path, '-acodec', 'pcm_s16le', '-ar', '11025', '-ac', '1', '-y', outputPath]
subprocess.run(command,stdout=subprocess.PIPE,stdin=subprocess.PIPE)
return outputPath
【问题讨论】:
标签: python python-3.x audio ffmpeg