├── README.md └── mp4tomp3.py /README.md: -------------------------------------------------------------------------------- 1 | mp4tomp3.py 2 |

A small Python script to convert mp4 video files to mp3 audio. Useful for turning video from sites such as www.ted.com into audio files useable on any old mp3 player.
3 | It uses mplayer and lame to do the actual conversion.

4 | 5 | Usage: python mp4tomp3.py [input directory [output directory]]
6 |   input directory (optional) - set directory containing mp4 files to convert (defaults to current folder)
7 |   output directory (optional) - set directory to export mp3 files to (defaults to input)
8 | example: python mp4tomp3.py ./video ./audio
9 | 10 | Note: you will need python (2 or 3), mplayer and lame for this script to work
11 |
12 | sudo apt-get install python2.7   -- for python 2
13 | sudo apt-get install python3.6   -- for python 3
14 | sudo apt-get install mplayer
15 | sudo apt-get install lame
16 | 
17 | 18 | Mac Support: Thanks to jdingus for this tip.
19 | First install Homebrew (https://brew.sh/). Once installed you will be able to install dependencies with the following commands.
20 |
21 | brew install mplayer
22 | brew install lame
23 | 
24 | -------------------------------------------------------------------------------- /mp4tomp3.py: -------------------------------------------------------------------------------- 1 | # MP4 TO MP3 CONVERSION SCRIPT 2 | # script to convert mp4 video files to mp3 audio 3 | # useful for turning video ripped from sites such as Youtube or TED 4 | # into audio files useable with any old mp3 player. 5 | # 6 | # usage: python mp4tomp3.py [input directory [output directory]] 7 | # input directory (optional) - set directory containing mp4 files to convert (defaults to current folder) 8 | # output directory (optional) - set directory to export mp3 files to (defaults to input) 9 | # 10 | # NOTE: you will need python (2 or 3), mplayer and lame for this script to work 11 | # sudo apt-get install lame 12 | # sudo apt-get install mplayer 13 | # sudo apt-get install python2.7 -- for python 2 14 | # sudo apt-get install python3.6 -- for python 3 15 | 16 | from __future__ import print_function # for compatibility with both python 2 and 3 17 | from subprocess import call # for calling mplayer and lame 18 | from sys import argv # allows user to specify input and output directories 19 | import os # help with file handling 20 | 21 | def check_file_exists(directory, filename, extension): 22 | path = directory + "/" + filename + extension 23 | return os.path.isfile(path) 24 | 25 | def main(indir, outdir): 26 | 27 | 28 | try: 29 | # check specified folders exist 30 | if not os.path.exists(indir): 31 | exit("Error: Input directory \'" + indir + "\' does not exist. (try prepending './')") 32 | if not os.path.exists(outdir): 33 | exit("Error: Output directory \'" + outdir + "\' does not exist.") 34 | if not os.access(outdir, os.W_OK): 35 | exit("Error: Output directory \'" + outdir + "\' is not writeable.") 36 | 37 | print("[{0}/*.mp4] --> [{1}/*.mp3]".format(indir, outdir)) 38 | files = [] # files for exporting 39 | 40 | # get a list of all convertible files in the input directory 41 | filelist = [ f for f in os.listdir(indir) if f.endswith(".mp4") ] 42 | for path in filelist: 43 | basename = os.path.basename(path) 44 | filename = os.path.splitext(basename)[0] 45 | files.append(filename) 46 | # remove files that have already been outputted from the list 47 | files[:] = [f for f in files if not check_file_exists(outdir, f, ".mp3")] 48 | except OSError as e: 49 | exit(e) 50 | 51 | if len(files) == 0: 52 | exit("Could not find any files to convert that have not already been converted.") 53 | 54 | # convert all unconverted files 55 | for filename in files: 56 | print("-- converting {0}/{2}.mp4 to {1}/{2}.mp3 --".format(indir, outdir, filename)) 57 | call(["mplayer", "-novideo", "-nocorrect-pts", "-ao", "pcm:waveheader", indir + "/" + filename + ".mp4"]) 58 | call(["lame", "-v", "audiodump.wav", outdir + "/" + filename + ".mp3"]) 59 | os.remove("audiodump.wav") 60 | 61 | # set the default directories and try to get input directories 62 | args = [".", "."] 63 | for i in range(1, min(len(argv), 3)): 64 | args[i - 1] = argv[i] 65 | 66 | # if only input directory is set, make the output directory the same 67 | if len(argv) == 2: 68 | args[1] = args[0] 69 | 70 | main(args[0], args[1]) 71 | --------------------------------------------------------------------------------