Skip to content

Instantly share code, notes, and snippets.

@jfalava
Created May 11, 2024 10:27
Show Gist options
  • Save jfalava/6ecb10107c5be302b6973ecabde71ec5 to your computer and use it in GitHub Desktop.
Save jfalava/6ecb10107c5be302b6973ecabde71ec5 to your computer and use it in GitHub Desktop.
FFmpeg flac to m4a for Apple Music Lossless scripts

Convert music files to .m4a

One file

ffmpeg -i <ogfile> -c:v copy -c:a alac <newfile>.m4a

All the files

Powershell

# Set the path to the folder where the script is located
$scriptPath = (Get-Item -Path $MyInvocation.MyCommand.Path).Directory.FullName

# Set the path for the new 'm4a' folder
$m4aFolderPath = Join-Path -Path $scriptPath -ChildPath 'm4a'

# Create the 'm4a' folder if it doesn't exist
if (-not (Test-Path -Path $m4aFolderPath -PathType Container)) {
    New-Item -Path $m4aFolderPath -ItemType Directory | Out-Null
}

# Get all the FLAC files in the current folder
$flacFiles = Get-ChildItem -Path $scriptPath -Filter *.flac

# Loop through each FLAC file and convert it to ALAC
foreach ($flacFile in $flacFiles) {
    $ogFileName = $flacFile.BaseName
    $newFileName = "$($ogFileName).m4a"
    
    # Run ffmpeg command
    & ffmpeg -i "$($flacFile.FullName)" -c:v copy -c:a alac "$($newFileName)"
    
    # Move the converted file to the 'm4a' folder
    Move-Item -Path $newFileName -Destination $m4aFolderPath
}

Write-Host "Conversion and move completed."

Bash

#!/bin/bash

# Set the path to the folder where the script is located
scriptPath=$(dirname "$(readlink -f "$0")")

# Set the path for the new 'm4a' folder
m4aFolderPath="$scriptPath/m4a"

# Create the 'm4a' folder if it doesn't exist
if [ ! -d "$m4aFolderPath" ]; then
  mkdir "$m4aFolderPath"
fi

# Get all the FLAC files in the current folder
flacFiles=("$scriptPath"/*.flac)

# Loop through each FLAC file and convert it to ALAC
for flacFile in "${flacFiles[@]}"; do
  ogFileName=$(basename "$flacFile" .flac)
  newFileName="${ogFileName}.m4a"
  
  # Run ffmpeg command
  ffmpeg -i "$flacFile" -c:v copy -c:a alac "$newFileName"
  
  # Move the converted file to the 'm4a' folder
  mv "$newFileName" "$m4aFolderPath/"
done

echo "Conversion and move completed."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment