55'''
66import tempfile
77from pathlib import Path
8- from typing import Dict , List , Optional , Union
8+ from typing import Dict , List , NamedTuple , Optional , Union
99
1010from .. import constants
1111from .._ffmpeg import input , merge_outputs , output
12- from .._utils import seconds_to_string
12+ from .._utils import seconds_to_string , string_to_seconds
1313
1414__all__ = [
1515 "adjust_tempo" ,
@@ -64,11 +64,16 @@ def modify_metadata(src: Union[str, Path], dst: Union[str, Path], *,
6464
6565
6666def separate_video_stream (src : Union [str , Path ], dst : Union [str , Path ]):
67- input (src ).output (dst , an = True , vcodec = "copy" ).run ()
67+ input (src , enable_cuda = False ).output (dst , an = True , enable_cuda = False , vcodec = "copy" ).run ()
6868
6969
70- def separate_audio_stream (src : Union [str , Path ], dst : Union [str , Path ]):
71- input (src ).output (dst , vn = True , acodec = "copy" ).run ()
70+ def separate_audio_stream (src : Union [str , Path ], dst : Union [str , Path ], pcm_format = False ):
71+ if pcm_format :
72+ kwargs = dict (format = constants .S16LE , acodec = constants .PCM_S16LE , ac = 1 , ar = "16k" )
73+ else :
74+ kwargs = dict (acodec = constants .COPY )
75+
76+ input (src , enable_cuda = False ).output (dst , vn = True , enable_cuda = False , ** kwargs ).run ()
7277
7378
7479def convert_format (src : Union [str , Path ], dst : Union [str , Path ], * ,
@@ -77,8 +82,8 @@ def convert_format(src: Union[str, Path], dst: Union[str, Path], *,
7782
7883
7984def cut_into_multiple_parts (src : Union [str , Path ], dst : Union [str , Path ],
80- * , durations : List [float ], vcodec = "libx264" ,
81- enable_cuda = True , overwrite = True ):
85+ * , durations : List [Union [ float , int , str ] ], vcodec = "libx264" ,
86+ enable_cuda = True , overwrite = True , accumulative = False ):
8287 """Cut the video or audio into multiple parts.
8388
8489 Example:
@@ -98,11 +103,17 @@ def cut_into_multiple_parts(src: Union[str, Path], dst: Union[str, Path],
98103
99104 for order , duration in enumerate (durations ):
100105 # skip negative value
101- if duration is not None and duration < 0 :
106+ if isinstance ( duration , ( int , float )) and duration < 0 :
102107 start_position -= duration
103108 continue
104109
105- outs .append (raw .output (f"{ dst / path .stem } _part{ order } { path .suffix } " ,
110+ if isinstance (duration , str ):
111+ duration = string_to_seconds (duration )
112+
113+ if isinstance (duration , (int , float )) and accumulative :
114+ duration -= start_position
115+
116+ outs .append (raw .output (f"{ dst / path .stem } _{ order } { path .suffix } " ,
106117 acodec = "copy" , vcodec = vcodec , enable_cuda = enable_cuda ,
107118 start_position = seconds_to_string (start_position ), duration = duration ))
108119
@@ -112,6 +123,35 @@ def cut_into_multiple_parts(src: Union[str, Path], dst: Union[str, Path],
112123 merge_outputs (* outs ).run (overwrite = overwrite )
113124
114125
126+ class TrimPair (NamedTuple ):
127+ Start : Union [str , int , float ]
128+ End : Union [str , int , float ]
129+ IsDuration : bool = False
130+
131+
132+ def cut_into_multiple_parts_v2 (src : Union [str , Path ], dst : Union [str , Path ],
133+ * , start_duration_pairs : List [TrimPair ], vcodec = "libx264" ,
134+ enable_cuda = True , overwrite = True ):
135+ outs = []
136+ path = Path (src )
137+ raw = input (src , enable_cuda = enable_cuda )
138+
139+ for order , pair in enumerate (start_duration_pairs ):
140+ start_position = string_to_seconds (pair .Start )
141+ end_position = string_to_seconds (pair .End )
142+
143+ if not pair .IsDuration and end_position > start_position :
144+ duration = end_position - start_position
145+ else :
146+ duration = end_position
147+
148+ outs .append (raw .output (f"{ dst / path .stem } _{ order } { path .suffix } " ,
149+ acodec = "copy" , vcodec = vcodec , enable_cuda = enable_cuda ,
150+ start_position = start_position , duration = duration ))
151+
152+ merge_outputs (* outs ).run (overwrite = overwrite )
153+
154+
115155def cut_one_part (src : Union [str , Path ], dst : Union [str , Path ], * , vcodec = "libx264" ,
116156 enable_cuda = True , overwrite = True , start : Union [str , int , float ] = None ,
117157 end : Union [str , int , float ] = None , duration : Union [int , float ] = None ,
0 commit comments