Convert-VideoToHEVC

PowerShell HEVC Video Converter

📋 Table of Contents

Overview

Main HEVC conversion script with batch processing and quality presets

Synopsis

Converts video files to HEVC (H.265) format using FFmpeg with advanced options, batch processing, and robust error handling.

Description

This script automates the conversion of various video formats (MKV, MP4, MOV, WMV, AVI, FLV, M4V)
to HEVC (H.265) using the libx265 encoder. It supports preset profiles for different content types and robust error handling.

Key features include:
- Preset profiles for different content types (Animation, Film, ScreenCapture, etc.)
- Customizable quality (CRF) and encoding preset.
- Optional interactive batch processing to convert files in chunks.
- Comprehensive error handling with detailed logging to a file and console.
- Dry-run mode using the -WhatIf common parameter to preview operations without making changes.
- Performance metrics and a summary report upon completion.
- Automatic detection and exclusion of already converted files.
- Ensures FFmpeg and FFprobe are available in the system's PATH before execution.
- Original files are moved to the output folder upon successful conversion, and failed conversions are moved to a 'failed' subfolder for troubleshooting.

Parameters

-Profile

Specifies a predefined encoding profile to use for conversion

-inputFolder

Specifies the path to the folder containing video files to be converted.
Defaults to the current directory where the script is executed.

-outputFolder

Specifies the path where converted HEVC video files will be saved.
A 'converted' subfolder will be created in the current directory by default if not specified.

-batchSize

Specifies the number of files to process in each batch. The script will pause and prompt for
continuation after each batch. If not specified, all files will be processed at once.

-crf

Constant Rate Factor (CRF) value for libx265 encoding.
Lower values result in higher quality and larger file sizes.
Acceptable range is 0 (lossless) to 51 (lowest quality). Default is 28.

-preset

Encoding preset for libx265. Controls the trade-off between encoding speed and compression efficiency.
Slower presets generally yield better compression and quality for a given CRF, but take longer.
Valid options: ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow, placebo.
Default is "medium".

-DebugMode

A switch parameter that, when present, enables verbose debugging output to the console
and detailed entries in the log file.

-LogToFile

A switch parameter that, when present, writes all console output to a separate log file.

-ProfileName

The name of the profile to load (e.g., "Animation" for "Animation.json").

.RETURNS
[PSObject] An object containing the profile's settings if successfully loaded;
otherwise, returns $null.

-Message

The message to log.

-ForegroundColor

Console text color (default: White).

-Message

The debug message string to be logged. This parameter is mandatory.

-Size

The file size value, specified in bytes. This parameter expects a long integer.

.RETURNS
[string] A formatted string representing the file size (e.g., "1.23 GB").

-FilePath

The full path to the media file for which information is to be retrieved.
This parameter is mandatory.

.RETURNS
[PSObject] An object containing the parsed media stream information (e.g.,
'codec_name', 'width', 'height', 'duration', 'bit_rate'); returns $null on failure.

Examples

Example 1

.\Convert-VideoToHEVC.ps1 -Profile Animation

Example 2

.\Convert-VideoToHEVC.ps1 -inputFolder "C:\MyVideos" -outputFolder "C:\ConvertedVideos" -crf 20 -preset slow

Example 3

.\Convert-VideoToHEVC.ps1 -batchSize 5 -crf 25 -preset fast

Example 4

.\Convert-VideoToHEVC.ps1 -DebugMode -LogToFile

Example 5

.\Convert-VideoToHEVC.ps1 -inputFolder ".\Source" -outputFolder ".\Output" -WhatIf

Example 6

Write-DebugInfo "Variable value: $myVariable"

Example 7

Format-FileSize -Size 1024
# Output: 1.00 KB

Example 8

Format-FileSize -Size 1073741824
# Output: 1.00 GB