<commit_before>import ffmpeg from 'fluent-ffmpeg';
import getFileExtension from './getFileExtension';
import mimeTypes from './mimeTypes';

const ffmpegHash = {};

const startConversion = (torrent, fileStream, res) => new Promise((resolve, reject) => {
  const { data } = torrent;
  const fileExtension = getFileExtension(data.name);
  const mime = mimeTypes[fileExtension];
  if (!mime) { reject(new Error(`streamConversion: Invalid mime type: ${data.name}`)); }

  const converter = ffmpeg()
    .input(fileStream)
    .outputOptions('-movflags frag_keyframe+empty_moov')
    .outputFormat('mp4')
    .output(res)

    .on('codecData', (codecData) => {
      // console.log('fluent-ffmpeg Notice: CodecData:', codecData);
      ffmpegHash[torrent.hash] = codecData;
      resolve();
    })
    .on('start', (cmd) => { console.log('fluent-ffmpeg Notice: Started:', cmd); })
    .on('progress', (progress) => { console.log('fluent-ffmpeg Notice: Progress:', progress.timemark, 'converted'); })
    .on('error', (err, stdout, stderr) => {
      // console.log('ffmpeg, file:', data.path, ' Error:', '\nErr:', err, '\nStdOut:', stdout, '\nStdErr:', stderr);
    });

  if (fileExtension === '.mkv') {
    converter.addOption('-vcodec')
      .addOption('copy')
      .addOption('-acodec')
      .addOption('copy')
      .run();
  } else {
    converter.inputFormat(fileExtension.substr(1))
      .audioCodec('aac')
      .videoCodec('libx264')
      .run();
  }
});

export default startConversion;
<commit_msg>feat(conversion): kill ffmpeg when res is closed<commit_after>import ffmpeg from 'fluent-ffmpeg';
import getFileExtension from './getFileExtension';
import mimeTypes from './mimeTypes';

const ffmpegHash = {};

const startConversion = (torrent, fileStream, res) => new Promise((resolve, reject) => {
  const { data } = torrent;
  const fileExtension = getFileExtension(data.name);
  const mime = mimeTypes[fileExtension];
  if (!mime) { reject(new Error(`streamConversion: Invalid mime type: ${data.name}`)); }

  const converter = ffmpeg()
    .input(fileStream)
    .outputOptions('-movflags frag_keyframe+empty_moov')
    .outputFormat('mp4')
    .output(res)

    .on('codecData', (codecData) => {
      // console.log('fluent-ffmpeg Notice: CodecData:', codecData);
      ffmpegHash[torrent.hash] = codecData;
      resolve();
    })
    .on('start', (cmd) => { console.log('fluent-ffmpeg Notice: Started:', cmd); })
    .on('progress', (progress) => { console.log('fluent-ffmpeg Notice: Progress:', progress.timemark, 'converted'); })
    .on('error', (err, stdout, stderr) => {
      console.log('ffmpeg, file:', data.path, ' Error:', '\nErr:', err, '\nStdOut:', stdout, '\nStdErr:', stderr);
    });

  if (fileExtension === '.mkv') {
    converter.addOption('-vcodec')
      .addOption('copy')
      .addOption('-acodec')
      .addOption('copy')
      .run();
  } else {
    converter.inputFormat(fileExtension.substr(1))
      .audioCodec('aac')
      .videoCodec('libx264')
      .run();
  }
  res.on('close', () => {
    converter.kill();
  });
});

export default startConversion;

{'fraction_non_alphanumeric': 0.18492268041237114, 'fraction_numerical': 0.0032216494845360823, 'mean_word_length': 3.7477064220183487, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 16, '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': '4216518', 'n_tokens_mistral': 1061, 'n_tokens_neox': 1045, 'n_words': 206}