0

I'm transcoding an RTMP stream using FFMPEG. I want the output to have a bitrate of 600k(approx) if the original bitrate is greater than 600k and otherwise the output should have the original bitrate. I've looked at -minrate, -maxrate, -bufsize. But don't think they are applicable here. Is there any way to achieve this, preferably in single FFMPEG command?

tezz
  • 349
  • 2
  • 19
  • 1
    Not possible in single command. What you need to do is: 1) determine bitrate of input. The longer your test segment the more accurate the result. 2) Use if/then statement in your favorite scripting language to select the appropriate ffmpeg command. – llogan Sep 23 '19 at 17:50
  • @llogan Because the input is an RTMP source, using `ffprobe` command to look for `bit_rate` in the video stream isn't working. (bitrate would be N/A). But I think there are other ways to determine this. e.g., `packet_size / duration` OR `bits_per_coded_sample * sample_rate` (I'm not sure about these ways) – tezz Sep 25 '19 at 17:06

1 Answers1

0

Here is an example of how it can be done. Read input with ffprobe for a short duration. This is safe if the bit_rate cannot be detected (i.e. N/A or empty)

q=$(ffprobe -i rtmp://localhost/rtmpapp/key -select_streams v:0 -show_entries stream=width,height,bit_rate  -of csv=s=_:p=0 -analyzeduration 100k -probesize 100k)

bit_rate=$(echo $q | cut -d'_' -f 3)
b=$(echo "($bit_rate / 1000)" | bc)  #convert to kbps

target=4000 # this will be maximum target bitrate
btarget=$(( (target < b || b == 0) ? target : b )) #get min of the two
bmax=$(echo "($btarget*1.1) /1" | bc) #multiplier for maxrate
bbuf=$(echo "($btarget*1.5) /1" | bc) #multiplier for bufsize
Mark Sergienko
  • 181
  • 1
  • 4