Skip to content

Instantly share code, notes, and snippets.

@vidhan13j07
Created July 12, 2016 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vidhan13j07/aa9ebbc57a7c882496cae40a0440d539 to your computer and use it in GitHub Desktop.
Save vidhan13j07/aa9ebbc57a7c882496cae40a0440d539 to your computer and use it in GitHub Desktop.
A small script to extract the audio from the videos in a particular directory
#!/usr/bin/env python3
import sys
import string
import os
def message():
print ('Command: python3 vid_to_audio.py <Absolute path of directory>')
def split_file(filename):
l = filename.split('.')
return ".".join(l[:-1]),l[-1]
def convert(filelist):
s = ' ()'
try:
for ffile in filelist:
print(ffile)
file = ffile
for character in s:
file = file.replace(character, '\\' + character)
filename,extension = split_file(file)
video_to_wav = 'ffmpeg -i ' + file + ' ' + filename + '.wav'
audio = 'lame ' + filename + '.wav' + ' ' + filename + '.mp3'
if os.path.exists(split_file(ffile)[0] + '.mp3'):
print('File already downloaded!\n\n')
continue
os.system(video_to_wav)
os.system(audio)
os.remove(split_file(ffile)[0] + '.wav')
print('*'*50 + '\n\n')
print('Successfully converted ' + ffile.split('.')[0].split('/')[-1] + ' to .mp3\n\n')
print('*'*50 + '\n\n\n\n')
except OSError as err:
print(err.reason)
exit(1)
def main():
if len(sys.argv) < 1 or len(sys.argv) > 2:
message()
exit(1)
directory = sys.argv[1]
try:
if os.path.exists(directory):
print("Starting the operation....")
except OSError as er:
print(er.reason)
exit(1)
fileList = []
for root, subFolders, files in os.walk(directory):
for file in files:
fileList.append(os.path.join(root,file))
convert(fileList)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment