1

I'm working on a YouTube-Downloader project and I'm having issues with my audio files. My code uses pytube to download the audio from a video.

The problem is that the downloaded audio file is either a .webm or a .mp4 file, which I'd like to convert to a .mp3 file. Is there a way to read the filetype (webm/mp4) first and then run a code that converts it to a mp3 file?

I'd like to run it as a script in pycharm. Not as a command in the console.

Thanks for your answers.

Das Typ

Das Typ
  • 11
  • 1
  • 4
  • yes. `str.endswith` and call ffmpeg with `subprocess.run` – kesh Apr 24 '22 at 13:57
  • `yt-dlp` could do all the steps for you. There is no need to reinvent the wheel – mashuptwice Apr 25 '22 at 04:03
  • Thanks for your help. I've tried it with 'ffmpeg -i input.webm output.mp3' but there is always an error that says something like: The command "ffmpeg" is either misspelled or could not be found. Same problem with yt-dlp since it seems like it uses ffmpeg too. How can I solve this? (I've installed ffmpeg over 'pip install ffmpeg') – Das Typ Apr 26 '22 at 10:07

1 Answers1

1

Make sure you have installed ffmpeg correctly,and set the environment path for it.

For windows;

  1. Install ffmpeg from here

  2. Extract it under the root directory C:\

  3. Set the environment path variable by typing this command in your cmd

    setx /m PATH "C:\ffmpeg\bin;%PATH%"
    

Be sure that the extracted folder name is ffmpeg

(You can test if it is installed by typing this into cmd:)

ffmpeg -version

Now you can use ffmpeg from cmd by running subprocess module:

import subprocess

path = '[yourmp4fileaddress]'

subprocess.run('ffmpeg -i "path/filename".mp4 "filename".mp3',shell=True)

just in case in you want to print the output of the above command in your compiler:

import subprocess

path = '[yourpathaddress]'

print(subprocess.run('ffmpeg -i "path/filename".mp4 "filename".mp3',shell=True,capture_output=True))
Edgar246
  • 11
  • 2
  • more info [here](https://stackoverflow.com/questions/65836756/python-ffmpeg-wont-accept-path-why/65860115#65860115) – AudioBaton Jul 05 '22 at 18:07