This is a Python script that converts audio files and extracts audio from video files, outputting to a specified audio format. It features configurable directory paths, automated file handling, and backup options.
- Converts various audio formats to a specified output format (default is MP3)
- Extracts audio from video files
- Automatically detects input file type (audio or video)
- Creates a new output folder for each run, named with the current date and time
- Handles files that are already in the target format efficiently
- Configurable through JSON configuration file
- Flexible file handling options:
- Optional backup of original files
- Optional deletion of processed input files
- Configurable input and output directories
- Python 3.x
- FFmpeg (must be installed and accessible from the command line)
- Clone this repository or download the script files
git clone https://github.com/mochiron-desu/audio-converter
- Ensure FFmpeg is installed on your system and accessible from the command line
# Ubuntu/Debian
sudo apt-get install ffmpeg
# macOS (using Homebrew)
brew install ffmpeg
# Windows
# Download from https://ffmpeg.org/download.html
- Create the necessary directories as specified in your config.json (or use the defaults)
mkdir input output
The script uses a JSON configuration file (config.json
) to manage settings:
{
"directories": {
"input": "input",
"base_output": "output"
},
"conversion": {
"output_format": "mp3"
},
"file_handling": {
"backup_input_files": true,
"delete_input_files": true
}
}
-
directories
input
: Directory where input files are locatedbase_output
: Base directory for output files
-
conversion
output_format
: Target format for audio conversion (default: "mp3")
-
file_handling
backup_input_files
: Whether to backup original files (true/false)delete_input_files
: Whether to delete original files after processing (true/false)
-
Full Processing (default)
backup_input_files: true, delete_input_files: true
- Original files are backed up and then deleted from the input directory
-
Safe Mode
backup_input_files: true, delete_input_files: false
- Original files are backed up but preserved in the input directory
-
Direct Processing
backup_input_files: false, delete_input_files: false
- Original files are preserved in place with no backup
-
Clean Processing
backup_input_files: false, delete_input_files: true
- Original files are deleted without backup after successful conversion
- Place your input audio and video files in the
input
directory - (Optional) Modify the
config.json
file to customize settings - Run the script:
python main.py
The script will process the files according to your configuration settings and create a new timestamped output directory containing the converted files.
audio_video_converter/
│
├── input/ # Place input files here
│ ├── song.m4a
│ └── video.mp4
│
├── output/
│ └── 20240817_220130/ # Timestamped output folder
│ ├── input/ # Backup of original files (if enabled)
│ │ ├── song.m4a
│ │ └── video.mp4
│ ├── song.mp3 # Converted audio file
│ └── video.mp3 # Extracted audio from video
│
├── config.json # Configuration file
└── main.py # Main script
- The script loads configuration from
config.json
(or uses defaults if not found) - Creates a new timestamped output directory
- If backup is enabled, creates an "input" subfolder in the output directory
- For each file in the input directory:
- Backs up the original file (if enabled)
- Uses FFprobe to determine if it's an audio or video file
- Converts to the target format or extracts audio from video
- Places the converted file in the output directory
- If deletion is enabled, removes original files from the input directory
- Provides detailed status messages for all operations
The script includes comprehensive error handling:
- Skips files that aren't recognized as audio or video
- Reports conversion errors for individual files without stopping the process
- Uses safe file operations with proper error reporting
- Provides fallback to default settings if config file is missing
- Reports but continues if file deletion fails
# Place files in input directory
cp mysong.m4a input/
python main.py
{
"directories": {
"input": "my_music",
"base_output": "converted_files"
},
"conversion": {
"output_format": "wav"
},
"file_handling": {
"backup_input_files": false,
"delete_input_files": false
}
}
Input formats are determined by FFmpeg support, which includes:
- MP3
- WAV
- M4A
- FLAC
- OGG
- WMA
- AAC
- MP4
- AVI
- MKV
- MOV
- WMV
- FLV
-
FFmpeg not found
- Ensure FFmpeg is installed and in your system PATH
- Try running
ffmpeg -version
in terminal/command prompt
-
Permission errors
- Ensure you have write permissions in both input and output directories
- Run the script with appropriate permissions
-
Config file errors
- Verify your config.json is valid JSON
- Use a JSON validator if necessary
This project is licensed under the MIT License - see the LICENSE file for details.
- FFmpeg for media processing capabilities
- Python's standard library for file operations
- JSON for configuration management
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
- 1.0.0
- Initial release
- Basic conversion functionality
- 1.1.0
- Added JSON configuration
- Added flexible file handling options
- Added backup functionality
If you have any questions or suggestions, please open an issue in the repository.