amishshah / prism-media

Easily transcode media using Node.js 🎶
https://amishshah.github.io/prism-media
Apache License 2.0
239 stars 53 forks source link

Premature close error #116

Closed ThePedroo closed 1 year ago

ThePedroo commented 1 year ago

Issue:

Hello, I'm getting a premature close, I'm trying to re-set filters in ffmpeg, and for that I need to restart the stream, new prism.FFmpeg again, and send to djsvoice to play, problem is, when I try to do that, I get a premature close from prism media, and I'm not sure why, stopping it would also cause to premature close. Not sure if this is more of a djs voice issue or prism media, but should be fixable with prism media probably, thanks anyways.

Steps to reproduce:

Here's how I'm doing it, not sure how exactly tell people to reproduce it, I'm sorry.

const metadata = this.player.state.resource.metadata
https.get(metadata, {
  headers: {
    'User-Agent': 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
    'Range': 'bytes=0-'
  }
}, (res) => {
  if (res.statusCode != 206) {
    this.config.track = null
    this.config.filters = {}

    res.destroy()

    utils.debugLog('trackException', 2, { track: decodedTrack, guildId: this.config.guildId, exception: resource.exception })

    return this.config
  }

  const file = fs.createWriteStream(`./cache/${this.config.guildId}.webm`)
  res.pipe(file)

  file.on('finish', async () => {
    console.log('Starting ffmpeg process')
    this.cache.ffmpeg = new prism.FFmpeg({
      args: [
        '-loglevel', '0',
        '-analyzeduration', '0',
        '-y',
        '-ss', `${new Date() - this.cache.startedAt}ms`,
        '-i', `./cache/${this.config.guildId}.webm`,
        '-f', 's16le',
        '-ar', '48000',
        '-ac', '2',
        '-af', filterCommand.join(',')
      ]
    }).process

    this.cache.ffmpeg.on('close', () => {
      fs.rm(`./cache/${this.config.guildId}.webm`, () => {})
    })

    // stop player
    this.player.stop(true)

    const resource = new djsVoice.AudioResource([], [this.cache.ffmpeg.stdout, new prism.VolumeTransformer({ type: 's16le' }), new prism.opus.Encoder({ rate: 48000, channels: 2, frameSize: 960 }) ], metadata, 5) 

    this.cache.silence = true

    this.connection.subscribe(this.player)
    this.player.play(resource)
  })
})

Further details:

ThePedroo commented 1 year ago

I fixed it, and for people who had the same issue, instead of killing with the child process, use .destroy from ffmpeg