Created
January 27, 2020 17:25
-
-
Save tripulse/2d894adfbee84a9f8a61a94f6451e257 to your computer and use it in GitHub Desktop.
Remuxes an audio file from an arbitary container into MKA with FFmpeg.
This file contains hidden or 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
import argparse | |
import subprocess | |
import typing | |
import re | |
import os | |
import glob | |
import sys | |
class FFmpegFail(Exception): | |
def __init__(self, statuscode, *args): | |
Exception.__init__(self, f"FFmpeg failed with a non-zero statuscode ({statuscode}).", *args) | |
class FFmpegRemuxIntoMKA(subprocess.Popen): | |
"""Batch process a list of audio container | |
bytestreams and converts into the MKA container.""" | |
FILEEXTENSION_FIND = re.compile(r'\.[a-z0-9]+$') | |
def __init__(self, remove_infile= False): | |
self.ffmpeg_argv = ['ffmpeg', | |
'-nostdin', | |
'-i', '', #< Input filename (must contain extension). | |
'-c', 'copy', | |
'-vn', # disable video-streams to be MKA. | |
'', #> Output filename (must contain extension). | |
'-y', #> Force overwriting if exists. | |
] | |
self.replace_file = bool(remove_infile) | |
def main(self, infiles: typing.Iterable[str]): | |
"""Processes a list of files through FFmpeg. | |
Raises `FFmpegFail` if FFmpeg exits with a non-zero status code.""" | |
for ifile in infiles: | |
self.ffmpeg_argv[3] = ifile | |
self.ffmpeg_argv[7] = self.FILEEXTENSION_FIND.sub('.mka', ifile) | |
subprocess.Popen.__init__(self, self.ffmpeg_argv, stderr= subprocess.PIPE) | |
(_, fflogs) = self.communicate() | |
print("Remuxed: %s -> %s" % (ifile, self.ffmpeg_argv[7]), file= sys.stderr) | |
if self.returncode != 0: | |
raise FFmpegFail(self.returncode, self.ffmpeg_argv, fflogs) | |
else: | |
if self.replace_file: | |
os.unlink(ifile) | |
print("Removed: %s" % ifile, file= sys.stderr) | |
if __name__ == '__main__': | |
optparser = argparse.ArgumentParser( | |
prog= "Any2MKA", | |
description= "a simple remuxer used to remux audio files into the MKA audio file-format." | |
"uses FFmpeg as the backend for this process.") | |
optparser.add_argument('FILES', | |
help= "Glob expression to grab the input files.", | |
type= lambda f: glob.glob(f)) | |
optparser.add_argument('-r', '--replace', | |
help= "Whether remove the input file after conversion.", | |
dest= 'REPLACE_FILE', action= 'store_true') | |
opts = optparser.parse_args() | |
rmux = FFmpegRemuxIntoMKA(opts.REPLACE_FILE) | |
rmux.main(opts.FILES) |
I'd not use this, nor do I currently.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is made for my personal use, as you can see the code quality isn't good.