Skip to content

Commit

Permalink
added WrongContainer exception that is raised by converter funcs
Browse files Browse the repository at this point in the history
  • Loading branch information
balintlaczko committed Sep 29, 2021
1 parent 379ff34 commit 079629c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
14 changes: 14 additions & 0 deletions musicalgestures/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,11 @@ def show(self):
return self.figure


class WrongContainer(Exception):
def __init__(self, message):
self.message = message


def convert(filename, target_name, overwrite=False):
"""
Converts a video to another format/container using ffmpeg.
Expand Down Expand Up @@ -404,6 +409,8 @@ def convert_to_avi(filename, target_name=None, overwrite=False):
return filename
if not target_name:
target_name = of + '.avi'
elif os.path.splitext(target_name)[1].lower() != '.avi':
raise WrongContainer(f"requested target container is NOT .avi but {os.path.splitext(target_name)[1]}.")
if not overwrite:
target_name = generate_outfilename(target_name)
cmds = ['ffmpeg', '-y', '-i', filename, "-c:v", "mjpeg",
Expand Down Expand Up @@ -432,13 +439,16 @@ def convert_to_mp4(filename, target_name=None, overwrite=False):
return filename
if not target_name:
target_name = of + '.mp4'
elif os.path.splitext(target_name)[1].lower() != '.mp4':
raise WrongContainer(f"requested target container is NOT .mp4 but {os.path.splitext(target_name)[1]}.")
if not overwrite:
target_name = generate_outfilename(target_name)
cmds = ['ffmpeg', '-y', '-i', filename,
"-q:v", "3", target_name]
ffmpeg_cmd(cmds, get_length(filename), pb_prefix='Converting to mp4:')
return target_name


def convert_to_webm(filename, target_name=None, overwrite=False):
"""
Converts a video to one with .webm extension using ffmpeg.
Expand All @@ -459,6 +469,8 @@ def convert_to_webm(filename, target_name=None, overwrite=False):
return filename
if not target_name:
target_name = of + '.webm'
elif os.path.splitext(target_name)[1].lower() != '.webm':
raise WrongContainer(f"requested target container is NOT .webm but {os.path.splitext(target_name)[1]}.")
if not overwrite:
target_name = generate_outfilename(target_name)
cmds = ['ffmpeg', '-y', '-i', filename,
Expand Down Expand Up @@ -486,6 +498,8 @@ def cast_into_avi(filename, target_name=None, overwrite=False):
of = os.path.splitext(filename)[0]
if not target_name:
target_name = of + '.avi'
elif os.path.splitext(target_name)[1].lower() != '.avi':
raise WrongContainer(f"requested target container is NOT .avi but {os.path.splitext(target_name)[1]}.")
if not overwrite:
target_name = generate_outfilename(target_name)
cmds = ['ffmpeg', '-y', '-i', filename, "-codec copy", target_name]
Expand Down
29 changes: 22 additions & 7 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ def test_output(self, format_pairs, execution_number, tmp_path, testvideo_avi):
else:
startfile = testvideo_avi
result = convert(startfile, target_name)
length_in = get_length(startfile)
length_out = get_length(result)
# length_in = get_length(startfile)
# length_out = get_length(result)
assert os.path.isfile(result) == True
assert os.path.splitext(result)[1] == fex_to
assert target_name == result
Expand All @@ -169,12 +169,17 @@ class Test_convert_to_avi:
def test_output(self, tmp_path, testvideo_mp4):
target_name = str(tmp_path).replace("\\", "/") + "/testvideo_converted.avi"
testvideo_avi = convert_to_avi(testvideo_mp4, target_name=target_name)
length_in = get_length(testvideo_mp4)
length_out = get_length(testvideo_avi)
# length_in = get_length(testvideo_mp4)
# length_out = get_length(testvideo_avi)
assert os.path.isfile(testvideo_avi) == True
assert os.path.splitext(testvideo_avi)[1] == ".avi"
assert target_name == testvideo_avi
# assert length_in == length_out # this will fail due to ffmpeg bug: https://trac.ffmpeg.org/ticket/9443#ticket

def test_requested_container_incorrect(self, tmp_path, testvideo_mp4):
target_name = str(tmp_path).replace("\\", "/") + "/testvideo_converted.mp4"
with pytest.raises(WrongContainer):
convert_to_avi(testvideo_mp4, target_name=target_name)


class Test_convert_to_mp4:
Expand All @@ -188,15 +193,25 @@ def test_output(self, tmp_path, testvideo_avi):
assert target_name == testvideo_mp4
assert length_in == length_out

def test_requested_container_incorrect(self, tmp_path, testvideo_avi):
target_name = str(tmp_path).replace("\\", "/") + "/testvideo_converted.avi"
with pytest.raises(WrongContainer):
convert_to_mp4(testvideo_avi, target_name=target_name)


class Test_convert_to_webm:
# @pytest.mark.xfail(raises=AssertionError)
def test_output(self, tmp_path, testvideo_avi):
target_name = str(tmp_path).replace("\\", "/") + "/testvideo_converted.webm"
testvideo_webm = convert_to_webm(testvideo_avi, target_name=target_name)
length_in = get_length(testvideo_avi)
length_out = get_length(testvideo_webm)
# length_in = get_length(testvideo_avi)
# length_out = get_length(testvideo_webm)
assert os.path.isfile(testvideo_webm) == True
assert os.path.splitext(testvideo_webm)[1] == ".webm"
assert target_name == testvideo_webm
# assert length_in == length_out # this will fail, need to find out why
# assert length_in == length_out # this will fail, need to find out why

def test_requested_container_incorrect(self, tmp_path, testvideo_avi):
target_name = str(tmp_path).replace("\\", "/") + "/testvideo_converted.mp4"
with pytest.raises(WrongContainer):
convert_to_webm(testvideo_avi, target_name=target_name)

0 comments on commit 079629c

Please sign in to comment.