Skip to content

Commit beb194a

Browse files
author
Sukhbinder Singh
committed
Black and tests
1 parent b4e4086 commit beb194a

File tree

4 files changed

+84
-26
lines changed

4 files changed

+84
-26
lines changed

README.md

+20
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,26 @@ vidtoolz install vidtoolz-apply-fadein-fadeout
2424

2525
type ``vid fadeinout --help`` to get help
2626

27+
```bash
28+
usage: vid fadeinout [-h] [-d DURATION] [-o OUTPUT]
29+
video {fadein,fadeout,both}
30+
31+
Apply fadein-fadeout effects on videos
32+
33+
positional arguments:
34+
video Path to the input video file.
35+
{fadein,fadeout,both}
36+
Type of fade effect to apply.
37+
38+
optional arguments:
39+
-h, --help show this help message and exit
40+
-d DURATION, --duration DURATION
41+
Duration of the fade effect in seconds. (Default 1)
42+
-o OUTPUT, --output OUTPUT
43+
Path for the output video file. Defaults to
44+
'output_video.mp4'.
45+
46+
```
2747

2848

2949
## Development
+19-8
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import pytest
22
import moviepy as mpy
3-
from vidtoolz_apply_fadein_fadeout.applyfadeinfadeout import apply_fade_effect
3+
from vidtoolz_apply_fadein_fadeout.applyfadeinfadeout import apply_fade_effect
44
import vidtoolz_apply_fadein_fadeout as w
55

66
from argparse import Namespace, ArgumentParser
77

8+
89
def test_create_parser():
910
subparser = ArgumentParser().add_subparsers()
1011
parser = w.create_parser(subparser)
1112

1213
assert parser is not None
1314

14-
result = parser.parse_args(['test.mp4', 'fadein'])
15+
result = parser.parse_args(["test.mp4", "fadein"])
1516
assert result.video == "test.mp4"
1617
assert result.fade_type == "fadein"
1718
assert result.duration == 1
@@ -24,11 +25,13 @@ def test_plugin(capsys):
2425
assert "Hello! This is an example ``vidtoolz`` plugin." in captured.out
2526

2627

27-
2828
@pytest.fixture
2929
def sample_clip():
3030
"""Create a short test video clip."""
31-
return mpy.ColorClip(size=(640, 480), color=(255, 0, 0), duration=5).with_fps(30) # Red video
31+
return mpy.ColorClip(size=(640, 480), color=(255, 0, 0), duration=5).with_fps(
32+
30
33+
) # Red video
34+
3235

3336
@pytest.mark.parametrize("fade_type", ["fadein", "fadeout", "both"])
3437
def test_apply_fade_effect_with_clip(sample_clip, fade_type):
@@ -37,30 +40,38 @@ def test_apply_fade_effect_with_clip(sample_clip, fade_type):
3740
processed_clip, fps = apply_fade_effect(sample_clip, fade_type, duration)
3841

3942
assert isinstance(processed_clip, mpy.VideoClip), "Output should be a VideoClip"
40-
assert processed_clip.duration == sample_clip.duration, "Clip duration should remain unchanged"
43+
assert (
44+
processed_clip.duration == sample_clip.duration
45+
), "Clip duration should remain unchanged"
4146
assert fps == sample_clip.fps, "FPS should be preserved"
4247

4348
sample_clip.close()
4449
processed_clip.close()
4550

51+
4652
@pytest.mark.parametrize("fade_type", ["fadein", "fadeout", "both"])
4753
def test_apply_fade_effect_with_path(tmp_path, fade_type):
4854
"""Test fade effects using a file path."""
4955
test_video_path = str(tmp_path / "test.mp4")
50-
sample_clip = mpy.ColorClip(size=(640, 480), color=(255, 0, 0), duration=5).with_fps(30)
56+
sample_clip = mpy.ColorClip(
57+
size=(640, 480), color=(255, 0, 0), duration=5
58+
).with_fps(30)
5159
sample_clip.write_videofile(test_video_path, codec="libx264", fps=30, audio=False)
5260

5361
duration = 1 # 1-second fade duration
5462
processed_clip, fps = apply_fade_effect(test_video_path, fade_type, duration)
5563

5664
assert isinstance(processed_clip, mpy.VideoClip), "Output should be a VideoClip"
57-
assert processed_clip.duration == sample_clip.duration, "Clip duration should remain unchanged"
65+
assert (
66+
processed_clip.duration == sample_clip.duration
67+
), "Clip duration should remain unchanged"
5868
assert fps == sample_clip.fps, "FPS should be preserved"
5969

6070
sample_clip.close()
6171
processed_clip.close()
6272

73+
6374
def test_invalid_fade_type(sample_clip):
6475
"""Test with an invalid fade type."""
6576
with pytest.raises(ValueError, match="Invalid fade type"):
66-
apply_fade_effect(sample_clip, "invalid_type", 1)
77+
apply_fade_effect(sample_clip, "invalid_type", 1)
+33-9
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,56 @@
11
import vidtoolz
2-
from vidtoolz_apply_fadein_fadeout.applyfadeinfadeout import apply_fade_effect, write_clip
2+
from vidtoolz_apply_fadein_fadeout.applyfadeinfadeout import (
3+
apply_fade_effect,
4+
write_clip,
5+
)
6+
37

48
def create_parser(subparser):
5-
parser = subparser.add_parser("fadeinout", description="Apply fadein-fadeout effects on videos ")
9+
parser = subparser.add_parser(
10+
"fadeinout", description="Apply fadein-fadeout effects on videos "
11+
)
612
# Add subprser arguments here.
713
parser.add_argument("video", help="Path to the input video file.")
8-
parser.add_argument("fade_type", choices=["fadein", "fadeout", "both"], help="Type of fade effect to apply.")
9-
parser.add_argument("-d", "--duration", type=float, default=1, help="Duration of the fade effect in seconds. (Default 1)")
10-
parser.add_argument("-o", "--output", default="output_video.mp4", help="Path for the output video file. Defaults to 'output_video.mp4'.")
14+
parser.add_argument(
15+
"fade_type",
16+
choices=["fadein", "fadeout", "both"],
17+
help="Type of fade effect to apply.",
18+
)
19+
parser.add_argument(
20+
"-d",
21+
"--duration",
22+
type=float,
23+
default=1,
24+
help="Duration of the fade effect in seconds. (Default 1)",
25+
)
26+
parser.add_argument(
27+
"-o",
28+
"--output",
29+
default="output_video.mp4",
30+
help="Path for the output video file. Defaults to 'output_video.mp4'.",
31+
)
1132

1233
return parser
1334

1435

1536
class ViztoolzPlugin:
16-
""" Apply fadein-fadeout effects on videos """
37+
"""Apply fadein-fadeout effects on videos"""
38+
1739
__name__ = "fadeinout"
1840

1941
@vidtoolz.hookimpl
2042
def register_commands(self, subparser):
2143
self.parser = create_parser(subparser)
2244
self.parser.set_defaults(func=self.run)
23-
45+
2446
def run(self, args):
2547
clip, fps = apply_fade_effect(args.video, args.fade_type, args.duration)
26-
write_clip(clip,fps, args.output)
27-
48+
write_clip(clip, fps, args.output)
49+
print(f"{args.output} written.")
50+
2851
def hello(self, args):
2952
# this routine will be called when "vidtoolz "fadeinout is called."
3053
print("Hello! This is an example ``vidtoolz`` plugin.")
3154

55+
3256
fadeinout_plugin = ViztoolzPlugin()

vidtoolz_apply_fadein_fadeout/applyfadeinfadeout.py

+12-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from moviepy import VideoFileClip
33
from moviepy import vfx
44

5+
56
def apply_fade_effect(video_path, fade_type, duration):
67
"""
78
Apply fadein or fadeout effect to a video and write the result to an output file.
@@ -11,30 +12,32 @@ def apply_fade_effect(video_path, fade_type, duration):
1112
fade_type (str): "fadein" or "fadeout" or "both".
1213
duration (float): Duration of the fade effect in seconds.
1314
"""
14-
# Load video clip
15-
clip = (
16-
VideoFileClip(video_path) if isinstance(video_path, str) else video_path
17-
)
15+
# Load video clip
16+
clip = VideoFileClip(video_path) if isinstance(video_path, str) else video_path
1817
fps = clip.fps
1918
# Apply fade effects
2019
if fade_type.lower() == "fadein":
2120
processed_clip = clip.with_effects([vfx.FadeIn(duration)])
2221
elif fade_type.lower() == "fadeout":
2322
processed_clip = clip.with_effects([vfx.FadeOut(duration)])
2423
elif fade_type.lower() == "both":
25-
processed_clip = clip.with_effects([vfx.FadeIn(duration), vfx.FadeOut(duration)])
24+
processed_clip = clip.with_effects(
25+
[vfx.FadeIn(duration), vfx.FadeOut(duration)]
26+
)
2627
else:
2728
raise ValueError("Invalid fade type. Choose 'fadein', 'fadeout', or 'both'.")
28-
29-
clip.close()
29+
3030
return processed_clip, fps
3131

32+
3233
def write_clip(processed_clip, fps, output_path):
3334
"""
3435
output_path (str): Path to save the output video.
3536
"""
3637
# Write the result to the output file. Using the same codec as input.
37-
processed_clip.write_videofile(output_path, codec="libx264", audio_codec="aac", fps=fps)
38+
has_audio = processed_clip.audio is not None
39+
processed_clip.write_videofile(
40+
output_path, codec="libx264", fps=fps, audio_codec="aac" if has_audio else None,
41+
)
3842
# Close the clips to free up resources
3943
processed_clip.close()
40-

0 commit comments

Comments
 (0)