某些原因,我需要将音频数据转换为字节,例如来自http或grpc客户端的数据
所以我想用ffmpeg通过os / exec.Command转换音频
奇怪的是,当我直接转换音频字节会失败,但是用保存文件迷住了,效果很好
下面有两个测试
import requests
from bs4 import BeautifulSoup
url = 'https://www.ntu.edu.sg/events/Pages/default.aspx'
response = requests.get(url)
headers = {'User-Agent','Mozilla/5.0'}
soup = BeautifulSoup(response.content,'lxml')
titles = [item.text.replace('\u200b','') for item in soup.select("[class^='ntu_event_summary_title']")]
print(titles)
第一个测试输出
// ffmpeg work well with file's io.Reader
func TestMp4ToWav(t *testing.T) {
var f io.Reader
var err error
f, err = os.Open("test.m4a")
require.NoError(t, err)
cmdStr := strings.Split("-f mp4 -i /dev/stdin -ar 16000 -ac 1 -f wav -", " ")
cmd := exec.Command("ffmpeg", cmdStr...)
cmd.Stdin = f
var errBuff bytes.Buffer
cmd.Stderr = &errBuff
d, err := cmd.Output()
t.Log(errBuff.String())
if err != nil {
t.Error(err)
}
err = ioutil.WriteFile("output.wav", d, 0644)
assert.NoError(t, err)
}
// but not work with bytes.Buffer
func TestMp4ToWavWithBuff(t *testing.T) {
var data []byte
var err error
data, err = ioutil.ReadFile("test.m4a")
require.NoError(t, err)
cmdStr := strings.Split("-f mp4 -i /dev/stdin -ar 16000 -ac 1 -f wav -", " ")
cmd := exec.Command("ffmpeg", cmdStr...)
cmd.Stdin = bytes.NewBuffer(data)
var errBuff bytes.Buffer
cmd.Stderr = &errBuff
d, err := cmd.Output()
t.Log(errBuff.String())
require.NoError(t, err)
err = ioutil.WriteFile("output.wav", d, 0644)
assert.NoError(t, err)
}
第二次测试输出
ffmpeg_test.go:54: ffmpeg version 2.8.15-0ubuntu0.16.04.1 Copyright (c) 2000-2018 the FFmpeg developers
built with gcc 5.4.0 (Ubuntu 5.4.0-6ubuntu1~16.04.10) 20160609
configuration: --prefix=/usr --extra-version=0ubuntu0.16.04.1 --build-suffix=-ffmpeg --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --cc=cc --cxx=g++ --enable-gpl --enable-shared --disable-stripping --disable-decoder=libopenjpeg --disable-decoder=libschroedinger --enable-avresample --enable-avisynth --enable-gnutls --enable-ladspa --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-librtmp --enable-libschroedinger --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvorbis --enable-libvpx --enable-libwavpack --enable-libwebp --enable-libx265 --enable-libxvid --enable-libzvbi --enable-openal --enable-opengl --enable-x11grab --enable-libdc1394 --enable-libiec61883 --enable-libzmq --enable-frei0r --enable-libx264 --enable-libopencv
libavutil 54. 31.100 / 54. 31.100
libavcodec 56. 60.100 / 56. 60.100
libavformat 56. 40.101 / 56. 40.101
libavdevice 56. 4.100 / 56. 4.100
libavfilter 5. 40.101 / 5. 40.101
libavresample 2. 1. 0 / 2. 1. 0
libswscale 3. 1.101 / 3. 1.101
libswresample 1. 2.101 / 1. 2.101
libpostproc 53. 3.100 / 53. 3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/dev/stdin':
Metadata:
major_brand : M4A
minor_version : 512
compatible_brands: isomiso2
encoder : Lavf56.40.101
Duration: 00:00:05.02, start: 0.023220, bitrate: 136 kb/s
Stream #0:0(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 134 kb/s (default)
Metadata:
handler_name : SoundHandler
Output #0, wav, to 'pipe:':
Metadata:
major_brand : M4A
minor_version : 512
compatible_brands: isomiso2
ISFT : Lavf56.40.101
Stream #0:0(und): Audio: pcm_s16le ([1][0][0][0] / 0x0001), 16000 Hz, mono, s16, 256 kb/s (default)
Metadata:
handler_name : SoundHandler
encoder : Lavc56.60.100 pcm_s16le
Stream mapping:
Stream #0:0 -> #0:0 (aac (native) -> pcm_s16le (native))
size= 158kB time=00:00:05.03 bitrate= 256.1kbits/s
video:0kB audio:157kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.048375%
test.m4a audio download address
PS我也尝试伪造os.File对象仍然无法工作