ffmpeg-mcp-studio 0.2.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ffmpeg_mcp/__init__.py +6 -0
- ffmpeg_mcp/assets/__init__.py +0 -0
- ffmpeg_mcp/assets/fonts/Montserrat.ttf +0 -0
- ffmpeg_mcp/assets/fonts/OFL.txt +93 -0
- ffmpeg_mcp/composition/__init__.py +0 -0
- ffmpeg_mcp/composition/models.py +138 -0
- ffmpeg_mcp/composition/presets.py +201 -0
- ffmpeg_mcp/composition/renderer.py +315 -0
- ffmpeg_mcp/configs/__init__.py +3 -0
- ffmpeg_mcp/configs/logging_config.py +36 -0
- ffmpeg_mcp/core/__init__.py +0 -0
- ffmpeg_mcp/core/beats.py +51 -0
- ffmpeg_mcp/core/captions.py +124 -0
- ffmpeg_mcp/core/formats.py +30 -0
- ffmpeg_mcp/core/loudness.py +38 -0
- ffmpeg_mcp/core/paths.py +64 -0
- ffmpeg_mcp/core/probe.py +67 -0
- ffmpeg_mcp/core/runner.py +78 -0
- ffmpeg_mcp/core/silence.py +42 -0
- ffmpeg_mcp/core/styles.py +39 -0
- ffmpeg_mcp/core/text_render.py +143 -0
- ffmpeg_mcp/core/transcribe.py +101 -0
- ffmpeg_mcp/effects/__init__.py +0 -0
- ffmpeg_mcp/effects/audio.py +53 -0
- ffmpeg_mcp/effects/color.py +59 -0
- ffmpeg_mcp/effects/reframe.py +37 -0
- ffmpeg_mcp/effects/speed.py +96 -0
- ffmpeg_mcp/effects/text.py +113 -0
- ffmpeg_mcp/effects/zoom.py +57 -0
- ffmpeg_mcp/exceptions/__init__.py +3 -0
- ffmpeg_mcp/exceptions/build_exception_message.py +24 -0
- ffmpeg_mcp/main.py +97 -0
- ffmpeg_mcp/services/__init__.py +63 -0
- ffmpeg_mcp/services/add_background_music.py +64 -0
- ffmpeg_mcp/services/add_captions.py +79 -0
- ffmpeg_mcp/services/add_text_overlay.py +63 -0
- ffmpeg_mcp/services/apply_color_filter.py +38 -0
- ffmpeg_mcp/services/apply_zoom.py +43 -0
- ffmpeg_mcp/services/change_speed.py +40 -0
- ffmpeg_mcp/services/clip_video.py +73 -0
- ffmpeg_mcp/services/compose_video.py +252 -0
- ffmpeg_mcp/services/concat_clips_with_transition.py +114 -0
- ffmpeg_mcp/services/crop_video.py +75 -0
- ffmpeg_mcp/services/detect_beats.py +18 -0
- ffmpeg_mcp/services/export_for_platform.py +80 -0
- ffmpeg_mcp/services/extract_audio.py +45 -0
- ffmpeg_mcp/services/extract_frames.py +118 -0
- ffmpeg_mcp/services/get_video_metadata.py +31 -0
- ffmpeg_mcp/services/make_gif.py +61 -0
- ffmpeg_mcp/services/normalize_video_clips.py +119 -0
- ffmpeg_mcp/services/overlay_image.py +137 -0
- ffmpeg_mcp/services/overlays_video.py +95 -0
- ffmpeg_mcp/services/reframe_video.py +41 -0
- ffmpeg_mcp/services/remove_silences.py +54 -0
- ffmpeg_mcp/services/render_preview.py +67 -0
- ffmpeg_mcp/services/scale_video.py +70 -0
- ffmpeg_mcp/services/transcribe_audio.py +43 -0
- ffmpeg_mcp/services/trim_and_concatenate_video.py +137 -0
- ffmpeg_mcp/utils/__init__.py +4 -0
- ffmpeg_mcp/utils/calculate_video_offset.py +28 -0
- ffmpeg_mcp/utils/validate_input_video_path.py +37 -0
- ffmpeg_mcp_studio-0.2.0.dist-info/METADATA +368 -0
- ffmpeg_mcp_studio-0.2.0.dist-info/RECORD +66 -0
- ffmpeg_mcp_studio-0.2.0.dist-info/WHEEL +4 -0
- ffmpeg_mcp_studio-0.2.0.dist-info/entry_points.txt +3 -0
- ffmpeg_mcp_studio-0.2.0.dist-info/licenses/LICENSE +21 -0
ffmpeg_mcp/__init__.py
ADDED
|
File without changes
|
|
Binary file
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
Copyright 2024 The Montserrat.Git Project Authors (https://github.com/JulietaUla/Montserrat.git)
|
|
2
|
+
|
|
3
|
+
This Font Software is licensed under the SIL Open Font License, Version 1.1.
|
|
4
|
+
This license is copied below, and is also available with a FAQ at:
|
|
5
|
+
https://openfontlicense.org
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
-----------------------------------------------------------
|
|
9
|
+
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
|
|
10
|
+
-----------------------------------------------------------
|
|
11
|
+
|
|
12
|
+
PREAMBLE
|
|
13
|
+
The goals of the Open Font License (OFL) are to stimulate worldwide
|
|
14
|
+
development of collaborative font projects, to support the font creation
|
|
15
|
+
efforts of academic and linguistic communities, and to provide a free and
|
|
16
|
+
open framework in which fonts may be shared and improved in partnership
|
|
17
|
+
with others.
|
|
18
|
+
|
|
19
|
+
The OFL allows the licensed fonts to be used, studied, modified and
|
|
20
|
+
redistributed freely as long as they are not sold by themselves. The
|
|
21
|
+
fonts, including any derivative works, can be bundled, embedded,
|
|
22
|
+
redistributed and/or sold with any software provided that any reserved
|
|
23
|
+
names are not used by derivative works. The fonts and derivatives,
|
|
24
|
+
however, cannot be released under any other type of license. The
|
|
25
|
+
requirement for fonts to remain under this license does not apply
|
|
26
|
+
to any document created using the fonts or their derivatives.
|
|
27
|
+
|
|
28
|
+
DEFINITIONS
|
|
29
|
+
"Font Software" refers to the set of files released by the Copyright
|
|
30
|
+
Holder(s) under this license and clearly marked as such. This may
|
|
31
|
+
include source files, build scripts and documentation.
|
|
32
|
+
|
|
33
|
+
"Reserved Font Name" refers to any names specified as such after the
|
|
34
|
+
copyright statement(s).
|
|
35
|
+
|
|
36
|
+
"Original Version" refers to the collection of Font Software components as
|
|
37
|
+
distributed by the Copyright Holder(s).
|
|
38
|
+
|
|
39
|
+
"Modified Version" refers to any derivative made by adding to, deleting,
|
|
40
|
+
or substituting -- in part or in whole -- any of the components of the
|
|
41
|
+
Original Version, by changing formats or by porting the Font Software to a
|
|
42
|
+
new environment.
|
|
43
|
+
|
|
44
|
+
"Author" refers to any designer, engineer, programmer, technical
|
|
45
|
+
writer or other person who contributed to the Font Software.
|
|
46
|
+
|
|
47
|
+
PERMISSION & CONDITIONS
|
|
48
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
49
|
+
a copy of the Font Software, to use, study, copy, merge, embed, modify,
|
|
50
|
+
redistribute, and sell modified and unmodified copies of the Font
|
|
51
|
+
Software, subject to the following conditions:
|
|
52
|
+
|
|
53
|
+
1) Neither the Font Software nor any of its individual components,
|
|
54
|
+
in Original or Modified Versions, may be sold by itself.
|
|
55
|
+
|
|
56
|
+
2) Original or Modified Versions of the Font Software may be bundled,
|
|
57
|
+
redistributed and/or sold with any software, provided that each copy
|
|
58
|
+
contains the above copyright notice and this license. These can be
|
|
59
|
+
included either as stand-alone text files, human-readable headers or
|
|
60
|
+
in the appropriate machine-readable metadata fields within text or
|
|
61
|
+
binary files as long as those fields can be easily viewed by the user.
|
|
62
|
+
|
|
63
|
+
3) No Modified Version of the Font Software may use the Reserved Font
|
|
64
|
+
Name(s) unless explicit written permission is granted by the corresponding
|
|
65
|
+
Copyright Holder. This restriction only applies to the primary font name as
|
|
66
|
+
presented to the users.
|
|
67
|
+
|
|
68
|
+
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
|
|
69
|
+
Software shall not be used to promote, endorse or advertise any
|
|
70
|
+
Modified Version, except to acknowledge the contribution(s) of the
|
|
71
|
+
Copyright Holder(s) and the Author(s) or with their explicit written
|
|
72
|
+
permission.
|
|
73
|
+
|
|
74
|
+
5) The Font Software, modified or unmodified, in part or in whole,
|
|
75
|
+
must be distributed entirely under this license, and must not be
|
|
76
|
+
distributed under any other license. The requirement for fonts to
|
|
77
|
+
remain under this license does not apply to any document created
|
|
78
|
+
using the Font Software.
|
|
79
|
+
|
|
80
|
+
TERMINATION
|
|
81
|
+
This license becomes null and void if any of the above conditions are
|
|
82
|
+
not met.
|
|
83
|
+
|
|
84
|
+
DISCLAIMER
|
|
85
|
+
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
86
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
|
|
87
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
|
|
88
|
+
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
|
|
89
|
+
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
90
|
+
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
|
|
91
|
+
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
92
|
+
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
|
|
93
|
+
OTHER DEALINGS IN THE FONT SOFTWARE.
|
|
File without changes
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
from typing import Literal
|
|
2
|
+
|
|
3
|
+
from pydantic import BaseModel, Field, model_validator
|
|
4
|
+
|
|
5
|
+
from ffmpeg_mcp.core.formats import FormatName, PlatformName
|
|
6
|
+
from ffmpeg_mcp.core.styles import TextStyleName
|
|
7
|
+
from ffmpeg_mcp.effects.color import ColorPreset
|
|
8
|
+
from ffmpeg_mcp.effects.reframe import ReframeMode
|
|
9
|
+
from ffmpeg_mcp.effects.text import Position, TextAnimation
|
|
10
|
+
|
|
11
|
+
IMAGE_EXTENSIONS = ('.jpg', '.jpeg', '.png', '.webp', '.bmp', '.tif', '.tiff')
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class SpeedSegment(BaseModel):
|
|
15
|
+
"""Speed change for part of a clip. Times are seconds from the clip's trimmed start (before speed changes)."""
|
|
16
|
+
|
|
17
|
+
start: float = Field(ge=0)
|
|
18
|
+
end: float = Field(gt=0)
|
|
19
|
+
speed: float | None = Field(default=None, description='Constant speed for the segment, e.g. 0.5 = slow motion, 2 = double speed')
|
|
20
|
+
speed_start: float | None = Field(default=None, description='Gradual ramp: speed at segment start')
|
|
21
|
+
speed_end: float | None = Field(default=None, description='Gradual ramp: speed at segment end')
|
|
22
|
+
|
|
23
|
+
@model_validator(mode='after')
|
|
24
|
+
def _check(self):
|
|
25
|
+
if self.speed is None and (self.speed_start is None or self.speed_end is None):
|
|
26
|
+
raise ValueError('Give either speed, or both speed_start and speed_end')
|
|
27
|
+
return self
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class ZoomSegment(BaseModel):
|
|
31
|
+
"""Punch-in zoom. Times are seconds in the clip's output (after trimming and speed changes)."""
|
|
32
|
+
|
|
33
|
+
start: float = Field(ge=0)
|
|
34
|
+
end: float = Field(gt=0)
|
|
35
|
+
zoom: float = Field(default=1.2, ge=1.0, le=4.0)
|
|
36
|
+
center_x: float = Field(default=0.5, ge=0, le=1, description='Zoom focus, fraction of width')
|
|
37
|
+
center_y: float = Field(default=0.5, ge=0, le=1, description='Zoom focus, fraction of height')
|
|
38
|
+
ease: Literal['smooth', 'snap'] = 'smooth'
|
|
39
|
+
ramp: float = Field(default=0.3, ge=0, description='Seconds to ease in/out when ease="smooth"')
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class Transition(BaseModel):
|
|
43
|
+
type: str = Field(
|
|
44
|
+
default='fade',
|
|
45
|
+
description='xfade transition name (fade, wipeleft, slideup, circleopen, dissolve, zoomin, ...) or "none" for a hard cut',
|
|
46
|
+
)
|
|
47
|
+
duration: float = Field(default=0.4, gt=0, le=3)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class ClipSpec(BaseModel):
|
|
51
|
+
path: str = Field(description='Video or image file. Images become still clips (with a gentle ken-burns zoom by default).')
|
|
52
|
+
start: float = Field(default=0.0, ge=0, description='Trim start (seconds in the source)')
|
|
53
|
+
end: float | None = Field(default=None, description='Trim end (seconds in the source); None = end of file')
|
|
54
|
+
image_duration: float = Field(default=3.0, gt=0, description='Length of an image clip')
|
|
55
|
+
speed: float = Field(default=1.0, ge=0.1, le=10)
|
|
56
|
+
speed_ramp: list[SpeedSegment] | None = None
|
|
57
|
+
zoom: list[ZoomSegment] | None = None
|
|
58
|
+
ken_burns: float | None = Field(default=None, ge=1.0, le=2.0, description='Slow zoom reaching this factor by the clip end, e.g. 1.1')
|
|
59
|
+
color: ColorPreset | None = None
|
|
60
|
+
volume: float = Field(default=1.0, ge=0, le=4)
|
|
61
|
+
mute: bool = False
|
|
62
|
+
reframe_mode: ReframeMode | None = Field(default=None, description='Override the composition reframe_mode for this clip')
|
|
63
|
+
focus_x: float = Field(default=0.5, ge=0, le=1, description='Horizontal focus when cropping to the target format')
|
|
64
|
+
focus_y: float = Field(default=0.5, ge=0, le=1)
|
|
65
|
+
transition_in: Transition | None = Field(default=None, description='Transition from the previous clip; overrides default_transition')
|
|
66
|
+
|
|
67
|
+
@property
|
|
68
|
+
def is_image(self) -> bool:
|
|
69
|
+
return self.path.lower().endswith(IMAGE_EXTENSIONS)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
class TextOverlay(BaseModel):
|
|
73
|
+
text: str
|
|
74
|
+
start: float = Field(default=0.0, ge=0, description='Seconds on the final timeline')
|
|
75
|
+
duration: float = Field(default=3.0, gt=0)
|
|
76
|
+
style: TextStyleName = 'title'
|
|
77
|
+
position: Position = 'top_center'
|
|
78
|
+
x: float | None = Field(default=None, ge=0, le=1, description='Explicit centre x (fraction of width); needs y too')
|
|
79
|
+
y: float | None = Field(default=None, ge=0, le=1)
|
|
80
|
+
animation: TextAnimation = 'fade'
|
|
81
|
+
color: str | None = Field(default=None, description='Override text colour, e.g. #FFE600')
|
|
82
|
+
size: float | None = Field(default=None, gt=0, le=0.3, description='Override font size as a fraction of video height')
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
class ImageOverlay(BaseModel):
|
|
86
|
+
path: str
|
|
87
|
+
start: float = Field(default=0.0, ge=0)
|
|
88
|
+
duration: float | None = Field(default=None, gt=0, description='None = until the end')
|
|
89
|
+
position: Position = 'top_right'
|
|
90
|
+
x: float | None = Field(default=None, ge=0, le=1)
|
|
91
|
+
y: float | None = Field(default=None, ge=0, le=1)
|
|
92
|
+
width: int | None = Field(default=200, gt=0, description='Width in px (aspect ratio kept); None = original size')
|
|
93
|
+
opacity: float = Field(default=1.0, ge=0, le=1)
|
|
94
|
+
animation: TextAnimation = 'fade'
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
class Captions(BaseModel):
|
|
98
|
+
enabled: bool = True
|
|
99
|
+
style: TextStyleName = 'bold_pop'
|
|
100
|
+
mode: Literal['phrase_highlight', 'word', 'phrase'] = 'phrase_highlight'
|
|
101
|
+
words_per_line: int = Field(default=3, ge=1, le=8)
|
|
102
|
+
position: Position = 'caption'
|
|
103
|
+
transcript_path: str | None = Field(
|
|
104
|
+
default=None, description='Use this transcript (.json from transcribe_audio, or .srt) instead of auto-transcribing'
|
|
105
|
+
)
|
|
106
|
+
model_size: str = Field(default='base', description='faster-whisper model: tiny, base, small, medium, large-v3')
|
|
107
|
+
language: str | None = Field(default=None, description='ISO code, e.g. "en"; None = auto-detect')
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
class Music(BaseModel):
|
|
111
|
+
path: str
|
|
112
|
+
volume: float = Field(default=0.25, ge=0, le=2)
|
|
113
|
+
duck: bool = Field(default=True, description='Lower the music automatically while there is speech')
|
|
114
|
+
fade_in: float = Field(default=0.5, ge=0)
|
|
115
|
+
fade_out: float = Field(default=1.5, ge=0)
|
|
116
|
+
start_offset: float = Field(default=0.0, ge=0, description='Start the song from this many seconds in')
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
class Export(BaseModel):
|
|
120
|
+
platform: PlatformName = 'instagram_reel'
|
|
121
|
+
crf: int = Field(default=20, ge=14, le=32)
|
|
122
|
+
preset: Literal['ultrafast', 'veryfast', 'faster', 'fast', 'medium', 'slow'] = 'medium'
|
|
123
|
+
normalize_loudness: bool = True
|
|
124
|
+
target_lufs: float = Field(default=-14.0, ge=-30, le=-8)
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
class Composition(BaseModel):
|
|
128
|
+
format: FormatName = 'reel'
|
|
129
|
+
fps: int = Field(default=30, ge=12, le=60)
|
|
130
|
+
reframe_mode: ReframeMode = 'crop'
|
|
131
|
+
clips: list[ClipSpec] = Field(min_length=1)
|
|
132
|
+
default_transition: Transition | None = Field(default=None, description='Transition between every pair of clips; None = hard cuts')
|
|
133
|
+
text_overlays: list[TextOverlay] = Field(default_factory=list)
|
|
134
|
+
image_overlays: list[ImageOverlay] = Field(default_factory=list)
|
|
135
|
+
captions: Captions | None = None
|
|
136
|
+
music: Music | None = None
|
|
137
|
+
color: ColorPreset | None = Field(default=None, description='Grade applied to every clip without its own color')
|
|
138
|
+
export: Export = Field(default_factory=Export)
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
"""One-call recipes. Each builds a Composition spec and renders it with the composition engine."""
|
|
2
|
+
|
|
3
|
+
from ffmpeg_mcp.composition.models import Captions, ClipSpec, Composition, Music, TextOverlay, Transition, ZoomSegment
|
|
4
|
+
from ffmpeg_mcp.composition.renderer import render
|
|
5
|
+
from ffmpeg_mcp.core.beats import detect_beats
|
|
6
|
+
from ffmpeg_mcp.core.probe import probe_media
|
|
7
|
+
from ffmpeg_mcp.core.silence import detect_silences, speech_ranges
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def _transition(name: str | None, duration: float) -> Transition | None:
|
|
11
|
+
if not name or name == 'none':
|
|
12
|
+
return None
|
|
13
|
+
return Transition(type=name, duration=duration)
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def _title(title: str | None, total: float) -> list[TextOverlay]:
|
|
17
|
+
if not title:
|
|
18
|
+
return []
|
|
19
|
+
return [
|
|
20
|
+
TextOverlay(
|
|
21
|
+
text=title, start=0.0, duration=max(0.5, min(3.0, total - 0.1)), style='title', position='top_center', animation='slide_up'
|
|
22
|
+
)
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def build_reel_from_clips(
|
|
27
|
+
clips: list[str],
|
|
28
|
+
music_path: str | None = None,
|
|
29
|
+
title: str | None = None,
|
|
30
|
+
captions: bool = True,
|
|
31
|
+
caption_style: str = 'bold_pop',
|
|
32
|
+
transition: str | None = 'fade',
|
|
33
|
+
transition_duration: float = 0.4,
|
|
34
|
+
color: str | None = None,
|
|
35
|
+
target_duration: float | None = None,
|
|
36
|
+
format: str = 'reel',
|
|
37
|
+
reframe_mode: str = 'crop',
|
|
38
|
+
) -> Composition:
|
|
39
|
+
if not clips:
|
|
40
|
+
raise ValueError('Provide at least one clip')
|
|
41
|
+
specs: list[ClipSpec] = []
|
|
42
|
+
lengths: list[float] = []
|
|
43
|
+
any_audio = False
|
|
44
|
+
for path in clips:
|
|
45
|
+
spec = ClipSpec(path=path)
|
|
46
|
+
if spec.is_image:
|
|
47
|
+
lengths.append(spec.image_duration)
|
|
48
|
+
else:
|
|
49
|
+
info = probe_media(path)
|
|
50
|
+
any_audio = any_audio or info.has_audio
|
|
51
|
+
lengths.append(info.duration)
|
|
52
|
+
specs.append(spec)
|
|
53
|
+
|
|
54
|
+
trans = _transition(transition, transition_duration)
|
|
55
|
+
overlap = (trans.duration if trans else 0.0) * (len(specs) - 1)
|
|
56
|
+
total = sum(lengths) - overlap
|
|
57
|
+
if target_duration and total > target_duration:
|
|
58
|
+
# Shorten every clip by the same factor, keeping each clip's beginning.
|
|
59
|
+
budget = target_duration + overlap
|
|
60
|
+
factor = budget / sum(lengths)
|
|
61
|
+
for spec, length in zip(specs, lengths):
|
|
62
|
+
keep = max(0.5, length * factor)
|
|
63
|
+
if spec.is_image:
|
|
64
|
+
spec.image_duration = keep
|
|
65
|
+
else:
|
|
66
|
+
spec.end = keep
|
|
67
|
+
total = target_duration
|
|
68
|
+
|
|
69
|
+
music = None
|
|
70
|
+
if music_path:
|
|
71
|
+
music = Music(path=music_path, volume=0.25 if any_audio else 0.9, duck=any_audio)
|
|
72
|
+
return Composition(
|
|
73
|
+
format=format,
|
|
74
|
+
reframe_mode=reframe_mode,
|
|
75
|
+
clips=specs,
|
|
76
|
+
default_transition=trans,
|
|
77
|
+
text_overlays=_title(title, total),
|
|
78
|
+
captions=Captions(style=caption_style) if (captions and any_audio) else None,
|
|
79
|
+
music=music,
|
|
80
|
+
color=color,
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def build_beat_sync_montage(
|
|
85
|
+
clips: list[str],
|
|
86
|
+
music_path: str,
|
|
87
|
+
cut_every_n_beats: int = 2,
|
|
88
|
+
target_duration: float = 15.0,
|
|
89
|
+
music_start_offset: float = 0.0,
|
|
90
|
+
zoom_on_downbeats: bool = True,
|
|
91
|
+
transition: str | None = None,
|
|
92
|
+
color: str | None = None,
|
|
93
|
+
title: str | None = None,
|
|
94
|
+
format: str = 'reel',
|
|
95
|
+
fps: int = 30,
|
|
96
|
+
) -> tuple[Composition, dict]:
|
|
97
|
+
if not clips:
|
|
98
|
+
raise ValueError('Provide at least one clip')
|
|
99
|
+
if cut_every_n_beats < 1:
|
|
100
|
+
raise ValueError('cut_every_n_beats must be >= 1')
|
|
101
|
+
beats = detect_beats(music_path, start_offset=music_start_offset, max_duration=target_duration + 10)
|
|
102
|
+
times = beats['beats']
|
|
103
|
+
if len(times) < 2:
|
|
104
|
+
raise ValueError('Not enough beats detected in the music; try a different track or start offset')
|
|
105
|
+
|
|
106
|
+
first = times[0]
|
|
107
|
+
rel = [t - first for t in times]
|
|
108
|
+
downbeats = {round(t - first, 2) for t in beats['downbeats']}
|
|
109
|
+
cuts = [t for t in rel[::cut_every_n_beats] if t <= target_duration + 1e-3]
|
|
110
|
+
if len(cuts) < 2:
|
|
111
|
+
raise ValueError(f'target_duration {target_duration}s is shorter than {cut_every_n_beats} beats at {beats["tempo"]} BPM')
|
|
112
|
+
frames = [round(c * fps) for c in cuts]
|
|
113
|
+
seg_lengths = [(frames[i + 1] - frames[i]) / fps for i in range(len(frames) - 1)]
|
|
114
|
+
|
|
115
|
+
infos = {}
|
|
116
|
+
for path in clips:
|
|
117
|
+
if not ClipSpec(path=path).is_image:
|
|
118
|
+
infos[path] = probe_media(path)
|
|
119
|
+
cursors = {path: 0.0 for path in clips}
|
|
120
|
+
specs: list[ClipSpec] = []
|
|
121
|
+
trans = _transition(transition, 0.15)
|
|
122
|
+
for k, seg in enumerate(seg_lengths):
|
|
123
|
+
path = clips[k % len(clips)]
|
|
124
|
+
zoom = None
|
|
125
|
+
if zoom_on_downbeats and round(cuts[k], 2) in downbeats:
|
|
126
|
+
zoom = [ZoomSegment(start=0.0, end=max(0.1, seg * 0.6), zoom=1.15, ease='smooth', ramp=0.06)]
|
|
127
|
+
if path not in infos:
|
|
128
|
+
specs.append(ClipSpec(path=path, image_duration=seg, mute=True, zoom=zoom, transition_in=trans))
|
|
129
|
+
continue
|
|
130
|
+
dur = infos[path].duration
|
|
131
|
+
if seg >= dur - 0.05:
|
|
132
|
+
# Source shorter than the beat gap: slow it down to fill the gap.
|
|
133
|
+
specs.append(ClipSpec(path=path, start=0.0, end=dur, speed=max(0.1, dur / seg), mute=True, zoom=zoom, transition_in=trans))
|
|
134
|
+
continue
|
|
135
|
+
start = cursors[path]
|
|
136
|
+
if start + seg > dur:
|
|
137
|
+
start = 0.0
|
|
138
|
+
cursors[path] = start + seg
|
|
139
|
+
specs.append(ClipSpec(path=path, start=start, end=start + seg, mute=True, zoom=zoom, transition_in=trans))
|
|
140
|
+
|
|
141
|
+
total = (frames[-1] - frames[0]) / fps
|
|
142
|
+
comp = Composition(
|
|
143
|
+
format=format,
|
|
144
|
+
fps=fps,
|
|
145
|
+
clips=specs,
|
|
146
|
+
text_overlays=_title(title, total),
|
|
147
|
+
music=Music(path=music_path, volume=1.0, duck=False, fade_in=0.0, fade_out=1.0, start_offset=music_start_offset + first),
|
|
148
|
+
color=color,
|
|
149
|
+
)
|
|
150
|
+
return comp, {'tempo': beats['tempo'], 'cuts': len(specs), 'cut_points': [round(c, 3) for c in cuts]}
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def build_talking_head_reel(
|
|
154
|
+
video_path: str,
|
|
155
|
+
caption_style: str = 'karaoke',
|
|
156
|
+
caption_mode: str = 'phrase_highlight',
|
|
157
|
+
remove_silence: bool = False,
|
|
158
|
+
auto_punch_in_every: float | None = 4.0,
|
|
159
|
+
punch_in_zoom: float = 1.15,
|
|
160
|
+
music_path: str | None = None,
|
|
161
|
+
music_volume: float = 0.12,
|
|
162
|
+
title: str | None = None,
|
|
163
|
+
color: str | None = None,
|
|
164
|
+
focus_x: float = 0.5,
|
|
165
|
+
format: str = 'reel',
|
|
166
|
+
) -> Composition:
|
|
167
|
+
info = probe_media(video_path)
|
|
168
|
+
if not info.has_audio:
|
|
169
|
+
raise ValueError('talking_head_reel needs a video with speech audio')
|
|
170
|
+
specs: list[ClipSpec] = []
|
|
171
|
+
if remove_silence:
|
|
172
|
+
ranges = speech_ranges(info.duration, detect_silences(video_path))
|
|
173
|
+
if not ranges:
|
|
174
|
+
raise ValueError('No speech detected - try remove_silence=False')
|
|
175
|
+
for i, (s, e) in enumerate(ranges):
|
|
176
|
+
zoom = [ZoomSegment(start=0.0, end=e - s, zoom=punch_in_zoom, ease='snap')] if (auto_punch_in_every and i % 2 == 1) else None
|
|
177
|
+
specs.append(ClipSpec(path=video_path, start=s, end=e, zoom=zoom, focus_x=focus_x))
|
|
178
|
+
total = sum(e - s for s, e in ranges)
|
|
179
|
+
else:
|
|
180
|
+
zooms = []
|
|
181
|
+
if auto_punch_in_every and auto_punch_in_every > 0:
|
|
182
|
+
t, k = 0.0, 0
|
|
183
|
+
while t < info.duration:
|
|
184
|
+
end = min(info.duration, t + auto_punch_in_every)
|
|
185
|
+
if k % 2 == 1 and end - t > 0.2:
|
|
186
|
+
zooms.append(ZoomSegment(start=t, end=end, zoom=punch_in_zoom, ease='snap'))
|
|
187
|
+
t, k = end, k + 1
|
|
188
|
+
specs.append(ClipSpec(path=video_path, zoom=zooms or None, focus_x=focus_x))
|
|
189
|
+
total = info.duration
|
|
190
|
+
|
|
191
|
+
return Composition(
|
|
192
|
+
format=format,
|
|
193
|
+
clips=specs,
|
|
194
|
+
text_overlays=_title(title, total),
|
|
195
|
+
captions=Captions(style=caption_style, mode=caption_mode),
|
|
196
|
+
music=Music(path=music_path, volume=music_volume, duck=True) if music_path else None,
|
|
197
|
+
color=color,
|
|
198
|
+
)
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
__all__ = ['build_reel_from_clips', 'build_beat_sync_montage', 'build_talking_head_reel', 'render']
|