5
5
'''
6
6
import tempfile
7
7
from pathlib import Path
8
- from typing import Dict , List , Optional , Union
8
+ from typing import Dict , List , NamedTuple , Optional , Union
9
9
10
10
from .. import constants
11
11
from .._ffmpeg import input , merge_outputs , output
12
- from .._utils import seconds_to_string
12
+ from .._utils import seconds_to_string , string_to_seconds
13
13
14
14
__all__ = [
15
15
"adjust_tempo" ,
@@ -64,11 +64,16 @@ def modify_metadata(src: Union[str, Path], dst: Union[str, Path], *,
64
64
65
65
66
66
def 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 ()
68
68
69
69
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 ()
72
77
73
78
74
79
def 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], *,
77
82
78
83
79
84
def 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 ):
82
87
"""Cut the video or audio into multiple parts.
83
88
84
89
Example:
@@ -98,11 +103,17 @@ def cut_into_multiple_parts(src: Union[str, Path], dst: Union[str, Path],
98
103
99
104
for order , duration in enumerate (durations ):
100
105
# skip negative value
101
- if duration is not None and duration < 0 :
106
+ if isinstance ( duration , ( int , float )) and duration < 0 :
102
107
start_position -= duration
103
108
continue
104
109
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 } " ,
106
117
acodec = "copy" , vcodec = vcodec , enable_cuda = enable_cuda ,
107
118
start_position = seconds_to_string (start_position ), duration = duration ))
108
119
@@ -112,6 +123,35 @@ def cut_into_multiple_parts(src: Union[str, Path], dst: Union[str, Path],
112
123
merge_outputs (* outs ).run (overwrite = overwrite )
113
124
114
125
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
+
115
155
def cut_one_part (src : Union [str , Path ], dst : Union [str , Path ], * , vcodec = "libx264" ,
116
156
enable_cuda = True , overwrite = True , start : Union [str , int , float ] = None ,
117
157
end : Union [str , int , float ] = None , duration : Union [int , float ] = None ,
0 commit comments