vjeux / mp4-h264-re-encode

Pure re-encoding of an mp4-h264 video file with the web APIs as well as in-depth description of how it works.
https://vjeux.github.io/mp4-h264-re-encode/mp4box.html
MIT License
462 stars 7 forks source link

audio track encoding? #22

Open juntaosun opened 1 year ago

juntaosun commented 1 year ago

This is great, can you continue to support audio track encoding?

gomjellie commented 1 year ago

🙏🏻 please

hughfenghen commented 1 year ago

@juntaosun @gomjellie

@webav/av-cliper can mixin audio tracks and re-encoding. https://github.com/hughfenghen/WebAV/blob/main/packages/av-cliper/README.md#usage

Here is a code example to create the audio track https://github.com/hughfenghen/WebAV/blob/main/packages/av-cliper/src/mp4-utils.ts#L295C37-L295C37

const audioTrackOpts = {
  timescale: 1e6,
  samplerate: audioOpts.sampleRate,
  channel_count: audioOpts.channelCount,
  samplesize: audioOpts.sampleSize,
  hdlr: 'soun',
  name: 'SoundHandler',
  type: 'mp4a'
}

let trackId = 0
const encoder = new AudioEncoder({
  output: (chunk, meta) => {
    if (trackId === 0 && meta.decoderConfig?.description != null) {
      trackId = mp4File.addTrack({
        ...audioTrackOpts,
        description: createESDSBox(meta.decoderConfig?.description)
      })
    }
})