Modified bitrate calc to hopefully be more efficient

This commit is contained in:
Saxon1 2017-11-22 16:06:51 +10:30
parent e6e7c2bcc6
commit 5f6f34cb25
1 changed files with 14 additions and 21 deletions

View File

@ -67,6 +67,7 @@ const (
rtpSSRC = 1 // any value will do
bitsInByte = 8
bitrateOutputDelay = 60 // s
nanoSecPerSec = 1000000000
)
// flag values
@ -91,9 +92,10 @@ var (
dumpCC int
dumpPCRBase uint64
rtpSequenceNum uint16
bitSum int
bitSum int = 0
ffmpegPath string
tempDir string
bitRateOutCount float64 = 0.0
)
// command-line flags
@ -108,8 +110,8 @@ var (
func main() {
if runtime.GOOS == "windows" {
ffmpegPath = "C:/ffmpeg"
tempDir = "C:/Users/%%USERPROFILE%%/AppData/Local/Temp"
ffmpegPath = "C:/ffmpeg/ffmpeg"
tempDir = "tmp/"
} else {
ffmpegPath = "/usr/bin/ffmpeg"
tempDir = "/tmp/"
@ -151,9 +153,6 @@ func main() {
log.Fatal("Cannot combine filterFixContinuity and dumpProgramInfo flags\n")
}
// Start a thread to calculate bitrate
go bitrateCalculator()
for {
err := readWriteVideo(*input, *output)
fmt.Fprintln(os.Stderr, err)
@ -161,19 +160,6 @@ func main() {
time.Sleep(10 * time.Second)
}
}
func bitrateCalculator() {
prevTime := time.Now()
for {
now := time.Now()
if now.Sub(prevTime) > bitrateOutputDelay*time.Second {
fmt.Printf("Bitrate: %d bps\n", int64(float64(bitSum)/(float64(now.Sub(prevTime)/1000000000))))
bitSum = 0
prevTime = now
}
}
}
// readWriteVideo reads video from an RTSP stream (specified by the input URL) and
// rewrites the video in various formats and/or different protocols (HTTP, UDP or RTP).
func readWriteVideo(input string, output string) error {
@ -265,8 +251,15 @@ func readWriteVideo(input string, output string) error {
clipCount++
if err = sendClip(clip[:clipSize], output, conn); err != nil {
return err
} else {
bitSum += clipSize * bitsInByte
}
// Calculate bitrate and Output
deltaTime := float64(now.Sub(prevTime)/nanoSecPerSec)
bitRateOutCount += deltaTime
if bitRateOutCount > bitrateOutputDelay {
noOfBits := clipSize * bitsInByte
fmt.Printf("Bitrate: %d bps\n",int64(float64(noOfBits)/deltaTime))
bitRateOutCount = 0.0
}
clipSize = 0
packetCount = 0