• AnyStream is having some DRM issues currently, Netflix is not available in HD for the time being.
    Situations like this will always happen with AnyStream: streaming providers are continuously improving their countermeasures while we try to catch up, it's an ongoing cat-and-mouse game. Please be patient and don't flood our support or forum with requests, we are working on it 24/7 to get it resolved. Thank you.

Guide Automated Video Bitrate Analysis and Graph Generation Script

tectpro

Translator (ms_MY)
Thread Starter
Joined
Feb 27, 2011
Messages
1,439
Likes
968
I have created a PowerShell script that automates analyzing video files to extract bitrate information.
The script then generates a visual graph displaying the bitrate over time for each video file.
This script is handy for those who need to visually inspect the bitrate fluctuations of video files without manually manipulating data or creating graphs in tools like Excel.

Required Tools:
  1. CheckBitrate: This tool analyses the video files and extracts bitrate information.
    The output is saved in a CSV file format, with each line representing a point in time and the corresponding bitrate value.
    Code:
     https://github.com/rigaya/CheckBitrate/releases

  2. Gnuplot: A portable command-line-driven graphing utility that can generate graphs from the CSV files created by CheckBitrate.
    The graphs are saved as PNG images, providing a clear visual representation of the bitrate over time.
    Code:
     http://www.gnuplot.info/download.html

Script Overview:

The script scans the directory from which it is run for video files (currently set up to look for .mp4, .mkv, .ts and .m2ts files) and runs CheckBitrate on each file to generate a CSV file with bitrate data.
It then uses Gnuplot to create a graph from each CSV file, plotting the bitrate over time. The generated graph is saved as a PNG image with the same base name as the video file, making it easy to associate each graph with its corresponding video.


Key Points:
  • Customization: To match their installation locations, users must adjust the paths to the CheckBitrate and Gnuplot executables within the script.

  • CSV Files: The script does not delete the CSV files generated by CheckBitrate, allowing users to review or analyze the raw data if needed.

  • Output: The script outputs a PNG file with a graph of the video's bitrate over time for each video file processed. This file is saved in the same directory as the video file and shares its base name, making it easy to identify.

  • Execution Note: Please avoid running this script in directories on the C: drive, as I've encountered some odd issues in those cases. It's best to use a different drive or an external storage device.
Previous Version: In the past, I provided a similar solution as a batch script, where users had to manually create graphs using tools like Excel by opening the CSV files.
With this updated script, manual graph creation is no longer necessary, as the script automatically generates the graphs and saves them as PNG images.

I hope this script will be a helpful tool for anyone who needs to analyze video bitrate information more efficiently.
You are welcome to customize it to suit your specific needs, and I am open to any feedback or suggestions for improvement.



Sample screenshot of the Powershell script at work.

1709782118104.png


Sample screenshots of generated graphs:

1709782268371.png

1709782307120.png

1709782698527.png


Here is a sample of the content in CSV format:

Code:
,kbps,kbps(avg)
     0.000,44552.30,44552.30
     1.042,38911.37,41731.84
     2.083,57353.98,46939.22
     3.125,83807.45,56156.28
     4.167,46661.12,54257.24
     5.208,39479.89,51794.35
     6.250,41913.79,50382.84
     7.292,15669.29,46043.65
     8.333,28001.46,44038.96
     9.375,40653.60,43700.43
    10.417,60424.81,45220.82
    11.458,63728.03,46763.09
    12.500,57443.52,47584.66
    13.542,59424.22,48430.35
    14.583,72104.03,50008.59
    15.625,60414.63,50658.97
    16.667,55882.87,50966.26
    17.708,54529.31,51164.20
    18.750,26433.53,49862.59
    19.792,25200.15,48629.47
    20.833,23856.77,47449.82
    21.875,9445.10,45722.33
    22.917,10935.48,44209.86
    23.958,27377.13,43508.49
    25.000,33660.75,43114.58
    26.042,44560.87,43170.21
    27.083,38372.83,42992.53
    28.125,50320.04,43254.23
    29.167,56234.04,43701.81
    30.208,47701.60,43835.13
    31.250,55262.78,44203.77
    32.292,55919.05,44569.87
    33.333,48748.96,44696.51
    34.375,48493.28,44808.18
    35.417,23780.82,44207.40
    36.458,68887.61,44892.96
    37.500,53625.45,45128.97
    38.542,68141.73,45734.57
    39.583,64498.01,46215.68
    40.625,49233.89,46291.14
    41.667,108280.10,47803.06
    42.708,976.48,46688.15
    43.750,123.82,45605.25
    44.792,943.01,44590.20
    45.833,117.24,43601.92
    46.875,123.59,42842.47



PowerShell Script:

Please copy the script below into a new Txt file and rename it to, for example, scanbitrate.ps1

Code:
# Clear the console at the beginning of the script
Clear-Host

# Define the path to the CheckBitrate tool
$checkBitratePath = "E:\rigaya\CheckBitrate\CheckBitrate.exe"
# Define the full path to gnuplot executable
$gnuplotPath = "C:\Program Files\gnuplot\bin\gnuplot.exe"

# Get the current directory where the script is executed
$videoFolder = Get-Location

# Scan all video files in the current directory for specified extensions
Get-ChildItem -Path $videoFolder -File | Where-Object { $_.Extension -match "\.(mp4|mkv|ts|m2ts)$" } | ForEach-Object {
    $videoFile = $_.FullName
    $baseName = $_.BaseName

    Write-Host "`nProcessing file: $videoFile" -ForegroundColor Yellow
    Write-Host "Scanning with CheckBitrate..." -ForegroundColor Magenta

    # Run CheckBitrate and wait for it to complete
    & $checkBitratePath -i 1 "$videoFile"
    Start-Sleep -Seconds 2  # Adjust sleep time as needed

    # Find the CSV file generated by CheckBitrate
    $csvFile = Get-ChildItem -Path "$videoFolder\${baseName}*.bitrate.csv" | Select-Object -First 1
    if (-not $csvFile) {
        Write-Host "No CSV file found for $videoFile. Skipping graph generation." -ForegroundColor Red
        Continue
    }
    $csvPath = $csvFile.FullName
    $plotPath = $csvFile.FullName.Replace(".bitrate.csv", ".png")

    # Check if the CSV file contains data
    if ((Get-Content $csvPath).Count -le 1) {
        Write-Host "No data found in CSV file for $videoFile. Skipping graph generation." -ForegroundColor Red
        Continue
    }

    # Prepare gnuplot script
    $gnuplotCommands = @"
set terminal pngcairo size 1920,1080 enhanced font 'Verdana,10'
set output '$plotPath'
set title 'Bitrate Graph for $baseName'
set xlabel 'Time (s)'
set ylabel 'Bitrate (kbps)'
set datafile separator ','
set key outside right top vertical Right noreverse enhanced autotitles box linetype -1 linewidth 1.000
set grid
set autoscale
set style data lines
set style line 1 lt 1 lw 2 lc rgb '#0072bd'  # Bitrate line color
set style line 2 lt 1 lw 2 lc rgb '#d95319'  # Average Bitrate line color
plot '$csvPath' every ::1 using 1:2 with lines linestyle 1 title 'Bitrate', '$csvPath' every ::1 using 1:3 with lines linestyle 2 title 'Average Bitrate'
"@


    # Write commands to a temporary gnuplot script file
    $gnuplotScriptPath = [System.IO.Path]::GetTempFileName()
    $gnuplotCommands | Out-File -FilePath $gnuplotScriptPath -Encoding ASCII

    try {
        # Run gnuplot with the prepared script file
        & $gnuplotPath $gnuplotScriptPath
        if ($LASTEXITCODE -ne 0) {
            throw "Gnuplot exited with error code $LASTEXITCODE."
        }
        Write-Host "Graph generated and saved to $plotPath" -ForegroundColor Cyan
    }
    catch {
        Write-Host "Failed to generate graph for $videoFile. Error: $_" -ForegroundColor Red
    }
    finally {
        # Clean up the temporary script file
        Remove-Item -Path $gnuplotScriptPath -ErrorAction SilentlyContinue
    }

    Write-Host "-------------------------------------`n"
}

Write-Host "Graph generation complete. Press Enter to exit." -ForegroundColor Green
$null = Read-Host
 
Example screenshot of a video with a high bitrate.

1709792717101.png
 
Back
Top