Skip to content

Instantly share code, notes, and snippets.

@crazo7924
Created August 24, 2025 10:38
Show Gist options
  • Select an option

  • Save crazo7924/c49e1d35da7f87cb8b7fe062e909c351 to your computer and use it in GitHub Desktop.

Select an option

Save crazo7924/c49e1d35da7f87cb8b7fe062e909c351 to your computer and use it in GitHub Desktop.
# Get the collection of files.
$files = Get-ChildItem -Path '.' -Filter '*.opus'
$total = $files.Count
# No need for the synchronized hashtable as it was unused.
Write-Progress -Activity "opus -> m4a + fixing date..." -Status "Starting..." -PercentComplete 0
# Counter for processed items.
$processedCount = 0
# ForEach-Object -Parallel returns output to the pipeline. We can process this output.
$files | ForEach-Object -ThrottleLimit 8 -Parallel {
# It's good practice to wrap external command calls in a try/catch
# block to handle potential errors gracefully within a parallel loop.
try {
$file = $_
# Capture the output of ffprobe.
# Directly casting to [string] is simpler than Out-String.
$original_date = [string](ffprobe -v quiet -of default=noprint_wrappers=1:nokey=1 -show_entries stream_tags=date "$file")
# Modify the date format if it exists.
if ($original_date) {
# Trim any whitespace or newlines.
$original_date = $original_date.Trim()
# This assumes the date format is YYYYMMDD.
$modified_date = ($original_date.Insert(4, '-')).Insert(7, '-')
} else {
$modified_date = ""
}
$target = $file.BaseName + ".m4a"
# Run ffmpeg.
ffmpeg -v quiet -i "$file" -c:a aac -map_metadata 0:s:a:0 -metadata "date=$modified_date" "$target" -y
# Output a value to the pipeline to signal completion.
# Returning a boolean $true is slightly more efficient than a full string.
return $true
}
catch {
# Optionally, write errors to the console from the parallel thread.
Write-Error "Failed to process '$($file.Name)': $_"
}
} | ForEach-Object {
# This block runs sequentially in the main session.
# Increment the counter for each item received from the parallel block.
$processedCount++
$percent = (($processedCount / $total) * 100)
Write-Progress -Activity "opus -> m4a + fixing date..." -Status "Processed $processedCount of $total files" -PercentComplete $percent
}
# Final status update.
Write-Progress -Activity "opus -> m4a + fixing date..." -Status "Done!" -Completed
@crazo7924
Copy link
Author

Experimental!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment