MP3 to WAV conversion with Python

Preface

It will be first post. I usually use C # er [^ 1] to develop a rugged desktop application, but I will work on python to spread my knowledge. front end? Can you eat it? Python is quite fresh because I live in the world.

theme

My daughter, who is full of MMD [^ 2], goes to the conversion site one by one to convert the file format and uploads the file, so it can be completed only with her own PC without such troublesome things. I decided to make a converter. This time it will be MP3 → WAV conversion for use with MMD.

Development environment

I looked it up

If you google with "python mp3 wav", it seems that you can convert it using a library called pydub, so I introduced it immediately.

> pip install pydub

Well, do you need ffmpeg? I see, is it a ffmpeg wrapper-like library? I think I should choose and write the ffmpeg command by myself, but it's a good idea, so I decided to take care of pydub.

code

mp32wav.py


import pydub
import os
import sys

if __name__ == "__main__":
    mp3 = sys.argv[1]
    print(f"IN:{mp3}")

    dirname = os.path.dirname(mp3)
    baseName = os.path.splitext(os.path.basename(mp3))[0]
    wav = os.path.join(dirname, f"{baseName}.wav")
    print(f"OUT:{wav}")

    audio = pydub.AudioSegment.from_mp3(mp3)
    audio.export(wav, format='wav')

I just wanted to replace the extension, but it turned out to be a messy code. In essence ...

--The output file is the original file (mp3) with the extension .wav. --Read .mp3 and output as .wav

It's just that. I wonder why C # er has to write ʻos.path ... even though it says ʻimport os.

Operation

Well, I confirmed that it works as expected, but even if I put this mp32wav.py on the desktop, it is not possible to D & D, so prepare a batch file.

mp32wav.bat


@echo off
py mp32wav.py %1

Create a shortcut for this batch file on your desktop and D & D the .mp3 you want to convert, and a .wav will be generated in the same folder as the original file. Congratulations.

But ...

Apparently this is a known issue, but if you give a filename that includes double-byte spaces, it will be cut off there. There is no problem if you quote half-width spaces, and it seems that quoted ones are passed as command line arguments at the time of D & D, but it does not work well with full-width spaces. I will write the test code in C #

argsTest.cs


static void Main(string[] args)
{
    Console.WriteLine($"len={args.Length}");
    foreach (var i in Enumerable.Range(0, args.Length))
    {
        Console.WriteLine(args[i]);
    }
    Console.ReadLine();
}

Furthermore, if you call this from a batch file and eat "aaa bbb.txt" (half-width space) and "aaa bbb.txt" (full-width space)

Half-width space


len=1
C:\Tmp\aaa bbb.txt

Full-width space


len=1
C:\Tmp\aaa

It will be.

Normally, full-width spaces are not used for folder names and file names, but the OS allows such names, and some people may not be aware of half-width spaces and full-width spaces at all, so some measures are necessary. I think. I told my daughter that I could use only half-width spaces for the folder name and file name, and I avoided it for the time being (there is no way to avoid it ...) [^ 1]: Is VB.NET with C # 7.5 and VB6 2.0 with 0.5?

Recommended Posts

MP3 to WAV conversion with Python
Connect to BigQuery with Python
Connect to Wikipedia with Python
Post to slack with Python 3
Switch python to 2.7 with alternatives
Write to csv with Python
How to measure mp3 file playback time with python
[Python] How to FFT mp3 data
Python: How to use async with
Link to get started with python
[Python] Write to csv file with Python
Create folders from '01' to '12' with python
Nice to meet you with python
Try to operate Facebook with Python
Output to csv file with Python
[Python] POST wav files with requests [POST]
Convert list to DataFrame with python
To do tail recursion with Python2
How to get started with Python
What to do with PYTHON release?
Unable to install Python with pyenv
How to use FTP with Python
How to calculate date with python
Easily post to twitter with Python 3
I want to debug with Python
How to play music (wav / mp3) files on Raspberry Pi python
Try to reproduce color film with Python
Try logging in to qiita with Python
Change Python 64bit environment to 32bit environment with Anaconda
English speech recognition with python [speech to text]
Convert memo at once with Python 2to3
HTML email with image to send with python
Memo to ask for KPI with python
Python to remember only with hello, worlds
Output color characters to pretty with python
Algorithm learned with Python 7th: Year conversion
Introduction to Python Image Inflating Image inflating with ImageDataGenerator
Output Python log to console with GAE
Convert Excel data to JSON with python
Convert Hiragana to Romaji with Python (Beta)
Fractal to make and play with Python
I wanted to solve ABC160 with Python
Connect to MySQL with Python within Docker
How to work with BigQuery in Python
[Introduction to Python] Let's use foreach with Python
Single pixel camera to experience with Python
[Python] Introduction to CNN with Pytorch MNIST
Convert FX 1-minute data to 5-minute data with Python
I want to analyze logs with Python
How to do portmanteau test with python
I want to play with aws with python
How to display python Japanese with lolipop
Trying to handle SQLite3 with Python [Note]
[Part1] Scraping with Python → Organize to csv!
Connect to s3 with AWS Lambda Python
Add Gaussian noise to images with python2.7
How to enter Japanese with Python curses
Convert HEIC files to PNG files with Python
Easily download mp3 / mp4 with python and youtube-dl!
Convert Chinese numerals to Arabic numerals with Python
Connect to pepper with PEPPER Mac's python interpreter