mirror of
https://github.com/metafy-social/python-scripts.git
synced 2024-11-24 04:21:12 +00:00
Merge pull request #158 from jrafaaael/master
feat: convert audio from an extension to another (e.g. mp3 to wav)
This commit is contained in:
commit
945d44ffeb
28
scripts/AudioConverter/README.md
Normal file
28
scripts/AudioConverter/README.md
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
# Audio Converter
|
||||||
|
|
||||||
|
CLI tool to convert an audio file from one extension to another (e.g. mp3 to wav)
|
||||||
|
|
||||||
|
## Requirements
|
||||||
|
- [FFmpeg](https://ffmpeg.org/)
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
### Convert all audio files in a specific directory
|
||||||
|
```bash
|
||||||
|
python3 audio-converter.py -p <YOUR-PATH-WAS-HERE> -e <THE-DESIRED-EXTENSION>
|
||||||
|
```
|
||||||
|
|
||||||
|
e.g.
|
||||||
|
```bash
|
||||||
|
python3 audio-converter.py -p /home/username/Music -e .wav
|
||||||
|
```
|
||||||
|
|
||||||
|
### Convert specific audio file
|
||||||
|
```bash
|
||||||
|
python3 audio-converter.py -p <YOUR-PATH-WAS-HERE> -e <THE-DESIRED-EXTENSION>
|
||||||
|
```
|
||||||
|
|
||||||
|
e.g.
|
||||||
|
```bash
|
||||||
|
python3 audio-converter.py -p /home/username/Music/test.mp3 -e .wav
|
||||||
|
```
|
51
scripts/AudioConverter/audio-converter.py
Normal file
51
scripts/AudioConverter/audio-converter.py
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
import subprocess
|
||||||
|
import os
|
||||||
|
import argparse
|
||||||
|
|
||||||
|
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description='A program to convert audio to another audio format'
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'-p',
|
||||||
|
'--path',
|
||||||
|
type=str,
|
||||||
|
help='''
|
||||||
|
The full path of file to convert OR
|
||||||
|
the full path of folder that contains all files to convert'
|
||||||
|
''',
|
||||||
|
required=True
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'-e',
|
||||||
|
'--extension',
|
||||||
|
type=str,
|
||||||
|
help='The type of extension to convert the audio',
|
||||||
|
default='.mp3',
|
||||||
|
required=False
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def convert(file, extension):
|
||||||
|
file_name, _ = file.split('.')
|
||||||
|
output_name = file_name + extension
|
||||||
|
subprocess.run(['ffmpeg', '-i', file, output_name])
|
||||||
|
|
||||||
|
|
||||||
|
def convert_all(path, extension):
|
||||||
|
for _, _, files in os.walk(path):
|
||||||
|
for file in files:
|
||||||
|
convert(file, extension)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
args = parser.parse_args()
|
||||||
|
path = args.path
|
||||||
|
extension = args.extension
|
||||||
|
|
||||||
|
os.chdir(os.path.dirname(path))
|
||||||
|
|
||||||
|
if (os.path.isdir(path)):
|
||||||
|
convert_all(path=path, extension=extension)
|
||||||
|
else:
|
||||||
|
convert(file=path, extension=extension)
|
Loading…
Reference in New Issue
Block a user