Skip to content

Instantly share code, notes, and snippets.

@filipedfs
Created December 13, 2020 14:18
Show Gist options
  • Select an option

  • Save filipedfs/65cb32b47c8cd90a053199fccf2f4440 to your computer and use it in GitHub Desktop.

Select an option

Save filipedfs/65cb32b47c8cd90a053199fccf2f4440 to your computer and use it in GitHub Desktop.
Convert all media files from a folder to audio
import subprocess
import os
#%%
current_directory: str = os.getcwd()
working_directory = os.path.abspath(os.path.join(os.path.dirname(__file__), os.pardir))
#%%
def convert_to_audio(filename, file_extension):
ffmpeg_text = ['ffmpeg', '-n']
ffmpeg_text.append('-i')
ffmpeg_text.append(filename + file_extension)
ffmpeg_text.append('-c:a')
ffmpeg_text.append('pcm_s16le')
ffmpeg_text.append('-ar')
ffmpeg_text.append('48000')
ffmpeg_text.append('audio/' + filename + '.wav')
result = subprocess.run(ffmpeg_text,
stdout=subprocess.PIPE,
cwd=working_directory)
print(result.stdout)
#%%
f = []
for (dirpath, dirnames, filenames) in os.walk(working_directory):
f.extend(filenames)
break
# print(dirpath)
# print(dirnames)
# print(filenames)
#%
for filename in filenames:
filename, file_extension = os.path.splitext(filename)
# print(filename)
# print(file_extension)
if file_extension.lower() in ['.mp4', '.mov', '.wav', '.m4a']:
convert_to_audio(filename, file_extension)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment