Typescript 将ffmpeg传输到flac编码器时出现的问题

Typescript 将ffmpeg传输到flac编码器时出现的问题,typescript,ffmpeg,flac,Typescript,Ffmpeg,Flac,我需要用seektables对flac文件进行编码,ffmpeg的flac编码器不包括seektables,因此我需要使用flac CLI。我试图将任意音频文件转换为可查找的flac文件,方法是首先通过ffmpeg将其传输到flac编码器 export const transcodeToFlac: AudioTranscoder<{}> = ({ source, destination }) => { return new Promise((resolve, rej

我需要用seektables对flac文件进行编码,ffmpeg的flac编码器不包括seektables,因此我需要使用flac CLI。我试图将任意音频文件转换为可查找的flac文件,方法是首先通过ffmpeg将其传输到flac编码器

export const transcodeToFlac: AudioTranscoder<{}> = ({
  source,
  destination
}) => {
  return new Promise((resolve, reject) => {
    let totalSize = 0

    const { stdout: ffmpegOutput, stderr: ffmpegError } = spawn("ffmpeg", [
      "-i",
      source,
      "-f",
      "wav",
      "pipe:1"
    ])

    const { stdout: flacOutput, stdin: flacInput, stderr: flacError } = spawn(
      "flac",
      ["-"]
    )

    flacOutput.on("data", (buffer: Buffer) => {
      totalSize += buffer.byteLength
    })

    ffmpegError.on("data", error => {
      console.log(error.toString())
    })

    flacError.on("data", error => {
      console.log(error.toString())
    })

    //stream.on("error", reject)

    destination.on("finish", () => {
      resolve({
        mime: "audio/flac",
        size: totalSize,
        codec: "flac",
        bitdepth: 16,
        ext: "flac"
      })
    })

    ffmpegOutput.pipe(flacInput)
    flacOutput.pipe(destination)
  })
}
第一件事:

我需要用seektables对flac文件进行编码,
-:警告,编码到标准输出时无法回写搜索点

从flac-H的

A single INPUTFILE may be - for stdin.  No INPUTFILE implies stdin.  Use of
stdin implies -c (write to stdout).  Normally you should use:
   flac [options] -o outfilename  or  flac -d [options] -o outfilename
instead of:
   flac [options] > outfilename   or  flac -d [options] > outfilename
since the former allows flac to seek backwards to write the STREAMINFO or
WAVE/AIFF header contents when necessary.
尝试使用
flac--o outfilename.flac
而不仅仅是
flac-

它似乎适合我,并且产生的音频长度正确(在我的情况下,这与您的情况不同):
$rm out.flac;ffmpeg-nostdin-i~/audio/asmr/asmr\\摄像头\触摸\ \无\口音\ \无\说话-lQlZJ82ebBk.m4a-f wav-| flac--o out.flac


flac 1.3.2
Copyright (C) 2000-2009  Josh Coalson, 2011-2016  Xiph.Org Foundation
flac comes with ABSOLUTELY NO WARRANTY.  This is free software, and you are
welcome to redistribute it under certain conditions.  Type `flac' for details.

ffmpeg version n4.1.3 Copyright (c) 2000-2019 the FFmpeg developers
  built with gcc 8.2.1 (GCC) 20181127
  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
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/home/user/audio/asmr/ASMR _ Camera Touching _ No Mouthsounds _ NO TALKING-lQlZJ82ebBk.m4a':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2mp41
    encoder         : Lavf58.3.100
  Duration: 00:53:29.09, start: 0.000000, bitrate: 126 kb/s
    Stream #0:0(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 125 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
Stream mapping:
  Stream #0:0 -> #0:0 (aac (native) -> pcm_s16le (native))
Output #0, wav, to 'pipe:':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2mp41
    ISFT            : Lavf58.20.100
    Stream #0:0(und): Audio: pcm_s16le ([1][0][0][0] / 0x0001), 44100 Hz, stereo, s16, 1411 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
      encoder         : Lavc58.35.100 pcm_s16le
-: WARNING: skipping unknown chunk 'LIST' (use --keep-foreign-metadata to keep)
13% complete, ratio=0.284size=  552816kB time=00:53:29.09 bitrate=1411.2kbits/s speed= 665x      
video:0kB audio:552816kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.000014%
-: WARNING: unexpected EOF; expected 1073741823 samples, got 141516800 samples
13% complete, ratio=0.283

$ ffprobe ./out.flac 
ffprobe version n4.1.3 Copyright (c) 2007-2019 the FFmpeg developers
  built with gcc 8.2.1 (GCC) 20181127
  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
Input #0, flac, from './out.flac':
  Duration: 00:53:29.09, start: 0.000000, bitrate: 399 kb/s
    Stream #0:0: Audio: flac, 44100 Hz, stereo, s16

$ ffprobe ~/audio/asmr/ASMR\ _\ Camera\ Touching\ _\ No\ Mouthsounds\ _\ NO\ TALKING-lQlZJ82ebBk.m4a
ffprobe version n4.1.3 Copyright (c) 2007-2019 the FFmpeg developers
  built with gcc 8.2.1 (GCC) 20181127
  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
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/home/user/audio/asmr/ASMR _ Camera Touching _ No Mouthsounds _ NO TALKING-lQlZJ82ebBk.m4a':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2mp41
    encoder         : Lavf58.3.100
  Duration: 00:53:29.09, start: 0.000000, bitrate: 126 kb/s
    Stream #0:0(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 125 kb/s (default)
    Metadata:
      handler_name    : SoundHandler

$ vlc out.flac
#sounds ok, even at the end

-:警告:跳过未知区块“列表”(使用--保留要保留的外部元数据)

无可奈何:
错误:--从stdin或到stdout编码时无法使用keep外部元数据

然而,即使我得到了
-:警告:意外EOF;预期1073741823个样本,获得141516800个样本

这就是它停止在13%(141516800*100/1073741823=13.2%)的原因,输出看起来很好,长度与输入相同

更新: 这是因为
ffmpeg
没有填充输出的正确值
wav
,因为它被发送到管道而不是文件,所以ffmpeg最初使用四个0xFF字节作为ChunkSize,到wav编码完成时,ffmpeg知道正确的值是什么,但它无法返回到输出管道以更新ChunkSize部分。当输出到文件时,它可以

The canonical WAVE format starts with the RIFF header:

0         4   ChunkID          Contains the letters "RIFF" in ASCII form
                               (0x52494646 big-endian form).
4         4   ChunkSize        36 + SubChunk2Size, or more precisely:
                               4 + (8 + SubChunk1Size) + (8 + SubChunk2Size)
                               This is the size of the rest of the chunk 
                               following this number.  This is the size of the 
                               entire file in bytes minus 8 bytes for the
                               two fields not included in this count:
                               ChunkID and ChunkSize.
8         4   Format           Contains the letters "WAVE"
                               (0x57415645 big-endian form).
以下是输出到文件时,与输出到管道时相比,
ffmpeg
wav输出的不同之处:

(注意:不要运行此操作,即使在快速处理器上,也需要几分钟的100%CPU使用率(即一个内核)

flac
不知道输入的正确大小(ChunkSize为0xffffff)

  • 但是wav文件(输出到文件时由ffmpeg生成)将正常:
  • a。将wav生成文件:
    ffmpeg-nostdin-i~/audio/asmr/asmr\\uu\Camera\Touching\\uno\moutsounds\\uno\TALKING-lQlZJ82ebBk.m4a-f wav-toafile.wav

    b。连接到flac的管道:

    $ rm out2.flac; cat toafile.wav | flac - -o out2.flac
    
    flac 1.3.2
    Copyright (C) 2000-2009  Josh Coalson, 2011-2016  Xiph.Org Foundation
    flac comes with ABSOLUTELY NO WARRANTY.  This is free software, and you are
    welcome to redistribute it under certain conditions.  Type `flac' for details.
    
    -: WARNING: skipping unknown chunk 'LIST' (use --keep-foreign-metadata to keep)
    -: wrote 160381819 bytes, ratio=0.283
    

    这是因为它正确设置了
    ChunkSize
    值。(ChunkSize为0x21BDC046=566083654,这比输出infle.wav(其总大小为566083662字节)少8字节)

    问题是,从flac进行管道传输时,我仍然需要利用流。在我的代码中,目标流可能是通过管道传输到外部远程存储解决方案,如GCS。啊,我的错误,读了你的问题后,我错误地理解了你只想通过管道从ffmpeg传输到flac。问题是,正如
    flac-H
    所说,flac无法返回到流中(因为它是一个流)来编写您所说需要的搜索点。所以我不明白你怎么能逃脱flac对管道的书写。也许尝试修改flac以写入临时文件?(可能是O_TMPFILE,这样其他人就看不到它了-我从未尝试过,但
    man 2 open
    )然后只有在
    flac
    进程退出后才开始将该文件传输到流。这似乎是个糟糕的解决方案,伊姆霍:(这实际上是我现在正在做的,但我讨厌它。我宁愿不使用磁盘,因为这应该是一个可伸缩的解决方案。我想我只能接受它。不过,我注意到一件奇怪的事,如果我将wav文件直接流到flac,而不使用ffmpeg,它似乎可以工作。看起来
    ffmpeg
    无法查找back或者(因为它是一个流),因此如果将wav ffmpeg到管道而不是文件,则得到4x2=8个字节,即0xFF。4个字节是,另外4个似乎是Lavf58.2.7列表头的一部分(我不知道)但其中3个与前4个相同。可能这些信息足以让
    flac
    不必再查找,因为现在它知道大小(块和文件)它需要计算seekpoints,因此可以正常写入而无需回寻,甚至可以写入流。我更新了答案以包含上述信息,但我猜您仍然必须从ffmpeg或flac输出到临时文件,尽管后者占用的空间似乎较少(flac vs wav),前者似乎避免了带有
    意外EOF
    的看似危险的flac警告(注:此注释可以删除)
    $ colordiff -up <(hexdump -C toafile.wav) <(hexdump -C piped.wav)
    --- /dev/fd/63  2019-05-19 21:28:20.621944056 +0200
    +++ /dev/fd/62  2019-05-19 21:28:20.621944056 +0200
    @@ -1,8 +1,8 @@
    -00000000  52 49 46 46 46 c0 bd 21  57 41 56 45 66 6d 74 20  |RIFFF..!WAVEfmt |
    +00000000  52 49 46 46 ff ff ff ff  57 41 56 45 66 6d 74 20  |RIFF....WAVEfmt |
     00000010  10 00 00 00 01 00 02 00  44 ac 00 00 10 b1 02 00  |........D.......|
     00000020  04 00 10 00 4c 49 53 54  1a 00 00 00 49 4e 46 4f  |....LIST....INFO|
     00000030  49 53 46 54 0e 00 00 00  4c 61 76 66 35 38 2e 32  |ISFT....Lavf58.2|
    -00000040  37 2e 31 30 33 00 64 61  74 61 00 c0 bd 21 00 00  |7.103.data...!..|
    +00000040  37 2e 31 30 33 00 64 61  74 61 ff ff ff ff 00 00  |7.103.data......|
     00000050  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
     *
     00003440  00 00 01 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    -----------
    
    cat piped.wav | flac - -o out2.flac
    
    flac 1.3.2
    Copyright (C) 2000-2009  Josh Coalson, 2011-2016  Xiph.Org Foundation
    flac comes with ABSOLUTELY NO WARRANTY.  This is free software, and you are
    welcome to redistribute it under certain conditions.  Type `flac' for details.
    
    -: WARNING: skipping unknown chunk 'LIST' (use --keep-foreign-metadata to keep)
    -: 13% complete, ratio=0.284-: WARNING: unexpected EOF; expected 1073741823 samples, got 141516800 samples
    13% complete, ratio=0.283
    
    $ rm out2.flac; cat toafile.wav | flac - -o out2.flac
    
    flac 1.3.2
    Copyright (C) 2000-2009  Josh Coalson, 2011-2016  Xiph.Org Foundation
    flac comes with ABSOLUTELY NO WARRANTY.  This is free software, and you are
    welcome to redistribute it under certain conditions.  Type `flac' for details.
    
    -: WARNING: skipping unknown chunk 'LIST' (use --keep-foreign-metadata to keep)
    -: wrote 160381819 bytes, ratio=0.283