0

Right now I can use the below code to change all files in a single folder, but I have over 100 folders I need to convert audios for. How do I adjust my code to run through all folders and not just one?

# files                                                                         
lst = glob.glob("*.mp3")
print(lst)
 
for file in lst:
# convert wav to mp3
    os.system(f"""ffmpeg -i {file} -acodec pcm_u8 -ar 22050 {file[:-4]}.wav""")  
ankulka
  • 1
  • 1
  • add a loop that iterates over all the folders you want. `for folder in folderList:` –  Apr 01 '22 at 15:42

1 Answers1

0

I have the function below in my personal library. Point the path to a folder and set the ext to mp3.

Also, if you use this, please upvote the answer I link in the docstring...

def get_files_from_path(path: str = ".", ext=None) -> list:
    """Find files in path and return them as a list.
    Gets all files in folders and subfolders

    See the answer on the link below for a ridiculously
    complete answer for this.
    https://stackoverflow.com/a/41447012/9267296
    Args:
        path (str, optional): Which path to start on.
                              Defaults to '.'.
        ext (str/list, optional): Optional file extention.
                                  Defaults to None.

    Returns:
        list: list of full file paths
    """
    result = []
    for subdir, dirs, files in os.walk(path):
        for fname in files:
            filepath = f"{subdir}{os.sep}{fname}"
            if ext == None:
                result.append(filepath)
            elif type(ext) == str and fname.lower().endswith(ext.lower()):
                result.append(filepath)
            elif type(ext) == list:
                for item in ext:
                    if fname.lower().endswith(item.lower()):
                        result.append(filepath)
    return result
Edo Akse
  • 4,051
  • 2
  • 10
  • 21