yt-dlp move | handbrake script

This commit is contained in:
lovebird 2025-03-13 12:25:21 +01:00
parent b3ef5683f0
commit be98cd1942
61 changed files with 226 additions and 0 deletions

View File

@ -0,0 +1,116 @@
<#
.SYNOPSIS
Converts video files using HandBrakeCLI.
.DESCRIPTION
This script processes video files matching a specified pattern using HandBrakeCLI with a specified preset.
It creates output files with "-converted" added to the original filenames.
.PARAMETER InputPattern
Glob pattern to match input video files (e.g., "C:\Videos\*.mp4").
.PARAMETER Preset
HandBrake preset to use for conversion. Defaults to "Fast 1080p30".
.EXAMPLE
.\Convert-Videos.ps1 -InputPattern "C:\Videos\*.mp4"
Converts all MP4 files in C:\Videos using the default preset.
.EXAMPLE
.\Convert-Videos.ps1 -InputPattern "C:\Videos\*.mkv" -Preset "Very Fast 720p30"
Converts all MKV files in C:\Videos using the "Very Fast 720p30" preset.
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $true, Position = 0, HelpMessage = "Glob pattern for input video files")]
[ValidateNotNullOrEmpty()]
[string]$InputPattern,
[Parameter(Mandatory = $false, Position = 1, HelpMessage = "HandBrake preset to use")]
[string]$Preset = "Fast 1080p30"
)
function Test-CommandExists {
param (
[Parameter(Mandatory = $true)]
[string]$Command
)
$exists = $null -ne (Get-Command -Name $Command -ErrorAction SilentlyContinue)
return $exists
}
# Display script banner
Write-Host "Video Conversion Script" -ForegroundColor Cyan
Write-Host "======================" -ForegroundColor Cyan
Write-Host "Input Pattern: $InputPattern" -ForegroundColor Green
Write-Host "Using Preset: $Preset" -ForegroundColor Green
Write-Host ""
# Check if HandBrakeCLI is available
if (-not (Test-CommandExists "HandBrakeCLI")) {
Write-Error "HandBrakeCLI is not found in PATH. Please install HandBrake CLI and ensure it's in your PATH."
exit 1
}
# Get matching files
try {
$files = Get-Item -Path $InputPattern -ErrorAction Stop
$fileCount = ($files | Measure-Object).Count
if ($fileCount -eq 0) {
Write-Warning "No files found matching the pattern: $InputPattern"
exit 0
}
Write-Host "Found $fileCount files to process." -ForegroundColor Yellow
}
catch {
Write-Error "Error finding files: $_"
exit 1
}
# Process each file
$currentFile = 0
foreach ($file in $files) {
$currentFile++
$percentComplete = [math]::Round(($currentFile / $fileCount) * 100)
# Create output filename by adding "-converted" before the extension
$directory = [System.IO.Path]::GetDirectoryName($file.FullName)
$filename = [System.IO.Path]::GetFileNameWithoutExtension($file.Name)
$extension = [System.IO.Path]::GetExtension($file.Name)
$outputFile = Join-Path -Path $directory -ChildPath "$filename-converted$extension"
Write-Progress -Activity "Converting Videos" -Status "Processing $($file.Name)" -PercentComplete $percentComplete
Write-Host "[$currentFile/$fileCount] Converting: $($file.Name)" -ForegroundColor Cyan
try {
# Run HandBrakeCLI
$handbrakeArgs = @(
"-i", "`"$($file.FullName)`"",
"-o", "`"$outputFile`"",
"--preset", "`"$Preset`""
)
Write-Host " Command: HandBrakeCLI $($handbrakeArgs -join ' ')" -ForegroundColor DarkGray
$process = Start-Process -FilePath "HandBrakeCLI" -ArgumentList $handbrakeArgs -NoNewWindow -PassThru -Wait
if ($process.ExitCode -eq 0) {
Write-Host " Success: Created $outputFile" -ForegroundColor Green
} else {
Write-Host " Failed: HandBrakeCLI returned exit code $($process.ExitCode)" -ForegroundColor Red
}
}
catch {
Write-Host " Error processing $($file.Name): $_" -ForegroundColor Red
}
Write-Host ""
}
Write-Progress -Activity "Converting Videos" -Completed
Write-Host "Conversion process completed. Processed $fileCount files." -ForegroundColor Green

View File

@ -0,0 +1,104 @@
#!/bin/bash
# convert-videos.sh - Convert videos using HandBrakeCLI
# Usage: ./convert-videos.sh "input_pattern" [--preset "preset_name"]
# Exit on error, undefined variable, or pipe failure
set -euo pipefail
# Default values
PRESET="Fast 1080p30"
INPUT_PATTERN=""
TOTAL_FILES=0
CURRENT_FILE=0
# Print usage information
usage() {
echo "Usage: $(basename "$0") INPUT_PATTERN [--preset PRESET]"
echo
echo "Arguments:"
echo " INPUT_PATTERN Glob pattern for input files (e.g., '*.mp4' or 'videos/*.mkv')"
echo " --preset PRESET HandBrake preset to use (default: 'Fast 1080p30')"
echo
echo "Example:"
echo " $(basename "$0") '*.mp4'"
echo " $(basename "$0") 'videos/*.mkv' --preset 'Very Fast 720p30'"
exit 1
}
# Parse command line arguments
if [[ $# -lt 1 ]]; then
usage
fi
INPUT_PATTERN="$1"
shift
while [[ $# -gt 0 ]]; do
case "$1" in
--preset)
if [[ $# -gt 1 ]]; then
PRESET="$2"
shift 2
else
echo "Error: --preset requires a value" >&2
usage
fi
;;
*)
echo "Error: Unknown argument: $1" >&2
usage
;;
esac
done
# Check if HandBrakeCLI is installed
if ! command -v HandBrakeCLI &> /dev/null; then
echo "Error: HandBrakeCLI is not installed or not in your PATH" >&2
echo "Please install HandBrake CLI from https://handbrake.fr/downloads.php" >&2
exit 1
fi
# Expand the input pattern and count files
mapfile -t FILES < <(find . -path "$INPUT_PATTERN" -type f 2>/dev/null || find "$INPUT_PATTERN" -type f 2>/dev/null || echo "$INPUT_PATTERN")
TOTAL_FILES=${#FILES[@]}
if [[ $TOTAL_FILES -eq 0 ]]; then
echo "Error: No files match the pattern '$INPUT_PATTERN'" >&2
exit 1
fi
echo "Found $TOTAL_FILES file(s) to convert using preset '$PRESET'"
echo
# Process each file
for input_file in "${FILES[@]}"; do
((CURRENT_FILE++))
# Check if file exists and is readable
if [[ ! -f "$input_file" || ! -r "$input_file" ]]; then
echo "Warning: Cannot access file: $input_file - skipping"
continue
fi
# Generate output filename with "-converted" suffix
filename=$(basename -- "$input_file")
extension="${filename##*.}"
filename="${filename%.*}"
dirname=$(dirname -- "$input_file")
output_file="$dirname/$filename-converted.$extension"
# Display progress
echo -e "[$CURRENT_FILE/$TOTAL_FILES] Converting: $input_file"
echo -e " Output to: $output_file"
# Run HandBrakeCLI
HandBrakeCLI --preset="$PRESET" --input="$input_file" --output="$output_file" || {
echo "Error: Failed to convert $input_file" >&2
continue
}
echo -e "Completed: $output_file\n"
done
echo "Conversion complete! Processed $CURRENT_FILE of $TOTAL_FILES files."

6
packages/media/ref/yt-dlp/.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
/node_modules
/coverage
*.log
.DS_Store
clear_history.sh
./tests/assets