How to convert many mp3 files to wav files in python - python

I know with the help of ffmpeg, we can convert an mp3 to wav file. But is there any code or function for automating the process. Which means I have many mp3 files, Instead of manually converting each and every file, is there any other option to convert all the mp3 files to wav files in a particular folder?

Install the module pydub
pip install pydub
Install ffmpeg
sudo apt-get install ffmpeg
Use the below code to convert all the mp3 files
from pydub import AudioSegment
import os
# files
src_folder = "/home/user/Music/mp3"
dst_folder = "/home/user/Music/wav"
#get all music file
files = os.listdir(src_folder)
for name in files:
#name of the file
wav_name = name.replace(".mp3", "")
try:
# convert wav to mp3
sound = AudioSegment.from_mp3("{}/{}".format(src_folder, name))
sound.export("{}/{}".format(dst_folder, wav_name), format="wav")
except Exception as e:
pass
You can find more detailed information here - Link

Related

Compress multiple video at same time or single process using ffmepg python

I create python code to list all mp4 files in a current directory using below code
compress.py
import os
rootdir = 'SUBASH'
extensions = ('.mp4', '.avi', '.wmv')
for subdir, dirs, files in os.walk(rootdir):
for file in files:
ext = os.path.splitext(file)[-1].lower()
print(ext)
if ext in extensions:
print (os.path.join(subdir, file))
After listing all .mp4 files how to compress all file at a time using python and ffmpeg
I tried :
ffmpeg -i input.mp4 -vcodec libx265 -acodec copy output.mp4
I tried this method to compress single video at a time but i want to do same thing for all .mp4 files in a current or specific directory

Converting multiple audio files in different subdirectories using ffmpeg

I am trying to convert multiple .mp3 files stored in different folders to .wav file using ffmpeg. The .wav files will be stored in the folder where the .mp3 file is located
import os
folder = r'D:\S2ST_DataCollection-main\public\recordings' ##### root folder
count = 0
for dirname, dirs, files in os.walk(folder):
for filename in files:
filename_without_extension, extension = os.path.splitext(filename)
if extension == '.mp3': ######## get all the mp3 files
count +=1
os.system(r"C:\Users\amart\Downloads\Compressed\S2ST_DataCollection-main\public\music.bat") ###### use the mp3 to wav conversion stored as batch file
My music.bat file
for %%a in ("*.mp3") do ffmpeg -i "%%a" -vn -c:a pcm_s16le -ar 44100 "%%~na.wav"
Note: I have used pydub but it is not working
If i use the batch command only in a folder it is working fine but my .mp3 files are located in multiple folder
Any kind of help would be greatly appreciated

Attempting to convert .mp3 to .wav via python subprocess call into ffmpeg, "No such file or directory" despite audio file in same directory?

I am attempting to convert an .mp3 testaudio.mp3 into .wav testaudio.wav by using Python's subprocess module and ffmpeg.
I am on Windows, and when I use command prompt to run the following command, it works and converts my .mp3 into .wav successfully:
C:\PATH_programs\ffmpeg-4.4-full_build\ffmpeg-4.4-full_build\bin>ffmpeg -i testaudio.mp3 testaudio.wav
However, when I attempt to use a Python script to do the same thing, I get a "No such file or directory" error:
import subprocess
subprocess.call(['ffmpeg', '-i', 'testaudio.mp3', 'testaudio.wav'])
The ffmpeg.exe, convertmp3towav.py, and audiotest.mp3 files all live in the same directory C:\PATH_programs\ffmpeg-4.4-full_build\ffmpeg-4.4-full_build\bin.
I think pydub is more flexible than subprocess
from pydub import AudioSegment
audio_file = AudioSegment.from_file("testaudio.mp3")
audio_file.export("testaudio.wav", "wav")

Download and Install a .exe File using Python

I wanna create an auto-updater for my project which downloads and installs a file from the internet. I have done how I can download the file to my desired location with a desired name. The place where I am stuck is how I can install the file.
The code so far I have written:
from urllib.request import urlretrieve
import getpass
url = 'https://sourceforge.net/projects/artigence/files/latest/download'
print('File Downloading')
usrname = getpass.getuser()
destination = f'C:\\Users\\{usrname}\\Downloads\\download.exe'
download = urlretrieve(url, destination)
print('File downloaded')
And the file is downloaded to my downloads folder. Now, how can I install the .exe file using python?
You will need to use the subprocess module to execute the .exe files.
import subprocess
cmd = "{download location} batch.exe"
returned_value = subprocess.call(cmd, shell=True) # returns the exit code in unix
print('returned value:', returned_value)
refer
I strongly suggest not to use pyautogui for this.

Including ffmpeg in Django project

I am making a very simple Django app (it's a test for non-Django course) and I need to analyse a mp3 file in there, so I try to turn it into wav with this:
sound = AudioSegment.from_mp3('upload/' + filename)
sound.export('upload/wavfile', format="wav")
rate, data = wav.read('upload/wavfile')
I have installed ffmpeg by pip install ffmpeg in venv terminal, since I want to my code to run not only on my machine. The ffmpeg and ffprobe folders have appeared in /venv/lib/python3.7/site-packages/ however when I run my server I get the warning:
RuntimeWarning: Couldn't find ffmpeg or avconv - defaulting to ffmpeg,
but may not work
warn("Couldn't find ffmpeg or avconv - defaulting to ffmpeg, but may not work", RuntimeWarning)
and when I load file in web page it throws
[Errno 2] No such file or directory: 'ffprobe': 'ffprobe'
at the first line of the code above.
I would really appreciate any help with how I can use ffmpeg in my app or other ways to handle my mp3 file.
You have to add path for ffmpeg executable
import sys
sys.path.append('/path/to/ffmpeg')
or
import ffmpy
ff = ffmpy.FFmpeg(executable='C:\\ffmpeg\\bin\\ffmpeg.exe', inputs={path+'/Stage1Rap.wav': None}, outputs={path+'/FinalRap.mp3': ["-filter:a", "atempo=0.5"]})
ff.run()

Categories