| import os |
| import subprocess |
| import json |
|
|
| def get_mp3_bitrate(file_path): |
| """Extract bitrate (in kbps) using ffprobe.""" |
| cmd = [ |
| "ffprobe", |
| "-v", "quiet", |
| "-print_format", "json", |
| "-show_streams", |
| file_path |
| ] |
| try: |
| result = subprocess.run(cmd, capture_output=True, text=True) |
| info = json.loads(result.stdout) |
| for stream in info["streams"]: |
| if stream.get("codec_type") == "audio": |
| return int(int(stream["bit_rate"]) / 1000) |
| except Exception: |
| pass |
| return 192 |
|
|
|
|
| def convert_mp3_to_mp4(root_folder): |
| for dirpath, _, filenames in os.walk(root_folder): |
| for filename in filenames: |
| if filename.lower().endswith(".mp3"): |
| input_path = os.path.join(dirpath, filename) |
| output_path = os.path.splitext(input_path)[0] + ".mp4" |
|
|
| bitrate = get_mp3_bitrate(input_path) |
| print(f"Converting: {input_path} (bitrate: {bitrate}k)") |
|
|
| cmd = [ |
| "ffmpeg", |
| "-i", input_path, |
| "-c:a", "aac", |
| "-b:a", f"{bitrate}k", |
| "-vn", |
| "-y", |
| output_path |
| ] |
|
|
| subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.STDOUT) |
| print(f" ? {output_path}") |
|
|
|
|
| if __name__ == "__main__": |
| convert_mp3_to_mp4(".") |
|
|