Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/root/powershell/pwsh
- # Change the above if your pwsh resides elsewhere in Linux
- <#
- caps.ps1 -file <file> [-burn]
- Requires: ffmpeg/ffprobe in the system path otherwise edit in the full path to them.
- PowerShell 7+ (Any lesser version and it will just exit)
- It uses the following fonts at lines 65, 68, & 70 - change those lines to use another font:
- Windows font: C:\Windows\fonts\arial.ttf
- Linux font: /usr/share/fonts/truetype/dejavu/DejaVuSans.ttf
- MacOs font: /System/Library/Fonts/Arial.ttf
- *** NOTE: ONLY tested with:
- Windows 10 Pro x64
- DietPi v 6.33.3 (Raspbian GNU/Linux 10 (buster), Linux 5.4.79-v7l+ )
- Ubuntu 18.04.5 LTS (WSL)
- PowerShell 7.1.0 (Windows/Linux)
- ffmpeg version 2020-12-06-git-2aab42bc40-full_build-www.gyan.dev (Windows)
- ffmpeg version 4.1.6-1~deb10u1+rpt1 (RasPi - DietPi v6.33.3 - Raspbian GNU/Linux 10 (buster), Linux 5.4.79-v7l+ )
- Where: file = full path to the video file
- -burn = [optional] burn in timecodes
- *** NOTE: Burning timecodes will not work unless ffmpeg has been compiled with the following option:
- --enable-libfreetype
- If you don't know then enter the following command and search for 'drawtext' in the output:
- ffmpeg -filters
- Generates the contact sheet faster than CTBRec internal method and [optionally] burns in the timecode
- Extreme example: Video 3840x2160, 17.6GB, 2h54m (Ryzen5 3600)
- CTBRec internal: 15 mins 16 secs
- Powershell script: 44 secs
- #>
- # Parse command line parameters
- param (
- [Parameter(Mandatory=$true)][string]$file,
- [switch]$burn
- )
- function ContactSheet ($infile) {
- $tmpdir = ([System.IO.Path]::GetTempPath() + '~' + ([System.IO.Path]::GetRandomFileName())).Split('.')[0]
- New-Item -Path "$($tmpdir)" -ItemType Directory | Out-Null
- $duration = $(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 -i "$($infile)")
- for ($i = 1; $i -lt 57; $i++) {
- $interval = [int](($i-0.5) * $duration/56) # calculate position of each frame capture
- [string]$ttext = ([timespan]::fromseconds($interval).tostring()).Replace(':', '\:') # seconds to xx:xx:xx
- if ($burn) { # skip to calculated position and grab next key frame
- $ffcmd = "ffmpeg -v quiet -y -skip_frame nokey -ss $interval -i `"$file`" -vf scale=-1:720,select=`"eq(pict_type\,I)`",drawtext=`"text='$ttext':fontcolor=white:fontsize=48:box=1:boxcolor=black@0.5:boxborderw=5:x=(w-text_w)/2:y=(h-text_h-10):fontfile=$ffont`" -vframes 1 `"$tmpdir/image$(([string]$i).PadLeft(2, '0')).png`""
- } else {
- $ffcmd = "ffmpeg -v quiet -y -skip_frame nokey -ss $interval -i `"$file`" -vf select=`"eq(pict_type\,I)`" -vframes 1 `"$tmpdir/image$(([string]$i).PadLeft(2, '0')).png`""
- }
- Invoke-Expression $ffcmd
- }
- $ffcmd = "ffmpeg -v quiet -y -i $tmpdir/image%02d.png -vf `"scale=317:-1,tile=8x7:color=0x333333:margin=2:padding=2,scale=2560:-1`" -qscale:v 3 `"./$(([System.IO.FileInfo]$infile).BaseName).jpg`""
- Invoke-Expression $ffcmd # create contact sheet from images -> file.jpg
- Remove-Item -Path "$($tmpdir)" -Recurse -Force # remove images
- }
- if ($PSVersionTable.PSVersion.Major -ge 7) { # Check for PS 7+
- if ($IsWindows) {
- $ffont = "C\\:/Windows/fonts/arial.ttf" # ffmpeg font syntax changes depending on compilation/version, this works with the version I tested it with
- } else {
- if ($IsLinux) {
- $ffont = "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf"
- } else {
- $ffont = "/System/Library/Fonts/Arial.ttf" # assume MacOS - change this if necessary, I haven't got access to a Mac
- }
- }
- } else {
- exit
- }
- Push-Location "$(Split-Path $file -Parent)"
- ContactSheet "$(Split-Path $file -Leaf)"
- Pop-Location
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement