forked from aysiu/Mac-Scripts-and-Profiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExtractCombineMOVtoMP3.py
93 lines (83 loc) · 3.29 KB
/
ExtractCombineMOVtoMP3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/python
import os
import subprocess
# You can get ffmpeg from https://www.ffmpeg.org/download.html
# Path to ffmpeg binary
ffmpeg='/usr/local/bin/ffmpeg'
# Directory of movies to convert to audio
source='/PATH/TO/WHERE/THE/ORIGINAL/MOVIE/FILES/ARE'
# Directory to put output audio files
destination='/PATH/TO/WHERE/YOU/WANT/COMBINED/MP3S/TO/GO'
# Main
def main():
# First, test to make sure the ffmpeg binary exists. If it doesn't, there's no point in even running any of the rest of this script
if os.path.isfile(ffmpeg):
# Make sure the destination directory exists to output things
if os.path.isdir(destination):
# Loop through the pkgsinfo
if os.path.isdir(source):
for root, dirs, files in os.walk(source):
for dir in dirs:
# Skip directories starting with a period
if dir.startswith("."):
dirs.remove(dir)
fulldir = os.path.join(root, dir)
print "We are now in %s" % dir
# Make a temporary directory to store converted files
tempdir=os.path.join(destination, 'tempfiles', dir)
if not os.path.isdir(tempdir):
os.makedirs(tempdir)
# When the os.walk happens for files, it doesn't necessarily sort the files, so we'll put them into a list to sort and go through later
mp3s=[]
for subroot, subdirs, subfiles in os.walk(fulldir):
for file in subfiles:
# Skip files that start with a period
if file.startswith("."):
continue
# Get the full path to the file
fullfile = os.path.join(subroot, file)
# Get basename without the file extension
filebase=os.path.splitext(file)[0]
# Create path to output .mp3
newmp3=os.path.join(tempdir, filebase)
newmp3+='.mp3'
# Convert this movie file to .mp3
cmd = [ ffmpeg, '-y', '-i', fullfile, '-f', 'mp3', '-ab', '192000', '-vn', newmp3 ]
print "Creating %s" % newmp3
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# Append the full path of the file to the list
mp3s.append(newmp3)
# Wait a second for the command to complete
# time.sleep(1)
proc.wait()
# Path to concat text file
concat=os.path.join(destination, 'tempfiles', dir)
concat+='.txt'
# Open file to write names of mp3s to concat
f=open(concat, "w+")
# Sort the list
mp3s.sort()
# Loop through the list and add those paths to the text file for concat
for mp3 in mp3s:
f.write("file '" + mp3 + "'\n")
# Close text file
f.close()
# Name for combined mp3
outputmp3=os.path.join(destination, dir)
outputmp3+='.mp3'
# Combine all the mp3s
print "Combining %s" % concat
cmd = [ ffmpeg, '-y', '-f', 'concat', '-safe', '0', '-i', concat, '-c', 'copy', outputmp3 ]
print "Creating %s" % outputmp3
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# Wait a few seconds for the command to complete
#time.sleep(4)
proc.wait()
else:
print "%s is not a valid path to find files to convert." % source
else:
print "%s is not a valid destination directory." % destination
else:
print "The path %s to ffmpeg is invalid, so the script can't be run." % ffmpeg
if __name__ == '__main__':
main()