Skip to content

Commit

Permalink
add api
Browse files Browse the repository at this point in the history
  • Loading branch information
SUC-DriverOld committed May 11, 2024
1 parent c4d2937 commit e28aef2
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 2 deletions.
24 changes: 23 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Loudness Matching Tool

[English](./README.md) | [简体中文](./README_zh_CN.md)
English | [简体中文](./README_zh_CN.md)

</div>

Expand Down Expand Up @@ -34,6 +34,28 @@ pip install -r requirements.txt
python gui.py
```

## Use Command Line

Use `audio_processor.py`

```bash
"-i", "--input_dir", Input directory containing audio files
"-o", "--output_dir", Output directory to save processed audio files
"target_loudness", default=-23, Target loudness
"-type", "--loudness_type", default="LUFS", choices=["LUFS", "dBFS", "Peak_dBFS", "RMSdB"], Type of loudness to match
"--export_format", default="wav", choices=["wav", "flac", "mp3"], Audio export format
"--mp3_bitrate", default=320, choices=[128, 192, 256, 320], MP3 bitrate in kbps
"--ffmpeg_sample_rate", default=48000, choices=[32000, 44100, 48000], Output audio sample rate
"--ffmpeg_bit_depth", default=32, choices=[16, 24, 32], Output audio bit depth

```

Example:

```bash
python audio_processor.py -i input -o output -23 -type LUFS -export_format mp3 -mp3_bitrate 320
```

## Notes

1. Currently supported loudness matching methods include:
Expand Down
24 changes: 23 additions & 1 deletion README_zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Loudness Matching Tool

[English](./README.md) | [简体中文](./README_zh_CN.md)
[English](./README.md) | 简体中文

</div>

Expand Down Expand Up @@ -34,6 +34,28 @@ pip install -r requirements.txt
python gui.py
```

## 使用命令行

使用`audio_processor.py`

```bash
"-i", "--input_dir", Input directory containing audio files
"-o", "--output_dir", Output directory to save processed audio files
"target_loudness", default=-23, Target loudness
"-type", "--loudness_type", default="LUFS", choices=["LUFS", "dBFS", "Peak_dBFS", "RMSdB"], Type of loudness to match
"--export_format", default="wav", choices=["wav", "flac", "mp3"], Audio export format
"--mp3_bitrate", default=320, choices=[128, 192, 256, 320], MP3 bitrate in kbps
"--ffmpeg_sample_rate", default=48000, choices=[32000, 44100, 48000], Output audio sample rate
"--ffmpeg_bit_depth", default=32, choices=[16, 24, 32], Output audio bit depth

```

例如:

```bash
python audio_processor.py -i input -o output -23 -type LUFS -export_format mp3 -mp3_bitrate 320
```

## 一些说明

1. 目前支持的响度匹配有以下四种:
Expand Down
54 changes: 54 additions & 0 deletions audio_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import pyloudnorm
import numpy as np
import json
import argparse

FFMPEG = './ffmpeg/ffmpeg.exe'

Expand Down Expand Up @@ -161,3 +162,56 @@ def process_audio(input_dir, output_dir, target_loudness, loudness_type="LUFS",
progress_callback(int((i + 1) / len(audio_files) * 100))

shutil.rmtree(temp_dir)


def main():
parser = argparse.ArgumentParser(description="Audio processing API")
parser.add_argument("-i", "--input_dir", type=str, required=True,
help="Input directory containing audio files")
parser.add_argument("-o", "--output_dir", type=str, required=True,
help="Output directory to save processed audio files")
parser.add_argument("target_loudness", type=float,
default=-23, help="Target loudness (default: -23)")
parser.add_argument("-type", "--loudness_type", type=str, default="LUFS", choices=["LUFS", "dBFS", "Peak_dBFS", "RMSdB"],
help="Type of loudness to match (default: LUFS)")
parser.add_argument("--export_format", type=str, default="wav", choices=["wav", "flac", "mp3"],
help="Audio export format (default: wav)")
parser.add_argument("--mp3_bitrate", type=int, default=320, choices=[128, 192, 256, 320],
help="MP3 bitrate in kbps (default: 320)")
parser.add_argument("--ffmpeg_sample_rate", type=int, default=48000, choices=[32000, 44100, 48000],
help="Output audio sample rate (default: 48000)")
parser.add_argument("--ffmpeg_bit_depth", type=int, default=32, choices=[16, 24, 32],
help="Output audio bit depth (default: 32)")

args = parser.parse_args()

print("Input directory:{}, Output directory:{}, Target loudness:{}, Loudness type:{}, Export format:{}, MP3 bitrate:{}, FFMPEG sample rate:{}, FFMPEG bit depth:{}".format(
args.input_dir, args.output_dir, args.target_loudness, args.loudness_type, args.export_format, args.mp3_bitrate, args.ffmpeg_sample_rate, args.ffmpeg_bit_depth
))

with open('config.json', 'r') as config_file:
config = json.load(config_file)

config["export_format"] = args.export_format
config["mp3_bitrate"] = args.mp3_bitrate
config["ffmpeg_sample_rate"] = args.ffmpeg_sample_rate
config["ffmpeg_bit_depth"] = args.ffmpeg_bit_depth

with open('config.json', 'w') as config_file:
json.dump(config, config_file, indent=4)

if args.loudness_type == "LUFS":
loudness_type = "ITU-R BS.1770 (LUFS)"
elif args.loudness_type == "dBFS":
loudness_type = "平均响度 (dBFS)"
elif args.loudness_type == "Peak_dBFS":
loudness_type = "最大峰值 (dBFS)"
elif args.loudness_type == "RMSdB":
loudness_type = "总计 RMS (dB)"

process_audio(args.input_dir, args.output_dir,
args.target_loudness, loudness_type)


if __name__ == "__main__":
main()
2 changes: 2 additions & 0 deletions gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def initUI(self):
self.setLayout(layout)

self.setFixedWidth(200)
self.setFixedHeight(300)

def load_config(self):
try:
Expand Down Expand Up @@ -162,6 +163,7 @@ def initUI(self):

self.setLayout(layout)
self.setFixedWidth(300)
self.setFixedHeight(350)

def browse_input_dir(self):
input_dir = QFileDialog.getExistingDirectory(
Expand Down

0 comments on commit e28aef2

Please sign in to comment.