@@ -41,7 +41,10 @@ def add_config_validator(settings: dynaconf.base.LazySettings):
41
41
42
42
43
43
def transcode (
44
- item : I , to_format : TranscodeFormat , out_path : Optional [Path ] = None
44
+ item : I ,
45
+ to_format : TranscodeFormat ,
46
+ out_path : Optional [Path ] = None ,
47
+ overwrite : bool = False ,
45
48
) -> I :
46
49
"""Transcodes a track or album to a specific format.
47
50
@@ -51,6 +54,7 @@ def transcode(
51
54
to_format: Format to transcode to.
52
55
out_path: Path of the transcoded item. This defaults to the formatted path per
53
56
the configuration relative to the ``transcode_path`` setting.
57
+ overwrite: Whether or not to overwrite existing files.
54
58
55
59
Returns:
56
60
The transcoded track or album.
@@ -62,23 +66,29 @@ def transcode(
62
66
out_path = out_path or fmt_item_path (item , transcode_path )
63
67
64
68
if isinstance (item , Album ):
65
- return _transcode_album (item , to_format , out_path )
66
- return _transcode_track (item , to_format , out_path )
69
+ return _transcode_album (item , to_format , out_path , overwrite )
70
+ return _transcode_track (item , to_format , out_path , overwrite )
67
71
68
72
69
- def _transcode_album (album : Album , to_format : TranscodeFormat , out_path : Path ) -> Album :
73
+ def _transcode_album (
74
+ album : Album , to_format : TranscodeFormat , out_path : Path , overwrite : bool = False
75
+ ) -> Album :
70
76
"""Transcodes an album to a specific format.
71
77
72
78
Args:
73
79
album: Album to transcode. Must contain flacs only.
74
80
to_format: Format to transcode to.
75
81
out_path: Path of the transcoded album. This defaults to the formatted path per
76
- the configuration relative to the ``transcode_path`` setting.
82
+ the configuration relative to the ``transcode_path`` setting. Track files
83
+ within the album will not be renamed except for any file extension changes.
84
+ overwrite: Whether or not to overwrite existing files.
77
85
78
86
Returns:
79
87
The transcoded album.
80
88
81
89
Raises:
90
+ FileExistsError: If ``overwrite`` is ``False`` and the output path for a track
91
+ already exists.
82
92
ValueError: ``album`` contains a non-supported audio format.
83
93
"""
84
94
log .debug (f"Transcoding album. [{ album = !r} , { to_format = !r} , { out_path = !r} ]" )
@@ -110,44 +120,59 @@ def _transcode_album(album: Album, to_format: TranscodeFormat, out_path: Path) -
110
120
return transcoded_album
111
121
112
122
113
- def _transcode_track (track : Track , to_format : TranscodeFormat , out_path : Path ) -> Track :
123
+ def _transcode_track (
124
+ track : Track , to_format : TranscodeFormat , out_path : Path , overwrite : bool = False
125
+ ) -> Track :
114
126
"""Transcodes a track to a specific format.
115
127
116
128
Args:
117
129
track: Track to transcode. The track will only be transcoded if it is a flac.
118
130
to_format: Format to transcode to.
119
131
out_path: Path of the transcoded track. This defaults to the formatted path per
120
132
the configuration relative to the ``transcode_path`` setting.
133
+ overwrite: Whether or not to overwrite existing files.
121
134
122
135
Returns:
123
136
The transcoded track.
124
137
125
138
Raises:
139
+ FileExistsError: If ``out_path`` already exists and ``overwrite`` is ``False``.
126
140
ValueError: ``track`` contains a non-supported audio format.
127
141
"""
128
142
log .debug (f"Transcoding track. [{ track = !r} , { to_format = !r} , { out_path = !r} ]" )
129
143
144
+ if out_path .exists () and not overwrite :
145
+ raise FileExistsError (f"Given output path already exists. [{ out_path = } ]" )
146
+
130
147
if not track .audio_format == "flac" :
131
148
raise ValueError (f"Track has unsupported audio format. [{ track = !r} ]" )
132
149
133
150
out_path .parent .mkdir (parents = True , exist_ok = True )
134
151
out_path = out_path .with_suffix (".mp3" )
135
152
136
- _transcode_path (track .path , to_format , out_path )
153
+ _transcode_path (track .path , to_format , out_path , overwrite )
137
154
138
155
transcoded_track = Track .from_file (out_path )
139
156
log .info (f"Transcoded track. [{ transcoded_track = !r} ]" )
140
157
141
158
return transcoded_track
142
159
143
160
144
- def _transcode_path (path : Path , to_format : TranscodeFormat , out_path : Path ) -> None :
161
+ def _transcode_path (
162
+ path : Path , to_format : TranscodeFormat , out_path : Path , overwrite : bool = False
163
+ ) -> None :
145
164
"""Transcodes a file to `to_format`.
146
165
147
166
This is a separate function in order to support multiprocessing.
148
167
"""
168
+ if overwrite :
169
+ overwrite_arg = "-y"
170
+ else :
171
+ overwrite_arg = "-n"
172
+
149
173
args = [
150
174
"ffmpeg" ,
175
+ overwrite_arg ,
151
176
"-loglevel" ,
152
177
"error" ,
153
178
"-i" ,
0 commit comments