0
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

Luuk
  • 12,245
  • 5
  • 22
  • 33
  • Please use [edit] to explain your question (and create a better title) (Info on how to format your question is here: [How do I format my posts using Markdown or HTML?](https://stackoverflow.com/help/formatting) ) – Luuk Jun 25 '22 at 07:48
  • Are you trying to extract audio from `.mpeg` files, and output as `.mp3` files? – Jeppe Jun 25 '22 at 07:48
  • Yes. In goes media, out goes mp3. –  Jun 25 '22 at 07:50
  • Does this answer your question? [How can I iterate over files in a given directory?](https://stackoverflow.com/questions/10377998/how-can-i-iterate-over-files-in-a-given-directory) – Luuk Jun 25 '22 at 07:51
  • You should have kept those back-quotes in your question. It is formatting the code.... (I re-applied my edit) – Luuk Jun 25 '22 at 07:53
  • But is the problem getting the audio, or only applying it to the files that are actually `.mpeg`? For instance, your if-statement could filter correct files like this: `if i.endswith(".mpeg"):`. Is that your problem? Filtering files? – Jeppe Jun 25 '22 at 07:59
  • Nope. It still doesn't work for some reason. It still doesn't throw an error though. –  Jun 25 '22 at 08:00
  • You are also overwriting the .mpeg with the audio (but with same filename). – Jeppe Jun 25 '22 at 08:01
  • That is my problem. I looked at the iterate link and also using the stuff I know and it doesn't work. Still doesn't throw an error though. –  Jun 25 '22 at 08:01
  • 1
    Does this answer your question? [Find all files in a directory with extension .txt in Python](https://stackoverflow.com/questions/3964681/find-all-files-in-a-directory-with-extension-txt-in-python) – Jeppe Jun 25 '22 at 08:10
  • Now it says that ffmpeg is screwed up. Anyone know how to fix that? –  Jun 25 '22 at 08:11
  • You need to be a lot more specific. Try and search for your specific error/stacktrace. If you can't find a question that solves it, then open a new question and be very specific about your input, expected output and the error you get. – Jeppe Jun 25 '22 at 08:17
  • Ok anyone know why when I install or upgrade FFMPEG and then do version it says -bash: FFMPEG: command not found, but when I install or upgrade it says Requirement already satisfied: FFMPEG in ./.local/lib/python3.9/site-packages (1.4)? –  Jun 25 '22 at 08:19
  • No, not here - create a completely new question. But search for an existing one first. – Jeppe Jun 25 '22 at 08:25

1 Answers1

0

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)
Jeppe
  • 1,830
  • 3
  • 24
  • 33