Skip to content

Instantly share code, notes, and snippets.

@viperadnan-git
Created July 29, 2025 17:39
Show Gist options
  • Select an option

  • Save viperadnan-git/595e94518db027786277e871985d81c7 to your computer and use it in GitHub Desktop.

Select an option

Save viperadnan-git/595e94518db027786277e871985d81c7 to your computer and use it in GitHub Desktop.
Python script to get fonts name within a folder
#!/usr/bin/env python3
"""
Font Name Extractor
Lists font files in a directory and extracts their font names for use with ffmpeg.
"""
import os
import sys
from pathlib import Path
try:
from fontTools.ttLib import TTFont
from fontTools.ttLib.tables._n_a_m_e import table__n_a_m_e
except ImportError:
print("Error: fonttools library not found. Install with: pip install fonttools")
sys.exit(1)
def get_font_name(font_path):
"""
Extract the font name from a font file.
Returns the font family name or filename if extraction fails.
"""
try:
font = TTFont(font_path)
name_table = font['name']
# Try to get the font family name (nameID 1)
for record in name_table.names:
if record.nameID == 1: # Font Family name
if record.platformID == 3: # Microsoft platform
return record.toUnicode()
elif record.platformID == 1: # Macintosh platform
return record.toUnicode()
# Fallback: try full font name (nameID 4)
for record in name_table.names:
if record.nameID == 4: # Full font name
if record.platformID == 3:
return record.toUnicode()
elif record.platformID == 1:
return record.toUnicode()
except Exception as e:
print(f"Warning: Could not read font name from {font_path}: {e}")
return Path(font_path).stem # Return filename without extension
return Path(font_path).stem
def list_fonts_in_directory(directory_path):
"""
List all font files in a directory and extract their names.
"""
# Common font file extensions
font_extensions = {'.ttf', '.otf', '.woff', '.woff2', '.ttc', '.otc'}
directory = Path(directory_path)
if not directory.exists():
print(f"Error: Directory '{directory_path}' does not exist.")
return
if not directory.is_dir():
print(f"Error: '{directory_path}' is not a directory.")
return
font_files = []
# Find all font files
for file_path in directory.iterdir():
if file_path.is_file() and file_path.suffix.lower() in font_extensions:
font_files.append(file_path)
if not font_files:
print(f"No font files found in '{directory_path}'")
return
print(f"Found {len(font_files)} font file(s) in '{directory_path}':\n")
# Extract and print font names
for font_file in sorted(font_files):
font_name = get_font_name(font_file)
print(f"File: {font_file.name}")
print(f"Font Name: {font_name}")
print(f"FFmpeg format: '{font_name}'")
print("-" * 50)
def main():
"""Main function to handle command line arguments."""
if len(sys.argv) != 2:
print("Usage: python font_extractor.py <directory_path>")
print("Example: python font_extractor.py /usr/share/fonts/")
sys.exit(1)
directory_path = sys.argv[1]
list_fonts_in_directory(directory_path)
if __name__ == "__main__":
main()
@viperadnan-git
Copy link
Author

Install fonttools from pypi
Use python3 main.py <directory containing all the font files>

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