### Install pyJianYingDraft using pip Source: https://github.com/guanyixuan/pyjianyingdraft/blob/main/README.md Install the pyJianYingDraft library using pip. This command installs the library without the demo files. ```bash pip install pyJianYingDraft ``` -------------------------------- ### Time Conversion and Range Creation Source: https://github.com/guanyixuan/pyjianyingdraft/blob/main/README.md Demonstrates the usage of time conversion functions like `tim` and `trange`. `tim` converts string time formats to microseconds, while `trange` creates Timerange objects, supporting string inputs for start and duration. ```python import pyJianYingDraft as draft from pyJianYingDraft import SEC, tim, trange # 1秒钟 assert 1000000 == SEC == tim("1s") == tim("0.01666667m") # 0~1分钟 assert draft.Timerange(0, 60*SEC) == trange("0s", "1m") == trange("0s", "0.5m30s") # 片段开始后2秒 seg: draft.VideoSegment assert seg.target_timerange.start + 2*SEC == seg.target_timerange.start + tim("2s") ``` -------------------------------- ### Get Effect Type by Name Source: https://github.com/guanyixuan/pyjianyingdraft/blob/main/README.md Retrieves a specific video scene effect type using its name. This method is case-insensitive and ignores spaces and underscores. ```python assert VideoSceneEffectType.from_name("__全息 扫描__") == VideoSceneEffectType.全息扫描 ``` -------------------------------- ### Create Video Segment with Clip Settings Source: https://github.com/guanyixuan/pyjianyingdraft/blob/main/README.md Shows how to create a video segment and apply clip settings such as opacity and horizontal flip using the `ClipSettings` constructor. ```python from pyJianYingDraft import ClipSettings video_segment = draft.VideoSegment(video_material, draft.Timerange(0, video_material.duration), # 与素材等长 clip_settings=ClipSettings(alpha=0.5, # 不透明度为0.5 flip_horizontal=True) # 打开水平翻转 ) ``` -------------------------------- ### Create Video Segments with Source and Target Timings Source: https://github.com/guanyixuan/pyjianyingdraft/blob/main/README.md Illustrates the creation of video segments using both convenient and traditional constructors. The convenient constructor takes a material path directly, while the traditional one requires pre-creating a material instance, which is necessary for setting material crop properties. ```python import os import pyJianYingDraft as draft from pyJianYingDraft import trange, SEC # 假定已有草稿文件script(参见“快速上手”),创建三个轨道 for i in range(3, 0, -1): # 倒序 script.add_track(draft.TrackType.video, "%d" % i) # 以下部分讲解素材与片段的创建 # 方式一:便捷构造(推荐) tutorial_asset_dir = os.path.join(os.path.dirname(__file__), 'readme_assets', 'tutorial') video_path = os.path.join(tutorial_asset_dir, 'video.mp4') # 直接传入素材路径 seg1 = draft.VideoSegment(video_path, trange("0s", "4s")) # 截取素材的前4秒 # 方式二:传统构造 mat = draft.VideoMaterial(video_path) # 先创建素材实例 seg2 = draft.VideoSegment(mat, trange("0s", "4s")) # 再传入片段构造函数 # 视频素材长度为 5s print("Video material length: %f s" % (mat.duration / SEC)) # 以下部分讲解素材的时间截取与变速 ``` -------------------------------- ### Create Video Segments with Time Ranges and Speed Source: https://github.com/guanyixuan/pyjianyingdraft/blob/main/README.md Demonstrates creating video segments by specifying source time ranges and playback speed. Handles cases where the source material might be shorter than the requested duration, leading to errors. ```python seg11 = draft.VideoSegment(video_path, trange("0s", "4s")) # 自动截取素材的前4秒(4s表示持续时长) seg2 = draft.VideoSegment(video_path, trange("0s", "4s"), speed=1.25) # 自动截取素材的前4*1.25=5秒 seg4 = draft.VideoSegment(video_path, trange("0s", "3s"), speed=3.0) # 截取前3*3.0=9秒,素材不够长故报错 ``` ```python seg12 = draft.VideoSegment(video_path, trange("4s", "1s"), source_timerange=trange(0, "4s")) # 将素材在1s内放完,速度自动设置为5.0 ``` ```python seg3 = draft.VideoSegment(video_path, trange("1s", "66666h"), source_timerange=trange(0, "5s"), speed=2.0) # 将长5s的素材按2倍速放完,target_timerange的duration自动设为2.5s ``` -------------------------------- ### Add Multiple Video Tracks with Custom Order Source: https://github.com/guanyixuan/pyjianyingdraft/blob/main/README.md Demonstrates how to add multiple video tracks to a script and specify their order using `relative_index`. Tracks with lower indices appear higher in the layering. ```python script.add_track(draft.TrackType.video, track_name="前景", # 轨道名 relative_index=2) # 在所有视频轨道中的相对位置 script.add_track(draft.TrackType.video, track_name="背景", relative_index=1) # 由于1<2,所以前景轨道位于更上方 ``` -------------------------------- ### Add Segments to Script and Save Draft Source: https://github.com/guanyixuan/pyjianyingdraft/blob/main/README.md Shows how to add created video segments to specific tracks in a script and then save the script as a draft file. ```python # 将片段加入轨道 script.add_segment(seg11, "1").add_segment(seg12, "1") script.add_segment(seg2, "2") script.add_segment(seg3, "3") # 保存草稿 script.dump("*你的草稿工程文件夹*/draft_content.json") ``` -------------------------------- ### Import SRT Subtitles with Style Reference and Clip Settings Source: https://github.com/guanyixuan/pyjianyingdraft/blob/main/README.md Imports an SRT subtitle file using a style reference and explicitly applying clip settings. If `clip_settings` is None, it uses the reference segment's clip settings. ```python # 默认不会采用`style_reference`片段中的`clip_settings`设置,如果需要的话请显式传入`clip_settings=None` script.import_srt("subtitle.srt", track_name="subtitle", style_reference=seg1, clip_settings=None) # 相当于clip_settings=seg1.clip_settings ``` -------------------------------- ### Set Video Mix Mode for Overlaying Tracks Source: https://github.com/guanyixuan/pyjianyingdraft/blob/main/README.md Explains how to set video mix modes (e.g., multiply, screen, overlay) for video segments to control blending with lower layers. Requires at least two video tracks with the overlay track positioned above the base track. ```python from pyJianYingDraft import MixModeType # 创建两个视频轨道,明确层次关系 script.add_track(draft.TrackType.video, "base", relative_index=1) # 基础轨道在下层 script.add_track(draft.TrackType.video, "overlay", relative_index=2) # 叠加轨道在上层 # 基础视频片段 base_video = draft.VideoSegment("base.mp4", trange("0s", "10s")) script.add_segment(base_video, track_name="base") ``` -------------------------------- ### Import SRT Subtitles with Offset Source: https://github.com/guanyixuan/pyjianyingdraft/blob/main/README.md Imports an SRT subtitle file into a specified track, with an optional time offset. If the track doesn't exist, it will be created. Default styling is applied. ```python import pyJianYingDraft as draft # 假定已有草稿文件script(参见“快速上手”) # 将字幕导入到名为"subtitle"的轨道中,若轨道不存在将自动创建 # 不指定style和clip_settings,则默认模拟剪映导入字幕时的样式 script.import_srt("subtitle.srt", track_name="subtitle", time_offset="1.5s") # 字幕整体后移1.5秒 ``` -------------------------------- ### Add Video Effect with Parameters Source: https://github.com/guanyixuan/pyjianyingdraft/blob/main/README.md Applies a video effect to a segment, specifying parameters. Use None for default values or parameters not to be set. Parameter order matters. ```python from pyJianYingDraft import VideoSceneEffectType video_segment.add_effect(VideoSceneEffectType.全息扫描, [None, None, 100.0]) # 不设置前两个参数, 第三个参数(氛围)为100,其余参数也不设置 ``` -------------------------------- ### Time Conversion and Formatting Source: https://github.com/guanyixuan/pyjianyingdraft/blob/main/README.md Provides utilities for handling time formats within the JianYing draft system. Supports both microsecond (int) and string formats for time representation. ```APIDOC ## Time Conversion and Formatting ### Description Provides utilities for handling time formats within the JianYing draft system. Supports both microsecond (int) and string formats for time representation. ### Utilities - `tim(time_str)`: Converts a string representation of time (e.g., "1.5s", "1h3m12s") to microseconds. - `trange(start_time_str, duration_str)`: Creates a `Timerange` object from string representations of start time and duration. ### Time Units - Microseconds: Internal representation using `int`. - String format: Human-readable format like `"1.5s"`, `"1h3m12s"`. ### Examples ```python import pyJianYingDraft as draft from pyJianYingDraft import SEC, tim, trange # Convert seconds to microseconds assert 1000000 == SEC == tim("1s") # Create a Timerange object assert draft.Timerange(0, 60*SEC) == trange("0s", "1m") # Adjusting time relative to a segment's start # seg: draft.VideoSegment # assert seg.target_timerange.start + 2*SEC == seg.target_timerange.start + tim("2s") ``` ### Response These are utility functions and do not return values in the context of an API call, but rather provide converted values or objects. ``` -------------------------------- ### Overlay Video with Mix Mode Source: https://github.com/guanyixuan/pyjianyingdraft/blob/main/README.md Applies a video segment as an overlay using a specified mix mode. Ensure the overlay video and its duration are correctly defined. ```python overlay_video = draft.VideoSegment("overlay.mp4", trange("0s", "10s")) overlay_video.set_mix_mode(MixModeType.滤色) script.add_segment(overlay_video, track_name="overlay") ``` -------------------------------- ### Load and duplicate a template draft Source: https://github.com/guanyixuan/pyjianyingdraft/blob/main/README.md Use DraftFolder to manage drafts and duplicate an existing draft to use as a template for a new script. The new script is opened for editing. ```python import pyJianYingDraft as draft draft_folder = draft.DraftFolder("<剪映草稿文件夹>") # 一般形如 ".../JianyingPro Drafts" script = draft_folder.duplicate_as_template("模板草稿", "新草稿") # 复制"模板草稿",并命名为"新草稿",同时打开新草稿供编辑 # 对返回的ScriptFile对象进行编辑,如替换素材、添加轨道、片段等 script.save() # 保存你的"新草稿" ``` -------------------------------- ### Segment Creation with Time Range and Speed Source: https://github.com/guanyixuan/pyjianyingdraft/blob/main/README.md Defines how video and audio segments are created, including specifying their target time range on the timeline and the source time range from the material. Also supports setting the playback speed. ```APIDOC ## Segment Creation with Time Range and Speed ### Description Defines how video and audio segments are created, including specifying their target time range on the timeline and the source time range from the material. Also supports setting the playback speed. ### Segment Types - `VideoSegment` - `AudioSegment` ### Construction Methods 1. **Convenience Constructor**: Directly pass the material path string. 2. **Traditional Constructor**: Create a material instance first, then pass it to the segment constructor. This method is required for setting material cropping properties. ### Parameters - `material`: Path to the material file or a material instance. - `target_timerange`: The time range the segment will occupy on the timeline. - `source_timerange` (Optional): The time range of the material to be used for the segment. Defaults to the entire material duration. - `speed` (Optional): The playback speed of the segment. Curve speed is not currently supported. ### Examples ```python import os import pyJianYingDraft as draft from pyJianYingDraft import trange, SEC # Assuming 'script' is an existing draft object # script.add_track(draft.TrackType.video, "%d" % i) tutorial_asset_dir = os.path.join(os.path.dirname(__file__), 'readme_assets', 'tutorial') video_path = os.path.join(tutorial_asset_dir, 'video.mp4') # Convenience constructor seg1 = draft.VideoSegment(video_path, trange("0s", "4s")) # Use the first 4 seconds of the material # Traditional constructor mat = draft.VideoMaterial(video_path) seg2 = draft.VideoSegment(mat, trange("0s", "4s")) print(f"Video material length: {mat.duration / SEC} s") ``` ### Response These methods construct segment objects. They do not have a direct API response in the traditional sense but return the created segment instances. ``` -------------------------------- ### Add Fade In/Out to Audio and Video Segments Source: https://github.com/guanyixuan/pyjianyingdraft/blob/main/README.md Demonstrates adding fade-in and fade-out effects to audio segments and video segments with audio tracks using the `add_fade` method. Specifies durations for fade-in and fade-out. ```python import pyJianYingDraft as draft from pyJianYingDraft import trange # 为音频片段添加淡入淡出 audio_segment = draft.AudioSegment("audio.mp3", trange("0s", "10s")) audio_segment.add_fade("1s", "2s") # 1秒淡入,2秒淡出 # 为带音轨的视频片段添加淡入淡出 video_segment = draft.VideoSegment("video_with_audio.mp4", trange("0s", "10s")) video_segment.add_fade("1.5s", "1.5s") # 1.5秒淡入,1.5秒淡出 ``` -------------------------------- ### Inspect material metadata from a draft Source: https://github.com/guanyixuan/pyjianyingdraft/blob/main/README.md Extract metadata such as resource IDs for stickers, text bubbles, and flower fonts from a JianYing draft. This information can be used to add similar assets. ```python import pyJianYingDraft as draft draft_folder = draft.DraftFolder("<剪映草稿文件夹>") draft_folder.inspect_material("草稿名称") # 或者 script = draft_folder.load_template("草稿名称") script.inspect_material() ``` -------------------------------- ### Create Text Segment with Style Source: https://github.com/guanyixuan/pyjianyingdraft/blob/main/README.md Creates a text segment with specified content, duration, font, style (size, color, underline, alignment), and clip settings (position). ```python import pyJianYingDraft as draft from pyJianYingDraft import FontType, TextStyle, ClipSettings # 带下划线、位置及大小类似字幕的浅蓝色文本 seg1 = draft.TextSegment("Subtitle", trange("0s", "10s"), font=FontType.文轩体, style=TextStyle(size=5.0, color=(0.7, 0.7, 1.0), underline=True, align=1), clip_settings=ClipSettings(transform_y=-0.8)) ``` -------------------------------- ### Add Keyframe for Audio Segment Volume Source: https://github.com/guanyixuan/pyjianyingdraft/blob/main/README.md Shows how to add a keyframe to an audio segment to control its volume at a specific point in time. The `KeyframeProperty` is not specified for audio volume. ```python audio_segment: draft.AudioSegment audio_segment.add_keyframe("0s", 0.6) # 片段开始时的音量为60% ``` -------------------------------- ### Import SRT Subtitles with Style Reference Source: https://github.com/guanyixuan/pyjianyingdraft/blob/main/README.md Imports an SRT subtitle file using another TextSegment as a style reference. Note that animation timing might not adjust to subtitle duration. ```python # 如果需要更复杂的样式或希望为字幕应用动画,可以为`style_reference`参数传入一个`TextSegment`对象作为样式参考(忽略其文本和片段长度设置) # 注意动画时间不会根据字幕片段长度进行调节,故当字幕片段过短时可能出现奇怪的效果 script.import_srt("subtitle.srt", track_name="subtitle", style_reference=seg1) # 以上一节“添加文本”中的文本作为参考 ``` -------------------------------- ### Add Segment to a Specific Track by Name Source: https://github.com/guanyixuan/pyjianyingdraft/blob/main/README.md Illustrates how to add a video segment to a specific track when multiple tracks of the same type exist, using the track's name. ```python script.add_segment(video_segment, "背景") ``` -------------------------------- ### Add Animations to Text Segment Source: https://github.com/guanyixuan/pyjianyingdraft/blob/main/README.md Applies entrance, exit, and loop animations to a text segment. For text, add exit/entrance animations before loop animations. Multiple animations can be chained. ```python from pyJianYingDraft import TextIntro, TextOutro, TextLoopAnim text_seg.add_animation(TextIntro.复古打字机).add_animation(TextOutro.弹簧) text_seg.add_animation(TextLoopAnim.色差故障) # 注意:循环动画必须在出入场动画之后添加 ``` -------------------------------- ### Import Track Source: https://github.com/guanyixuan/pyjianyingdraft/blob/main/README.md Imports a specified track from a template draft into the current draft. This is useful for combining multiple template drafts. Currently supports audio, video, and text tracks. ```APIDOC ## Import Track ### Description Imports a specified track from a template draft into the current draft. This is useful for combining multiple template drafts. Currently supports audio, video, and text tracks. ### Method `target_script.import_track` ### Parameters - `source_script`: The draft from which to import the track. - `track_to_import`: The track object to import. - `offset` (Optional): The time offset in the target draft where the imported track will start. Defaults to the end of the target draft's duration. - `new_name` (Optional): A new name for the imported track. - `relative_index` (Optional): The position of the imported track relative to other tracks of the same type. Higher values are closer to the foreground. ### Request Example ```python source_script = draft_folder.load_template("