How to Transcode Files to FLAC
When adding files to a bug report, Research finds it helpful to have all of the files in FLAC format. You can use the code below to easily transcode an entire folder of audio/video files to FLAC.
Set-Up
-
To run this code, you will need to make sure that you have FFmpeg installed.
-
You will need to add a path to the input file folder.
-
Optionally, you can add a path to an output file folder. If there is no designated output folder, one will be created within the input folder.
NOTE: All FLAC files will have the same name as the original file.
Code
import os
import subprocess
from pathlib import Path
def get_audio_video_files(folder_path):
"""Get all audio and video files from the specified folder."""
# All file extensions supported by AssemblyAI API
valid_extensions = {
# Audio formats
'.3ga', '.8svx', '.aac', '.ac3', '.aif', '.aiff', '.alac',
'.amr', '.ape', '.au', '.dss', '.flv', '.m4a',
'.m4b', '.m4p', '.m4r', '.mp3', '.mpga', '.ogg', '.oga',
'.mogg', '.opus', '.qcp', '.tta', '.voc', '.wav', '.wma', '.wv',
# Video formats
'.webm', '.mts', '.m2ts', '.ts', '.mov', '.mp2', '.mp4',
'.m4v', '.mxf', '.mkv', '.avi', '.wmv'
}
files = []
for file in Path(folder_path).iterdir():
if file.is_file() and file.suffix.lower() in valid_extensions:
files.append(file)
return sorted(files)
def transcode_to_flac(input_file, output_file):
"""Transcode an audio/video file to FLAC format using ffmpeg."""
try:
# ffmpeg command with specified parameters
command = [
'ffmpeg',
'-i', str(input_file),
'-ar', '16000', # Sample rate 16kHz
'-sample_fmt', 's16', # Sample format: signed 16-bit
'-compression_level', '5', # FLAC compression level 5
str(output_file),
'-y' # Overwrite output file if it exists
]
# Run ffmpeg
result = subprocess.run(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
if result.returncode == 0:
return True, "Success"
else:
return False, result.stderr
except FileNotFoundError:
return False, "ffmpeg not found. Please install ffmpeg and add it to your PATH."
except Exception as e:
return False, str(e)
def main():
# ========================================
# PASTE YOUR FOLDER PATH HERE:
input_folder = r"<PATH-TO-INPUT-FOLDER>"
output_folder = r"<PATH-TO-OUTPUT-FOLDER>" # Or set to "" to use same as input
# ========================================
print("=" * 60)
print("Audio/Video to FLAC Transcoder")
print("=" * 60)
# Validate input folder
if not os.path.isdir(input_folder):
print(f"Error: Input folder does not exist: {input_folder}")
return
# Handle output folder
if not output_folder:
# Create output_files subfolder within input folder
output_folder = os.path.join(input_folder, "output_files")
print(f"Using same folder - creating subfolder: output_files")
if not os.path.isdir(output_folder):
try:
os.makedirs(output_folder, exist_ok=True)
print(f"Created output folder: {output_folder}")
except Exception as e:
print(f"Could not create folder: {e}")
return
print(f"\nInput folder: {input_folder}")
print(f"Output folder: {output_folder}")
# Get list of files
files = get_audio_video_files(input_folder)
if not files:
print("\nNo audio or video files found in the specified folder.")
return
print(f"\nFound {len(files)} file(s) to transcode:\n")
# Process each file
for i, input_file in enumerate(files, 1):
print(f"\n[{i}/{len(files)}] Processing: {input_file.name}")
# Use original filename with .flac extension
output_name = input_file.stem
output_file = Path(output_folder) / f"{output_name}.flac"
print(f"Transcoding to: {output_file.name}")
success, message = transcode_to_flac(input_file, output_file)
if success:
print(f"✓ Successfully transcoded to {output_file.name}")
else:
print(f"✗ Error: {message}")
print("\n" + "=" * 60)
print("Transcoding complete!")
print("=" * 60)
if __name__ == "__main__":
main()