fluent-ffmpeg 使用流媒体提取音频
fluent-ffmpeg extract audio using streaming
我正在尝试从视频中提取音频。此代码运行良好:
ffmpeg('1.mp4').output('1.mp3')
.noVideo()
.format('mp3')
.outputOptions('-ab','192k')
.run();
但是如果我用这样的流读取文件:
var video = fs.createReadStream('1.mp4');
var audio = fs.createWriteStream('1.mp3');
ffmpeg(video).output(audio)
.noVideo()
.format('mp3')
.outputOptions('-ab','192k')
.run();
输出文件重 1KB,里面没有任何东西。
如何使用流提取音频?
假设您正在使用 ffmpeg 包,它基本上是节点中 运行 ffmpeg 的包装器,您必须像这样进行操作:
此代码在我的系统上运行,使用您在上面评论中提供的相同文件,但您可能需要进行一些更改(节点 v8.12.0、ffmpeg v0.0.4)。
const ffmpeg = require('ffmpeg');
try {
let process = new ffmpeg('3.mp4');
process.then(function (video) {
// Callback mode
video.fnExtractSoundToMP3('3.mp3', function (error, file) {
if (!error)
console.log('Audio file: ' + file);
});
}, function (err) {
console.log('Error: ' + err);
});
} catch (e) {
console.log(e.code);
console.log(e.msg);
}
为了进一步调查,我在转码时为不同类型的错误以及其他事件添加了回调。第一个提示是 progress
事件的以下回调:
{ frames: NaN,
currentFps: NaN,
currentKbps: NaN,
targetSize: 0,
timemark: '00:00:00.00' }
还有一个 stderr
事件:
ffmpeg version n4.1 Copyright (c) 2000-2018 the FFmpeg developers
built with gcc 8.2.1 (GCC) 20180831
configuration: --prefix=/usr --disable-debug --disable-static --disable-stripping --enable-fontconfig --enable-gmp --enable-gnutls --enable-gpl --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libdrm --enable-libfreetype --enable-libfribidi --enable-libgsm --enable-libiec61883 --enable-libjack --enable-libmodplug --enable-libmp3lame --enable-libopencore_amrnb --enable-libopencore_amrwb --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libv4l2 --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxcb --enable-libxml2 --enable-libxvid --enable-nvdec --enable-nvenc --enable-omx --enable-shared --enable-version3
libavutil 56. 22.100 / 56. 22.100
libavcodec 58. 35.100 / 58. 35.100
libavformat 58. 20.100 / 58. 20.100
libavdevice 58. 5.100 / 58. 5.100
libavfilter 7. 40.101 / 7. 40.101
libswscale 5. 3.100 / 5. 3.100
libswresample 3. 3.100 / 3. 3.100
libpostproc 55. 3.100 / 55. 3.100
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x562c9e18de80] stream 0, offset 0x30: partial file
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x562c9e18de80] Could not find codec parameters for stream 0 (Video: h264 (avc1 / 0x31637661), none, 240x180, 374 kb/s): unspecified pixel format
Consider increasing the value for the 'analyzeduration' and 'probesize' options
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'pipe:0':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf54.63.104
Duration: 00:00:14.72, start: 0.000000, bitrate: N/A
Stream #0:0(und): Video: h264 (avc1 / 0x31637661), none, 240x180, 374 kb/s, 25 fps, 25 tbr, 12800 tbn, 25600 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 75 kb/s (default)
Metadata:
handler_name : SoundHandler
Stream mapping:
Stream #0:1 -> #0:0 (aac (native) -> mp3 (libmp3lame))
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x562c9e18de80] stream 1, offset 0xa1e: partial file
pipe:0: Invalid data found when processing input
Output #0, mp3, to 'pipe:1':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
TSSE : Lavf58.20.100
Stream #0:0(und): Audio: mp3 (libmp3lame), 44100 Hz, stereo, fltp, 192 kb/s (default)
Metadata:
handler_name : SoundHandler
encoder : Lavc58.35.100 libmp3lame
size= 0kB time=00:00:00.00 bitrate=N/A speed= 0x
{ frames: NaN,
currentFps: NaN,
currentKbps: NaN,
targetSize: 0,
timemark: '00:00:00.00' }
video:0kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
Output file is empty, nothing was encoded (check -ss / -t / -frames parameters if used)
后者解释了您在输出中看到的空文件。至于为什么错误只在流式传输文件时发生,而不是在通过其他方法打开文件时发生,这个答案解释了细节:
遗憾的是,该库尚不支持上面推荐的 movflags
输入选项 (https://github.com/fluent-ffmpeg/node-fluent-ffmpeg/issues/823)。
ffmpeg 根据我的经验,不喜欢使用流作为输入。下面的代码对我有用,首先让 ffmpeg 做它的事情,然后 create/play 和 input/output:
的流
let video = `${files[0].filename}`;
readstream.pipe(fs.createWriteStream(`./${video}`))
.on('error', () => {
console.log("Some error occurred in download:" + error);
res.send(error);
})
.on('finish', () => {
let audio = `${video.split(".")[0]}.mp3`
ffmpeg(`${video}`).output(audio)
.noVideo()
.format(`mp3`)
.on(`end`, (stdout, stderr) => {
let newReadstream = fs.createReadStream(`${audio}`);
})
.run()
}
我正在尝试从视频中提取音频。此代码运行良好:
ffmpeg('1.mp4').output('1.mp3')
.noVideo()
.format('mp3')
.outputOptions('-ab','192k')
.run();
但是如果我用这样的流读取文件:
var video = fs.createReadStream('1.mp4');
var audio = fs.createWriteStream('1.mp3');
ffmpeg(video).output(audio)
.noVideo()
.format('mp3')
.outputOptions('-ab','192k')
.run();
输出文件重 1KB,里面没有任何东西。
如何使用流提取音频?
假设您正在使用 ffmpeg 包,它基本上是节点中 运行 ffmpeg 的包装器,您必须像这样进行操作:
此代码在我的系统上运行,使用您在上面评论中提供的相同文件,但您可能需要进行一些更改(节点 v8.12.0、ffmpeg v0.0.4)。
const ffmpeg = require('ffmpeg');
try {
let process = new ffmpeg('3.mp4');
process.then(function (video) {
// Callback mode
video.fnExtractSoundToMP3('3.mp3', function (error, file) {
if (!error)
console.log('Audio file: ' + file);
});
}, function (err) {
console.log('Error: ' + err);
});
} catch (e) {
console.log(e.code);
console.log(e.msg);
}
为了进一步调查,我在转码时为不同类型的错误以及其他事件添加了回调。第一个提示是 progress
事件的以下回调:
{ frames: NaN,
currentFps: NaN,
currentKbps: NaN,
targetSize: 0,
timemark: '00:00:00.00' }
还有一个 stderr
事件:
ffmpeg version n4.1 Copyright (c) 2000-2018 the FFmpeg developers
built with gcc 8.2.1 (GCC) 20180831
configuration: --prefix=/usr --disable-debug --disable-static --disable-stripping --enable-fontconfig --enable-gmp --enable-gnutls --enable-gpl --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libdrm --enable-libfreetype --enable-libfribidi --enable-libgsm --enable-libiec61883 --enable-libjack --enable-libmodplug --enable-libmp3lame --enable-libopencore_amrnb --enable-libopencore_amrwb --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-libsoxr --enable-libspeex --enable-libssh --enable-libtheora --enable-libv4l2 --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxcb --enable-libxml2 --enable-libxvid --enable-nvdec --enable-nvenc --enable-omx --enable-shared --enable-version3
libavutil 56. 22.100 / 56. 22.100
libavcodec 58. 35.100 / 58. 35.100
libavformat 58. 20.100 / 58. 20.100
libavdevice 58. 5.100 / 58. 5.100
libavfilter 7. 40.101 / 7. 40.101
libswscale 5. 3.100 / 5. 3.100
libswresample 3. 3.100 / 3. 3.100
libpostproc 55. 3.100 / 55. 3.100
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x562c9e18de80] stream 0, offset 0x30: partial file
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x562c9e18de80] Could not find codec parameters for stream 0 (Video: h264 (avc1 / 0x31637661), none, 240x180, 374 kb/s): unspecified pixel format
Consider increasing the value for the 'analyzeduration' and 'probesize' options
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'pipe:0':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
encoder : Lavf54.63.104
Duration: 00:00:14.72, start: 0.000000, bitrate: N/A
Stream #0:0(und): Video: h264 (avc1 / 0x31637661), none, 240x180, 374 kb/s, 25 fps, 25 tbr, 12800 tbn, 25600 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #0:1(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 75 kb/s (default)
Metadata:
handler_name : SoundHandler
Stream mapping:
Stream #0:1 -> #0:0 (aac (native) -> mp3 (libmp3lame))
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x562c9e18de80] stream 1, offset 0xa1e: partial file
pipe:0: Invalid data found when processing input
Output #0, mp3, to 'pipe:1':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
TSSE : Lavf58.20.100
Stream #0:0(und): Audio: mp3 (libmp3lame), 44100 Hz, stereo, fltp, 192 kb/s (default)
Metadata:
handler_name : SoundHandler
encoder : Lavc58.35.100 libmp3lame
size= 0kB time=00:00:00.00 bitrate=N/A speed= 0x
{ frames: NaN,
currentFps: NaN,
currentKbps: NaN,
targetSize: 0,
timemark: '00:00:00.00' }
video:0kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
Output file is empty, nothing was encoded (check -ss / -t / -frames parameters if used)
后者解释了您在输出中看到的空文件。至于为什么错误只在流式传输文件时发生,而不是在通过其他方法打开文件时发生,这个答案解释了细节:
遗憾的是,该库尚不支持上面推荐的 movflags
输入选项 (https://github.com/fluent-ffmpeg/node-fluent-ffmpeg/issues/823)。
ffmpeg 根据我的经验,不喜欢使用流作为输入。下面的代码对我有用,首先让 ffmpeg 做它的事情,然后 create/play 和 input/output:
的流 let video = `${files[0].filename}`;
readstream.pipe(fs.createWriteStream(`./${video}`))
.on('error', () => {
console.log("Some error occurred in download:" + error);
res.send(error);
})
.on('finish', () => {
let audio = `${video.split(".")[0]}.mp3`
ffmpeg(`${video}`).output(audio)
.noVideo()
.format(`mp3`)
.on(`end`, (stdout, stderr) => {
let newReadstream = fs.createReadStream(`${audio}`);
})
.run()
}