Das Typ
Das Typ

Reputation: 21

How to convert webm/mp4 file to mp3 with ffmpeg in a script (pycharm)?

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

Upvotes: 2

Views: 16708

Answers (1)

Edgar246
Edgar246

Reputation: 21

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))

Upvotes: 2

Related Questions