stop ffmpeg stream at specific time of the day

Viewed 163

This stream will stop after 3600 seconds with the -t option.

Is it possible to stop the stream at a certain time of the day, e.g. at 1:00 A.M. using ffmpeg only?

ffmpeg -f dshow -i video="Virtual-Camera" -preset ultrafast -vcodec libx264 -tune zerolatency -b 900k \
-f mpegts -t 3600 udp://10.1.0.102:1234

The Docs are very clear about the syntax for Time duration.

So something like -t (1+1) doesn't work. It is not evaluated to -t 2.

I think Expression Evaluation doesn't help either because it seems to be only valid in filters.

A simple bash solution to stop at 01:00 AM would be:

# Set time to stop
# maximum value is 23:59:59
# use `offset_tomorrow` to set a time tomorrow
time_to_stop="23:59:59"

# remaining seconds until 1 AM
offset_tomorrow=3601

# calculate time difference between maximum time and now and add offset in seconds.
# add 1 hour and 1 minute to get the remaining seconds until 1 AM.
seconds_to_stop=$((`date -d$time_to_stop '+%s'`-`date '+%s'` + $offset_tomorrow))

ffmpeg -f dshow -i video="Virtual-Camera" -preset ultrafast -vcodec libx264 -tune zerolatency -b 900k \
-f mpegts -t $seconds_to_stop udp://10.1.0.102:1234
1 Answers

A more general Bash solution would be

#!/bin/bash
time_to_stop="$1"  # e.g. 15:47
s=$(date -d"$time_to_stop" '+%s')
now=$(date '+%s')
((s < now)) && ((s+=86400)) # add 24h (in seconds)
t=$((s - now))

then use value of t as and argument for -t (or -to ?) in the ffmpeg command line.

Related