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
Related
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
i am trying to convert my ImageConverter.py program to an Executable. I use
python3.6.7
pyinstaller --onefile ImageConverter.py
The Executable is made along with the folder pycache , build, and dist
The executable file is in the dist folder, when I try to run the exe the terminal pops up and performs the program and says complete. However no jpeg files are created or in the dist folder. I have also made sure my original .py file, along with my png files are also in the dist folder with the exe. is there something im missing here? are the converted jpeg files in another location?
The ImageConverter.py program uses PIL python package, it open the png files, converts them to RGB, than saves them as jpegs. The program works when running it as usual in terminal using python3 but does not work when trying the exe. any help is appreciated! thanks
ImageConverter.py. :
from PIL import Image #Python Image Library - Image Processing
import glob
import os
import sys
application_path = os.path.dirname(sys.executable)
print(glob.glob("*.png"))
#Iterate through all the images
#Convert images to RGB
#Save Images
for files in glob.glob("*.png"):
im = Image.open(files)
rgb_im = im.convert("RGB")
rgb_im.save(files.replace("png", "jpeg"), quality=95)
output_path = os.path.join(application_path, f'images')
Ive tried to run the executable multiple times. I put the png files and also .py into the dist folder where the exe exists and run. I was expecting the png files to convert to jpeg files, leaving png and jpegs in the dist folder after running exe. However terminal said :
PythonPractice/ImageProcessingPractice/dist/ImageConverter ; exit;
[]
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.
[Process completed]
There is no converted Jpegs in the dist folder
sys.executable is the location of the Python interpreter you are running, e.g. /usr/bin/python3 or C:\Program Files\Python\Scripts\python.exe, not the location of the script you are running.
Therefore, for a system-installed Python binary, os.path.dirname(sys.executable) will return something like /usr/bin or C:\Program Files\Python\Scripts, and os.path.join(application_path, f'images') will return something like /usr/bin/images or C:\Program Files\Python\Scripts\images.
But for a PyInstaller program that used --onefile, that's likely a temporary directory somewhere that gets cleaned up right after the program is finished running.
It might make more sense to just use
output_path = 'images'
without any prefix, which should be relative to the current working directory (wherever you are when you run the script).
I'm using SOX Command in Windows to Convert .wav files to a spectogram .png file, from the cmd directly it's working but in the python script it returns 1 but the image doesn't exist in the directory specified.
rootdir =r'C:\Users\Heba\output.wav'
wave_path = rootdir
wave_image_path = wave_path.replace(".wav", ".png").
cmdstring = 'sox "{}" -n spectrogram -r -o "{}"'.format(wave_path, wave_image_path)
subprocess.call(cmdstring, shell=True)
it doesn't provide any errors too, any help please?
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
I wrote a python script to rename all the JPG and jpeg files present in a particular folder.
I changed my directory to the folder which contained all the jpg, JPG files and a rename.py ( which is the script that I wrote ).
import os
cwd=os.getcwd()
for file in os.listdir(cwd):
i=1
if file.endswith('.JPG') or file.endswith('.jpeg'):
new=str(i)+'.JPG'
os.rename(file, new)
i+=1
After running the script all the JPG and jpeg files went missing.(Edit:except 1.JPG)Why did this happen and how do I recover the files?