Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
param (
[string]$src = ".",
[string]$dst = ".\reencode"
)
# Add-Type -AssemblyName System.Windows.Forms
function time($block) {
$sw = [Diagnostics.Stopwatch]::StartNew()
&$block
$sw.Stop()
$sw.Elapsed
}
function touch ($file) { new-item -force -ItemType "file" -path $file.toString() }
function encode ( $file, $dst ) {
# $file = get-childitem $file
$fn = $file.Name
$full = $file.fullname
$ext = $file.extension
$fd = $file.DirectoryName.Replace($pwd, "")
Write-Host "doin the conversion of $full, and it's going to $dst"
$inp = ".\inp-$fn"
$out = ".\out-$fn"
mkdir $inp
mkdir $out
$framerate = ./ffprobe -v error -select_streams v:0 -show_entries stream=avg_frame_rate -of default=noprint_wrappers=1:nokey=1 $full
$height = ./ffprobe -v error -select_streams v:0 -show_entries stream=height -of default=noprint_wrappers=1:nokey=1 $full
$width = ./ffprobe -v error -select_streams v:0 -show_entries stream=width -of default=noprint_wrappers=1:nokey=1 $full
$frame_height = 720.0 # 1080.0
$frame_width = 1280.0 # 1920.0
# $scale = [math]::min(1080.0/$height, 1920.0/$width) # full HD
$scale = [math]::min($frame_height/$height, $frame_width/$width) # not quite full HD
write-output "Framerate: $framerate`nHeight: $height`nWidth: $width`nScale: $scale"
$time = time {
& .\ffmpeg.exe -i $full -f image2 "$inp\seq-%09d.png"
}
write-output $time
Write-output "Enhancing images to $out. Scale is $scale"
$dur = time {
& .\waifu2x-cudnn\waifu2x-caffe-cui.exe -p "cudnn" -s $scale -n 3 -i $inp -o $out -model_dir ".\waifu2x-cudnn"
}
$dur = $dur.TotalSeconds
Write-Output "...took $dur seconds"
$img_height = ./ffprobe -v error -select_streams v:0 -show_entries stream=height -of default=noprint_wrappers=1:nokey=1 "$out\seq-000000001.png"
$img_width = ./ffprobe -v error -select_streams v:0 -show_entries stream=width -of default=noprint_wrappers=1:nokey=1 "$out\seq-000000001.png"
touch $dst
$time = time {
# add this for padding: https://ffmpeg.org/ffmpeg-filters.html#pad
# -vf "pad=width=$($frame_height):height=$($frame_width):x=$(calc_me):y=$(calc_me):color=black"
# -aspect $frame_width/$frame_height
#
# maybe this also:
# -filter:a loudnorm
& .\ffmpeg.exe -y -framerate $framerate -f image2 -i "$out\seq-%09d.png" -vcodec h264 -pix_fmt yuv420p "temp$ext"
& .\ffmpeg.exe -y -i "$full" -i "temp$ext" -map 1:v -c copy -map 0 -map -0:v $dst.toString()
}
write-output $time
# Write-Host "Press any key to continue ..."
# $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Remove-Item -path "$inp" -recurse
Remove-Item -path "$out" -recurse
# .\ffmpeg -i $file.fullname -vcodec copy -acodec copy -scodec copy -map 0 ".\REENCODE$fd\$fn" 24000/1001
}
$extns = "\.(mp4|mkv|avi|ogm)"
time {
$src = get-item $src
new-item -path $dst -ItemType "directory"
$dst = "$(get-item $dst)\"
$files = get-childitem -path $src -recurse
foreach ($f in $files) {
$extn = [IO.Path]::GetExtension($f)
if ($extn -match $extns) {
time {
$d = $f.fullname.toString().Replace($src.toString(), $dst.toString())
write-output "Encoding $f to $d"
encode -file $f -dst $d
}
}
}
Write-Output "Done!"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment