javascriptffmpegprocess-managementshelljs

how to stop the FFmpeg process programmatically without using Ctrl + C?


const shell = require("shelljs");

const processShell = shell.exec(
  `ffmpeg -i "https://pull-hls-f16-va01.tiktokcdn.com/game/stream-2998228870023348312_or4/index.m3u8?expire=1728629296&session_id=000-2024092706481532076B1319E1100113F8&sign=1016b521d08053bc0ae8eddb0881b029" -movflags use_metadata_tags -map_metadata 0 -metadata title="Chill chill kiếm kèo Warthunder" -metadata artist="bacgaucam" -metadata year="2024" -c copy "downloads/bacgaucam-927202491939.mp4" -n -stats -hide_banner -loglevel error`,
  { async: true }
);

setTimeout(() => {
  // processShell.kill();
  process.exit();
}, 20000);

my video only works when I use Ctrl+C to stop it. I’ve tried using process.exit(), .kill(pid, "SIGINT"), and the .kill() method from the shell.exec() ffmpeg() of fluent-ffmpeg or spawn() of child_process reference, but none of them work

If you want to test it directly, please message me to get a new live url in case it expires (https://t.me/ppnam15) or clone this: https://github.com/loo-kuhs/tiktok-live-downloader

can anyone help? Thanks!


Solution

  • 'use strict';
    
    const { spawn } = require('child_process');
    
    const url = "https://LongURL/file.m3u8"
    
    const ffmpeg_process = spawn('ffmpeg', ['-i', url, '-c', 'copy', './video.mp4'], { stdio: ['pipe', 'pipe', 'pipe'] });
    
    ffmpeg_process.on('spawn', () => {
        console.log('FFmpeg process started');
    });
    
    ffmpeg_process.on('error', (err) => {
        console.error('Failed to start FFmpeg process:', err);
    });
    
    ffmpeg_process.on('close', (code) => {
        console.log(`FFmpeg process exited with code ${code}`);
    });
    
    function stopFFmpeg() {
        if (ffmpeg_process && ffmpeg_process.stdin) {
            ffmpeg_process.stdin.write('q');
            ffmpeg_process.stdin.end();
            ffmpeg_process.on('close', () => {
                console.log('FFmpeg process stopped');
            });
        }
    }
    
    setTimeout(stopFFmpeg, 10000);