<commit_before>/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */

import { expect } from 'chai'
import * as ffmpeg from 'fluent-ffmpeg'
import { pathExists, readdir } from 'fs-extra'
import { join } from 'path'
import { buildAbsoluteFixturePath, wait } from '../miscs'
import { PeerTubeServer } from '../server/server'

function sendRTMPStream (options: {
  rtmpBaseUrl: string
  streamKey: string
  fixtureName?: string // default video_short.mp4
  copyCodecs?: boolean // default false
}) {
  const { rtmpBaseUrl, streamKey, fixtureName = 'video_short.mp4', copyCodecs = false } = options

  const fixture = buildAbsoluteFixturePath(fixtureName)

  const command = ffmpeg(fixture)
  command.inputOption('-stream_loop -1')
  command.inputOption('-re')

  if (copyCodecs) {
    command.outputOption('-c:v libx264')
    command.outputOption('-g 50')
    command.outputOption('-keyint_min 2')
    command.outputOption('-r 60')
  } else {
    command.outputOption('-c copy')
  }

  command.outputOption('-f flv')

  const rtmpUrl = rtmpBaseUrl + '/' + streamKey
  command.output(rtmpUrl)

  command.on('error', err => {
    if (err?.message?.includes('Exiting normally')) return

    if (process.env.DEBUG) console.error(err)
  })

  if (process.env.DEBUG) {
    command.on('stderr', data => console.log(data))
  }

  command.run()

  return command
}

function waitFfmpegUntilError (command: ffmpeg.FfmpegCommand, successAfterMS = 10000) {
  return new Promise<void>((res, rej) => {
    command.on('error', err => {<commit_msg>Fix send rtmp command<commit_after>/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */

import { expect } from 'chai'
import * as ffmpeg from 'fluent-ffmpeg'
import { pathExists, readdir } from 'fs-extra'
import { join } from 'path'
import { buildAbsoluteFixturePath, wait } from '../miscs'
import { PeerTubeServer } from '../server/server'

function sendRTMPStream (options: {
  rtmpBaseUrl: string
  streamKey: string
  fixtureName?: string // default video_short.mp4
  copyCodecs?: boolean // default false
}) {
  const { rtmpBaseUrl, streamKey, fixtureName = 'video_short.mp4', copyCodecs = false } = options

  const fixture = buildAbsoluteFixturePath(fixtureName)

  const command = ffmpeg(fixture)
  command.inputOption('-stream_loop -1')
  command.inputOption('-re')

  if (copyCodecs) {
    command.outputOption('-c copy')
  } else {
    command.outputOption('-c:v libx264')
    command.outputOption('-g 50')
    command.outputOption('-keyint_min 2')
    command.outputOption('-r 60')
  }

  command.outputOption('-f flv')

  const rtmpUrl = rtmpBaseUrl + '/' + streamKey
  command.output(rtmpUrl)

  command.on('error', err => {
    if (err?.message?.includes('Exiting normally')) return

    if (process.env.DEBUG) console.error(err)
  })

  if (process.env.DEBUG) {
    command.on('stderr', data => console.log(data))
  }

  command.run()

  return command
}

function waitFfmpegUntilError (command: ffmpeg.FfmpegCommand, successAfterMS = 10000) {
  return new Promise<void>((res, rej) => {
    command.on('error', err => {
{'fraction_non_alphanumeric': 0.14167725540025414, 'fraction_numerical': 0.010165184243964422, 'mean_word_length': 4.128664495114006, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6519533', 'n_tokens_mistral': 1079, 'n_tokens_neox': 1034, 'n_words': 225}