Project

General

Profile

Multiple MPEG-TS Spawn, hanging ffmpeg, need help

Added by Bogdan I. over 2 years ago

I have the following setup:

- Tvheadend 4.3-2009, installed from the PPA
- Ubuntu 18.04 (and Ubuntu 20.04)
- Multiple DVB-C tuner makes

I need to burn in subtitles on the video, and do hardware transcoding. Sadly this leaves me with requirement of spawning ffmpeg to handle my transcoding needs.

Due to various issues, sometimes a channel will go offline at the source, because the providers in my area are just crappy.

I have around 3 profiles that I use right now, each one doing it's own thing, setup like this:

The bash script I use is:

#!/bin/bash

if read -t 2 -n 1; then

    /usr/local/bin/v_ffmpeg -threads 1 -v fatal -nostats -vsync 1 -async 1 \
    -init_hw_device cuda=cuda -filter_hw_device cuda -extra_hw_frames 3 -reinit_filter 1 -hwaccel nvdec -hwaccel_output_format cuda -filter_threads 2 -filter_complex_threads 2 \
    -i - -filter_complex "[0:s] scale=w=1920:h=1080 [sub]; [0:v] hwdownload [video]; [video][sub] overlay,format=nv12,hwupload=derive_device=cuda [v]" \
    -map "[v]" -map 0:a \
    -c:v h264_nvenc -preset medium -b:v 5M -bufsize 10M -profile:v main -temporal-aq 1 \
    -acodec copy -copy_unknown \
    -f mpegts -

else
  exit 1
fi

The issue I'm facing is that sometimes, when a channel is offline (no signal from dvb-c), the ffmpeg process WILL spawn, but it will hang indefinitely wait because there is no input from the stdin pipe. For some reasons, tvheadend will not kill the ffmpeg child. But because the hardware is initialized, this ends up using around 76MB VRAM. During half a day, this will obviously end up using all my available VRAM, even tough the processes don't really do anything but wait for input.

To try to get around the issue of no input, I have added the read -t 2 -n 1 block. In my limited testing this SHOULD timeout if there's no input from stdin, and then obviously exit the script. Sadly, this does not seem to happen, so after around 12 hours, I end up with something like this:

        ├─tvheadend─┬─1567*[hd-5m.sh]
        │           ├─10*[hd-5m.sh───z_ffmpeg───22*[{z_ffmpeg}]]
        │           ├─8*[sd-sub-0.sh───z_ffmpeg───21*[{z_ffmpeg}]]
        │           ├─5*[sub-burn-0.sh]
        │           ├─3*[sub-burn-0.sh───v_ffmpeg───4*[{v_ffmpeg}]]
        │           └─231*[{tvheadend}]

So the question I'm asking is: how exactly can I prevent this behaviour and make tvheadend kill the orphaned ffmpeg children ?

I have tried:

- swapping the "Kill Signal" (SIGKILL, SIGTERM), there was no difference
- reducing/increasing the "Kill Timeout", there was no difference
- changing the data timeout, there was no difference

Stracing the script and/or the ffmpeg process reveals it doing nothing but waiting:

root@nxt1:/opt# strace -p 2475
strace: Process 2475 attached
read(0,

Replies (4)

RE: Multiple MPEG-TS Spawn, hanging ffmpeg, need help - Added by saen acro over 2 years ago

Where is pipe IN and pipe OUT?

RE: Multiple MPEG-TS Spawn, hanging ffmpeg, need help - Added by Bogdan I. over 2 years ago

-i - and -f mpegts - are the ins and out.

RE: Multiple MPEG-TS Spawn, hanging ffmpeg, need help - Added by saen acro over 2 years ago

Bogdan I. wrote:

-i - and -f mpegts - are the ins and out.

there is no pipe in your example

/usr/local/bin/ffmpeg -i pipe:0 -c:a copy -c:v h264_nvenc -b:v 3M -minrate 3M -maxrate 3M -bufsize 6M -r 25 -threads 4 -f mpegts pipe:1

so for your case
-i -
must be
-i $1

and
-f mpegts -
must be
-f mpegts $2

and runned as
/script.sh "pipe:0" "pipe:1"

some examples
https://tvheadend.org/boards/4/topics/45229
https://tvheadend.org/boards/5/topics/34202
https://tvheadend.org/boards/5/topics/43530
https://tvheadend.org/issues/4905

RE: Multiple MPEG-TS Spawn, hanging ffmpeg, need help - Added by Bogdan I. over 2 years ago

That's not an issue.

You don't need a pipe:0/1, - is enough for stdin/stdout, that's how dash (-) it gets interpreted.

- on the output side means stdout. You can also write pipe:1 in its place. As input, it means stdin and can be written as pipe:0.

This is an answer from Gyan Doshi, an ffmpeg maintainer/developer - https://stackoverflow.com/questions/63596219/what-does-dash-mean-as-ffmpeg-output-filename#comment115294653_63596688

    (1-4/4)