Certainly! To achieve your goal of using hardware acceleration with h264_qsv by default and switching to software encoding with libx264 when processing files with the vc-1 codec, you'll need to modify your script to detect the codec of each input file and adjust the encoding parameters accordingly.
Here's how you can do it:
-
Use ffprobe to detect the codec of the input file.
You can run ffprobe within your script to extract the codec information of the input video stream. Here's how you can do it in PowerShell:
# Get the codec name of the input video
$codec = (& ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of csv=p=0 "$fullName").Trim()
This command runs ffprobe on $fullName (your input file) and extracts the codec name of the first video stream, trimming any whitespace.
-
Adjust your encoding parameters based on the codec.
Now that you have the codec name stored in $codec, you can use an if statement to set your encoding parameters:
# Decide whether to use hardware acceleration or not based on codec
if ($codec -eq "vc1") {
# For vc-1 codec, use software encoding with libx264
$hwaccelParam = ""
$global:VideoLibrary = "libx264"
} else {
# For other codecs, use hardware acceleration with h264_qsv
$hwaccelParam = "-hwaccel qsv -hwaccel_device auto"
$global:VideoLibrary = "h264_qsv"
}
Here, $hwaccelParam will either contain the hardware acceleration parameters or be empty, and $global:VideoLibrary is set to the appropriate encoder.
-
Modify your FFmpeg commands to use these parameters.
Use $hwaccelParam in your FFmpeg commands:
# Scan the file with FFMPEG
if ($global:LimitCPU) {
ffmpeg.exe -v error $hwaccelParam -i "$fullName" -max_muxing_queue_size 1024 -f null -threads $global:CPULimt - >"$ffmpegLog" 2>&1
} else {
ffmpeg.exe -v error $hwaccelParam -i "$fullName" -max_muxing_queue_size 1024 -f null - >"$ffmpegLog" 2>&1
}
By incorporating $hwaccelParam, the script will include hardware acceleration parameters only when appropriate.
-
Use $global:VideoLibrary in your encoding step.
When you get to the encoding/transcoding part of your script, ensure that $global:VideoLibrary is used as your video encoder:
ffmpeg.exe -i "$fullName" [other parameters] -c:v $global:VideoLibrary [additional parameters] "$outputFile"
This way, the encoder used will switch between h264_qsv and libx264 based on the input codec.
Full Modified Script Snippet:
Here's how the modified section of your script would look:
# Get the codec name of the input video
$codec = (& ffprobe -v error -select_streams v:0 -show_entries stream=codec_name -of csv=p=0 "$fullName").Trim()
# Decide whether to use hardware acceleration or not based on codec
if ($codec -eq "vc1") {
# For vc-1 codec, use software encoding with libx264
$hwaccelParam = ""
$global:VideoLibrary = "libx264"
} else {
# For other codecs, use hardware acceleration with h264_qsv
$hwaccelParam = "-hwaccel qsv -hwaccel_device auto"
$global:VideoLibrary = "h264_qsv"
}
# Remove old FFMPEG log if it exists
if (Test-Path $ffmpegLog) {
try {
Remove-Item -LiteralPath "$ffmpegLog" -Force
} catch {
$ErrorMessage = $_.Exception.Message
LogWrite "Error while removing old FFMPEG Log: $ErrorMessage" -Colour "Red"
LogWrite "Scan cannot continue without the ability to modify: $ffmpegLog"
Exit 1
}
}
# Scan the file with FFMPEG
if ($global:LimitCPU) {
ffmpeg.exe -v error $hwaccelParam -i "$fullName" -max_muxing_queue_size 1024 -f null -threads $global:CPULimt - >"$ffmpegLog" 2>&1
} else {
ffmpeg.exe -v error $hwaccelParam -i "$fullName" -max_muxing_queue_size 1024 -f null - >"$ffmpegLog" 2>&1
}
# Continue with your script...
Explanation:
-
Codec Detection: The script uses ffprobe to detect the codec of the input file. The -select_streams v:0 option selects the first video stream, and -show_entries stream=codec_name retrieves the codec name.
-
Conditional Parameters: Based on the codec, the script sets $hwaccelParam to either the hardware acceleration parameters or an empty string. It also sets $global:VideoLibrary to the appropriate encoder.
-
FFmpeg Command Adjustment: The script includes $hwaccelParam in the FFmpeg command, so it uses hardware acceleration only when appropriate.
-
Encoding Step: Ensure that wherever you perform the actual encoding/transcoding, you use $global:VideoLibrary as your video encoder.
Additional Notes:
-
Ensure ffprobe and ffmpeg are in Your PATH: The script assumes that ffprobe.exe and ffmpeg.exe are available in your system's PATH. If they are not, you will need to provide the full path to these executables.
-
Error Handling: The script includes error handling when removing the old FFMPEG log.
-
Variables: Ensure that all variables like $fullName, $ffmpegLog, and $global:CPULimt are properly defined in your script prior to this snippet.
By making these adjustments, your script will now:
- Use hardware acceleration with
h264_qsv for all files except those with the vc-1 codec.
- Automatically switch to software encoding with
libx264 when processing vc-1 files.
- Revert back to hardware acceleration for other files.
Let me know if you need further assistance or clarification on any part of this solution!