Created
September 13, 2018 16:00
-
-
Save clamytoe/3d2b6f3f4f6d9ca32196d214b54caad7 to your computer and use it in GitHub Desktop.
Utility to extract the audio from a video file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import argparse | |
from os import path, system | |
# specify the location of your ffmpeg binary | |
ffmpeg = "/home/mohh/anaconda3/pkgs/ffmpeg-4.0-h04d0a96_0/bin/ffmpeg" | |
def parser(): | |
"""Argument parser""" | |
prog = __file__.rsplit("/", 1)[1] | |
parser = argparse.ArgumentParser( | |
prog=prog, description="Utility to extract the audio from a video file." | |
) | |
parser.add_argument("filename", help="name of the source video file") | |
parser.add_argument( | |
"-e", | |
"--ext", | |
default=".m4a", | |
help="extension of the audio file, defaults to .m4a", | |
required=False, | |
) | |
return vars(parser.parse_args()) | |
def extract(filename, ext=".m4a"): | |
"""Extracts the audio track from the video file""" | |
ext = "." + ext if "." not in ext else ext | |
og_ext = "." + filename.rsplit(".")[-1] | |
output = filename.replace(og_ext, ext) | |
if verify(filename): | |
cmd = '{0} -i "{1}" -vn -acodec copy "{2}"'.format(ffmpeg, filename, output) | |
system(cmd) | |
print("\n Successfully extracted: {}\n".format(output)) | |
else: | |
print("The video file '{}' does not exists!".format(filename)) | |
exit(1) | |
def verify(filename): | |
"""Verifies that the file exists""" | |
return path.isfile(filename) | |
def main(): | |
"""Runs the code if started as a script""" | |
params = parser() | |
filename = params.get("filename") | |
ext = params.get("ext") | |
extract(filename, ext) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment