Last active
September 18, 2024 10:21
-
-
Save maksii/5d11166c346417459b6b5b350ea6ae0c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ask for the folder path | |
$folderPath = Read-Host "Enter the full path to the folder containing audio files (e.g., 'C:\Media\Folder')" | |
# Ask for the desired audio format | |
$audioFormat = Read-Host "Enter the desired audio format (e.g., 'aac', 'mp3', 'wav')" | |
# Ask for the desired bitrate | |
$bitrate = Read-Host "Enter the desired bitrate (e.g., '192k')" | |
# Ensure FFmpeg is available | |
$ffmpegPath = "ffmpeg" # Assumes FFmpeg is in the system PATH | |
# Validate inputs | |
if (-not (Test-Path $folderPath)) { | |
Write-Host "The specified folder does not exist." | |
exit | |
} | |
if (-not $audioFormat) { | |
Write-Host "Please provide a valid audio format." | |
exit | |
} | |
if (-not $bitrate) { | |
Write-Host "Please provide a valid bitrate." | |
exit | |
} | |
# Remove quotes from folder path if present | |
$folderPath = $folderPath.Trim('"') | |
# Create a subfolder named 'converted' if it doesn't exist | |
$convertedFolderPath = Join-Path -Path $folderPath -ChildPath "converted" | |
if (-not (Test-Path $convertedFolderPath)) { | |
New-Item -ItemType Directory -Path $convertedFolderPath | Out-Null | |
} | |
# Supported audio file extensions | |
$audioExtensions = @(".mp3", ".wav", ".flac", ".aac", ".ogg", ".m4a") | |
# Get audio files in the folder | |
$audioFiles = Get-ChildItem -Path $folderPath -File | Where-Object { | |
$audioExtensions -contains $_.Extension.ToLower() | |
} | |
# Process each audio file | |
foreach ($file in $audioFiles) { | |
$inputFilePath = $file.FullName | |
$outputFileName = [System.IO.Path]::ChangeExtension($file.Name, $audioFormat) | |
$outputFilePath = Join-Path -Path $convertedFolderPath -ChildPath $outputFileName | |
# Build FFmpeg command to convert audio | |
$ffmpegCommand = "$ffmpegPath -i `"$inputFilePath`" -b:a $bitrate `"$outputFilePath`"" | |
# Execute FFmpeg command | |
Write-Host "Processing $($file.Name)..." | |
Start-Process -NoNewWindow -Wait -FilePath "cmd.exe" -ArgumentList "/c $ffmpegCommand" | |
# Update progress | |
Write-Host "Completed processing $($file.Name)." | |
} | |
# Completion Message | |
Write-Host "All files converted. Press Enter to exit." | |
Read-Host |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ask for the folder path | |
$folderPath = Read-Host "Enter the full path to the folder containing media files (e.g., \"C:\Media\Folder\")" | |
# Ask for start and end times | |
$endTime = Read-Host "Enter the end time for trimming (format HH:MM:SS, e.g., 00:02:30)" | |
# Ensure FFmpeg is available | |
$ffmpegPath = "ffmpeg" # Assumes FFmpeg is in the system PATH | |
# Validate inputs | |
if (-not (Test-Path $folderPath)) { | |
Write-Host "The specified folder does not exist." | |
exit | |
} | |
if (-not $endTime) { | |
Write-Host "Please provide the end time in the format HH:MM:SS (e.g., 00:02:30 for 2 minutes and 30 seconds)." | |
exit | |
} | |
# Remove quotes from folder path if present | |
$folderPath = $folderPath.Trim('"') | |
# Create a subfolder named 'trim' if it doesn't exist | |
$trimFolderPath = Join-Path -Path $folderPath -ChildPath "trim" | |
if (-not (Test-Path $trimFolderPath)) { | |
New-Item -ItemType Directory -Path $trimFolderPath | Out-Null | |
} | |
# Supported media file extensions | |
$mediaExtensions = @(".mp4", ".mkv", ".mov", ".avi", ".mp3", ".wav", ".flac") | |
# Get media files in the folder | |
$mediaFiles = Get-ChildItem -Path $folderPath -File | Where-Object { | |
$mediaExtensions -contains $_.Extension.ToLower() | |
} | |
# Process each media file | |
foreach ($file in $mediaFiles) { | |
$inputFilePath = $file.FullName | |
$outputFilePath = Join-Path -Path $trimFolderPath -ChildPath $file.Name | |
# Build FFmpeg command to trim after the end time | |
$ffmpegCommand = "$ffmpegPath -i `"$inputFilePath`" -ss $endTime -c copy `"$outputFilePath`"" | |
# Execute FFmpeg command | |
Write-Host "Processing $($file.Name)..." | |
Start-Process -NoNewWindow -Wait -FilePath "cmd.exe" -ArgumentList "/c $ffmpegCommand" | |
# Update progress | |
Write-Host "Completed processing $($file.Name)." | |
} | |
# Completion Message | |
Write-Host "All files processed. Press Enter to exit." | |
Read-Host |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ask for the folder path | |
$folderPath = Read-Host "Enter the full path to the folder containing media files (e.g., \"C:\Media\Folder\")" | |
# Ask for start and end times | |
$startTime = Read-Host "Enter the start time for trimming (format HH:MM:SS, e.g., 00:00:15)" | |
$endTime = Read-Host "Enter the end time for trimming (format HH:MM:SS, e.g., 00:23:40)" | |
# Ensure FFmpeg is available | |
$ffmpegPath = "ffmpeg" # Assumes FFmpeg is in the system PATH | |
# Validate inputs | |
if (-not (Test-Path $folderPath)) { | |
Write-Host "The specified folder does not exist." | |
exit | |
} | |
if (-not $startTime -or -not $endTime) { | |
Write-Host "Please provide both start and end times in the format HH:MM:SS (e.g., 00:01:30 for 1 minute and 30 seconds)." | |
exit | |
} | |
# Remove quotes from folder path if present | |
$folderPath = $folderPath.Trim('"') | |
# Create a subfolder named 'trim' if it doesn't exist | |
$trimFolderPath = Join-Path -Path $folderPath -ChildPath "trim" | |
if (-not (Test-Path $trimFolderPath)) { | |
New-Item -ItemType Directory -Path $trimFolderPath | Out-Null | |
} | |
# Supported media file extensions | |
$mediaExtensions = @(".mp4", ".mkv", ".mov", ".avi", ".mp3", ".wav", ".flac") | |
# Get media files in the folder | |
$mediaFiles = Get-ChildItem -Path $folderPath -File | Where-Object { | |
$mediaExtensions -contains $_.Extension.ToLower() | |
} | |
# Process each media file | |
foreach ($file in $mediaFiles) { | |
$inputFilePath = $file.FullName | |
$outputFilePath = Join-Path -Path $trimFolderPath -ChildPath $file.Name | |
# Build FFmpeg command | |
$ffmpegCommand = "$ffmpegPath -i `"$inputFilePath`" -ss $startTime -to $endTime -c copy `"$outputFilePath`"" | |
# Execute FFmpeg command | |
Write-Host "Processing $($file.Name)..." | |
Start-Process -NoNewWindow -Wait -FilePath "cmd.exe" -ArgumentList "/c $ffmpegCommand" | |
# Update progress | |
Write-Host "Completed processing $($file.Name)." | |
} | |
# Completion Message | |
Write-Host "All files processed. Press Enter to exit." | |
Read-Host |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ask for the folder path | |
$folderPath = Read-Host "Enter the full path to the folder containing media files (e.g., \"C:\Media\Folder\")" | |
# Ask for start and end times | |
$startTime = Read-Host "Enter the start time for trimming (format HH:MM:SS, e.g., 00:00:15)" | |
$endTime = Read-Host "Enter the end time for trimming (format HH:MM:SS, e.g., 00:23:40)" | |
# Ensure FFmpeg is available | |
$ffmpegPath = "ffmpeg" # Assumes FFmpeg is in the system PATH | |
# Validate inputs | |
if (-not (Test-Path $folderPath)) { | |
Write-Host "The specified folder does not exist." | |
exit | |
} | |
if (-not $startTime -or -not $endTime) { | |
Write-Host "Please provide both start and end times in the format HH:MM:SS (e.g., 00:01:30 for 1 minute and 30 seconds)." | |
exit | |
} | |
# Remove quotes from folder path if present | |
$folderPath = $folderPath.Trim('"') | |
# Create a subfolder named 'trim' if it doesn't exist | |
$trimFolderPath = Join-Path -Path $folderPath -ChildPath "trim" | |
if (-not (Test-Path $trimFolderPath)) { | |
New-Item -ItemType Directory -Path $trimFolderPath | Out-Null | |
} | |
# Supported media file extensions | |
$mediaExtensions = @(".mp4", ".mkv", ".mov", ".avi", ".mp3", ".wav", ".flac") | |
# Get media files in the folder | |
$mediaFiles = Get-ChildItem -Path $folderPath -File | Where-Object { | |
$mediaExtensions -contains $_.Extension.ToLower() | |
} | |
# Function to convert HH:MM:SS to seconds | |
function ConvertTo-Seconds($time) { | |
$parts = $time -split ":" | |
return [int]$parts[0] * 3600 + [int]$parts[1] * 60 + [int]$parts[2] | |
} | |
# Function to find the closest black screen time | |
function Find-ClosestBlackScreen($filePath, $targetTime) { | |
Write-Host "Analyzing black screen times for $($filePath)..." | |
$blackDetectCommand = "$ffmpegPath -i `"$filePath`" -vf blackdetect=d=0.1:pic_th=0.90 -an -f null - 2>&1" | |
$output = & cmd.exe /c $blackDetectCommand | |
$blackTimes = @() | |
foreach ($line in $output -split "`r`n") { | |
if ($line -match "black_start:(\d+\.\d+)") { | |
$blackTimes += [double]$matches[1] | |
} | |
} | |
$targetSeconds = ConvertTo-Seconds $targetTime | |
$closestTime = $blackTimes | Sort-Object { [math]::Abs($_ - $targetSeconds) } | Select-Object -First 1 | |
$closestTimeFormatted = [TimeSpan]::FromSeconds($closestTime).ToString("hh\:mm\:ss") | |
Write-Host "Closest black screen to $targetTime is at $closestTimeFormatted" | |
return $closestTimeFormatted | |
} | |
# Process each media file | |
foreach ($file in $mediaFiles) { | |
$inputFilePath = $file.FullName | |
$outputFilePath = Join-Path -Path $trimFolderPath -ChildPath $file.Name | |
# Find closest black screen times | |
Write-Host "Finding closest black screen times for $($file.Name)..." | |
$adjustedStartTime = Find-ClosestBlackScreen $inputFilePath $startTime | |
$adjustedEndTime = Find-ClosestBlackScreen $inputFilePath $endTime | |
# Build FFmpeg command | |
$ffmpegCommand = "$ffmpegPath -i `"$inputFilePath`" -ss $adjustedStartTime -to $adjustedEndTime -c copy `"$outputFilePath`"" | |
# Execute FFmpeg command | |
Write-Host "Processing $($file.Name) from $adjustedStartTime to $adjustedEndTime..." | |
Start-Process -NoNewWindow -Wait -FilePath "cmd.exe" -ArgumentList "/c $ffmpegCommand" | |
# Update progress | |
Write-Host "Completed processing $($file.Name)." | |
} | |
# Completion Message | |
Write-Host "All files processed. Press Enter to exit." | |
Read-Host |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment