I want to convert multiple MP3 audio files in a folder to WAV format (with mono type) using python code.
I tried the below code using pydub:
import os
from pydub import AudioSegment
audio_files = os.listdir('path')
# Folder is having audio files of both MP3 and WAV formats
len_audio=len(audio_files)
for i in range (len_audio):
if os.path.splitext(audio_files[i])[1] == ".mp3":
mp3_sound = AudioSegment.from_mp3(audio_files[i])
mp3_sound.export("<path>\\converted.wav", format="wav")
I am getting how will i export the converted wav file with different file names.
please suggest
I would do something like:
import os
from pydub import AudioSegment
path = "the path to the audio files"
#Change working directory
os.chdir(path)
audio_files = os.listdir()
# You dont need the number of files in the folder, just iterate over them directly using:
for file in audio_files:
#spliting the file into the name and the extension
name, ext = os.path.splitext(file)
if ext == ".mp3":
mp3_sound = AudioSegment.from_mp3(file)
#rename them using the old name + ".wav"
mp3_sound.export("{0}.wav".format(name), format="wav")
You can find more about the format mini language here.
This is simply just changing the extension name of multiple files
import os
from pathlib import Path
path = "the path to the audio files"
#Change working directory
os.chdir(path)
audio_files = os.listdir()
for file in audio_files:
p = Path(file)
p.rename(p.with_suffix('.wav'))
Related
import moviepy.editor as m
import os
for i in os.listdir():
if i == ".mpeg": #How do I make that work(I know it can't, I just have it there to explain what I need)
video = m.VideoFileClip(i)
video.audio.write_audiofile(i)
Need it to sort through files, find the media ones, and change them to .mp3
Here's how I would change your code. Extract the filename and extension, use the extension to filter files and use the filename to create a new filename with the .mp3 extension. This makes sure you don't overwrite your source video file with audio.
import moviepy.editor as m
import os
for file_path in os.listdir():
filename, file_extension = os.path.splitext(file_path)
if file_extension == ".mpeg":
video = m.VideoFileClip(file_path)
audio_file_path = filename + ".mp3"
video.audio.write_audiofile(audio_file_path)
So I am creating plagiarism software, for that, I need to convert .pdf, .docx,[enter image description here][1] etc files into a .txt format. I successfully found a way to convert all the files in one directory to another. BUT the problem is, this method is changing the file names
into binary values. I need to get the original file name which I am gonna need in the next phase.
**Code:**
import os
import uuid
import textract
source_directory = os.path.join(os.getcwd(), "C:/Users/syedm/Desktop/Study/FOUNDplag/Plagiarism-checker-Python/mainfolder")
for filename in os.listdir(source_directory):
file, extension = os.path.splitext(filename)
unique_filename = str(uuid.uuid4()) + extension
os.rename(os.path.join(source_directory, filename), os.path.join(source_directory, unique_filename))
training_directory = os.path.join(os.getcwd(), "C:/Users/syedm/Desktop/Study/FOUNDplag/Plagiarism-checker-Python/trainingdata")
for process_file in os.listdir(source_directory):
file, extension = os.path.splitext(process_file)
# We create a new text file name by concatenating the .txt extension to file UUID
dest_file_path = file + '.txt'
# extract text from the file
content = textract.process(os.path.join(source_directory, process_file))
# We create and open the new and we prepare to write the Binary Data which is represented by the wb - Write Binary
write_text_file = open(os.path.join(training_directory, dest_file_path), "wb")
# write the content and close the newly created file
write_text_file.write(content)
write_text_file.close()
remove this line where you rename the files:
os.rename(os.path.join(source_directory, filename), os.path.join(source_directory, unique_filename))
that's also not binary, but a uuid instead.
Cheers
I have Example.json.gz and I want to unpack it or extract it in python using shutil.unpack_archive()
However it gives error shutil.ReadError: Unknown archive format as '.gz' format is not in the list of default format.
So it has to be register first using shutil.register_archive_format. Can somebody please help me register and unpack (extract it)
You should define a function that knows how to extract a gz file and then register this function. You could use the gzip library, for instance:
import os
import re
import gzip
import shutil
def gunzip_something(gzipped_file_name, work_dir):
"""gunzip the given gzipped file"""
# see warning about filename
filename = os.path.split(gzipped_file_name)[-1]
filename = re.sub(r"\.gz$", "", filename, flags=re.IGNORECASE)
with gzip.open(gzipped_file_name, 'rb') as f_in: # <<========== extraction happens here
with open(os.path.join(work_dir, filename), 'wb') as f_out:
shutil.copyfileobj(f_in, f_out)
try:
shutil.register_unpack_format('gz', ['.gz', ], gunzip_something)
except:
pass
shutil.unpack_archive("Example.json.gz", os.curdir, 'gz')
WARNING: if you extract on the same dir where your gzipped file resides and your file does not have a .gz extension I'm not sure what happens (overwrite?).
I have many (like 1000) .bz2 files with each (200-50Mb) containing 4 .txt(.dat) files inside , how can I read some specific information from .txt(dat)s without decompressing them? I am only a beginner python 3 user,so please give me some hits or maybe useful examples. Thank you.
I made code which actually unzip .txt(s) in temp folder but it takes like 40sec to proceed 170Mb tar...only one...whereas I have thousands.
import bz2
import os
import tempfile
import shutil
pa = '/home/user/tar' #.tar(s) location
fds = sorted(os.listdir(pa))
i = 0
for bz in fds:
path = os.path.join(pa, tar)
i +=1
archive = bz2.BZ2File(path, 'r')
tmpdir = tempfile.mkdtemp(dir=os.getcwd())
bz2.decompress('example.txt', path=tmpdir)
path_to_my_file = os.path.join(tmpdir, 'example.txt')
here goes some simple manupulation with my .txt (like print smthg)
shutil.rmtree(tmpdir)
I am new to Python (using Python 3.6) and would like to extract the duration (in seconds) of all mp4 files I have in one folder. Code I have is:
path2 = path_directory
from moviepy.video.io.VideoFileClip import VideoFileClip
for root, dirs, files in os.walk(path2):
for filename in files:
clip = VideoFileClip(files)
print(clip.duration)
If I define clip = VideoFileClip("name_of_one_specific_file.mp4") it correctly prints the length (i.e. 590seconds) of that specific file, so I guess the mistake is in how I walk through all the files. I would need a list of the duration for each of the 245 mp4 files I have in path2.
Thank you very much in advance.
You just have a small error:
path2 = path_directory
from moviepy.video.io.VideoFileClip import VideoFileClip
for root, dirs, files in os.walk(path2):
for filename in files:
clip = VideoFileClip(filename) # <= !!!
print(clip.duration)
You want to open filename, not files. filename is the name of one specific file, files is the list of all files in a directory.
There's a newer library, available from Python 3.4, that's generally easier to use than walk. It's pathlib.
This is how you can use it in your situation.
from pathlib import Path
from moviepy.video.io.VideoFileClip import VideoFileClip
path2 = r' -------- '
for filename in Path(path2).glob('*.mp4'):
clip = VideoFileClip(filename.as_posix())
print(clip.duration)