Issue
The video generation system relied on hardcoded configuration values spread across multiple sections of the codebase, making it inflexible and difficult to customize for different use cases. Key parameters such as video resolution, frame rate, font sizes, text-to-speech (TTS) voice settings, and audio mixing parameters were embedded directly in the code, requiring manual code changes to adjust settings. This violated configuration management best practices and limited the system’s adaptability.
Culprit
The primary issue was the lack of centralized configuration management. Critical parameters were scattered across the codebase without a unified system for configuration, leading to:
- Hardcoded values: Video resolution, frame rate, and other settings were fixed in the code, preventing easy adjustments.
- No environment support: The system did not utilize environment variables or external configuration files.
- Lack of defaults: No fallback mechanism ensured that default values could be overridden or modified without altering the code.
- Poor version control: Configuration changes were not tracked or managed separately from the code.
Solution
To address these challenges, the system was refactored to use environment variables for configuration management. This involved:
Environment File Structure
A dedicated .env file was introduced to store 15+ configurable parameters, such as:
- Video settings: Resolution, frame rate, and other video-related parameters.
- Audio settings: Volume levels, TTS voice selections, and audio channel configurations.
- Default fallbacks: Default values were set for backward compatibility.
Code Refactoring
The system was updated to replace hardcoded values with environment variable lookups. This ensured that:
- Configuration values could be adjusted without modifying the code.
- Default values were preserved for backward compatibility.
- The system remained portable and adaptable to different environments.
Configuration Management
A .env.example file was added to version control with default values, while the actual .env file was excluded from version control using .gitignore. This allowed users to customize settings without altering the codebase.
Validation
The new configuration system was tested to ensure:
- Default values matched the original behavior.
- Custom resolutions and audio settings worked as expected.
- The system remained backward compatible.
This approach adheres to the 12-factor app principles by centralizing configuration in environment variables, ensuring the system is portable, maintainable, and adaptable to different use cases.