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.
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.
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.
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
.
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.
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