You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用ffmpeg将.mov流转为.flv时生成0KB无效文件的问题

Why Your fluent-ffmpeg Stream Approach Produces a 0KB FLV File (and How to Fix It)

Great question—this is a common gotcha when working with streams in fluent-ffmpeg. Let’s break down why your first code fails and how to fix it.

Root Cause

When you pass a file path to ffmpeg(), the library can automatically probe the file to detect its format, codecs, and other metadata. This lets ffmpeg properly decode the input without extra configuration.

But when you pass a ReadStream, ffmpeg can’t perform this upfront probing because streams are not seekable (it can’t jump around to read metadata headers like it can with a file). Without knowing the input format, ffmpeg doesn’t know how to decode the stream, so it doesn’t produce any output—hence the 0KB file. Additionally, using .pipe() directly with a stream input might not correctly trigger the ffmpeg processing workflow.

Fix: Explicitly Define Input Format and Use Proper Output Handling

To make the stream approach work, you need to:

  1. Tell ffmpeg exactly what format the input stream is in.
  2. Use fluent-ffmpeg’s built-in output methods to ensure the processing runs correctly.

Here’s the corrected code:

var ffmpeg = require('fluent-ffmpeg');
var fs = require('fs');

var writeStream = fs.createWriteStream('output.flv');
var readStream = fs.createReadStream('video.mov');

ffmpeg(readStream)
  .inputFormat('mov') // Explicitly set input format (matches your source file)
  .format('flv')      // Set output format
  .output(writeStream)
  .on('end', () => console.log('Conversion finished successfully!'))
  .on('error', (err) => console.error('Conversion failed:', err))
  .run(); // Trigger the ffmpeg process

Why This Works

  • inputFormat('mov'): Gives ffmpeg the critical info it needs to decode the stream since it can’t probe the file itself.
  • .output(writeStream) + .run(): Properly initializes the ffmpeg process, which reads from the input stream, processes the video/audio, and writes the converted data to your output stream. The .pipe() method doesn’t handle the process initiation correctly when using a stream input, leading to no data being processed.

Bonus Tips

  • Always include error handling to catch issues like missing ffmpeg binaries, invalid input streams, or format mismatches.
  • If you’re unsure of the input format, you can use tools like ffprobe (included with ffmpeg) to check before setting inputFormat.

内容的提问来源于stack exchange,提问作者Wise Colt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:50:13