TransWikia.com

FFmpeg starting manually but not with Systemd on boot

Stack Overflow Asked on November 20, 2021

On Raspberry Pi 4 B 4GB with official Debian 10 image, I have /home/pi/run.sh script with following:

#!/bin/bash
ffmpeg -nostdin -framerate 15 -video_size 1280x720 -input_format yuyv422  -i /dev/video0 -f alsa -i hw:Device 
    -af acompressor=threshold=-14dB:ratio=9:attack=10:release=1000 -c:a aac -ac 2 -ar 48000 -ab 160k 
    -c:v libx264 -pix_fmt yuv420p -b:v 3M -bf 1 -g 20 -flags +ilme+ildct -preset ultrafast 
    -streamid 0:0x101 -streamid 1:0x100 -mpegts_pmt_start_pid 4096 -mpegts_start_pid 0x259 -metadata:s:a:0 language="" -mpegts_service_id 131 -mpegts_transport_stream_id 9217 -metadata provider_name="Doesnt matter" -metadata service_name="Doesnt matter" 
    -minrate 3500 -maxrate 3500k -bufsize 4500k -muxrate 4000k  -f mpegts "udp://@239.1.67.13:1234?pkt_size=1316&bitrate=4000000&dscp=34" -loglevel debug < /dev/null > /tmp/ff3.log 2>&1

Script is starting from console without problems. It takes audio from USB sound card and video from USB camera and creates UDP stream to IPTV. Then I created Systemd service:

[Unit]
Description=Streamer
After=multi-user.target sound.target network.target

[Service]
ExecStart=/home/pi/run.sh
KillMode=control-group
Restart=on-failure
TimeoutSec=1

[Install]
WantedBy=multi-user.target
Alias=streaming.service

After restarting Raspberry, script has started, but FFmpeg hangs on error failures in log:

cur_dts is invalid st:0 (257) [init:1 i_done:0 finish:0] (this is harmless if it occurs once at the start per stream)
cur_dts is invalid st:1 (256) [init:1 i_done:0 finish:0] (this is harmless if it occurs once at the start per stream)
cur_dts is invalid st:0 (257) [init:1 i_done:0 finish:0] (this is harmless if it occurs once at the start per stream)
cur_dts is invalid st:1 (256) [init:1 i_done:0 finish:0] (this is harmless if it occurs once at the start per stream)
cur_dts is invalid st:0 (257) [init:1 i_done:0 finish:0] (this is harmless if it occurs once at the start per stream)
cur_dts is invalid st:1 (256) [init:1 i_done:0 finish:0] (this is harmless if it occurs once at the start per stream)
cur_dts is invalid st:0 (257) [init:1 i_done:0 finish:0] (this is harmless if it occurs once at the start per stream)
cur_dts is invalid st:1 (256) [init:1 i_done:0 finish:0] (this is harmless if it occurs once at the start per stream)

and will not start streaming to UDP target. But, if I manually login to SSH and issue systemctl stop streaming and then systemctl start streaming Ffmpeg starts successfully. What’s different with service auto-start on boot?

Setting the "sleep timeout" at script begginging will not help. However, removing audio stream from FFmpeg config looks to solve auto-start on boot.

2 Answers

For those who come to this question later, I had the same problem with running an FFmpeg streaming code at boot with systemd.

What I did to solve the problem was to write another python code (with subprocess imported) to stop and start the streaming systemd service again after the boot finishes with some sleeping delay. and adding this code as another service for systemd which runs after the previous service(under [Unit] use After=streaming.service).

Answered by Mahdi Joodi on November 20, 2021

TLDR

use

After=network-online.target

Find out real problem step by step.

1. What this error tells us.

github ffmpeg shows the error is while selecting output. choose_output function chooses output for stream.

2. Why output is not ready?

As you are trying to stream over network so You need fully configured network before you start ffmpeg.

You have small issue in your systemd script. there is difference between network.target and network-online.target

follow systemd manual for network targets

network.target has very little meaning during start-up.

network-online.target is a target that actively waits until the nework is "up"

Now you would know why your script was not working in systemd at startup but after service restart it started working?

Do comment for any more clarification needed.

Answered by Devidas on November 20, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP