Skip to content

Commit e07d895

Browse files
committed
Refactor plugin to work with video files also
1 parent 0bd710f commit e07d895

7 files changed

Lines changed: 44 additions & 12 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Transcode Audio Files
1+
# Transcode Audio
22
Plugin for [Unmanic](https://github.com/Unmanic)
33

44
---

changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11

2+
**<span style="color:#56adda">0.0.4</span>**
3+
- Rebrand the plugin as Transcode Audio and add support for transcoding audio streams in video/media files while copying non-audio streams and preserving the source container
4+
25
**<span style="color:#56adda">0.0.3</span>**
36
- Add smart output target to Basic mode so matching audio codecs can be re-encoded when they are significantly above the recommended bitrate window
47

description.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11

22
---
33

4+
Transcode Audio is designed to transcode audio streams in media files.
5+
6+
It supports both audio-only source files and video/media containers. When processing a video/media file, the
7+
plugin transcodes only the audio streams and copies all non-audio streams through to the output unchanged.
8+
49
##### Links:
510

611
- [Support](https://unmanic.app/discord)
@@ -22,8 +27,10 @@ For information on the available encoder settings:
2227
##### Additional Information:
2328

2429
:::note
25-
The output file extension will always be determined by the selected audio encoder.
26-
For example, selecting an MP3 encoder will always produce a *XXXX.mp3* file, and selecting an AAC encoder will always produce a *XXXX.m4a* file.
30+
For audio-only files, the output file extension is determined by the selected audio encoder.
31+
For example, selecting an MP3 encoder will produce a *XXXX.mp3* file, and selecting an AAC encoder will produce a *XXXX.m4a* file.
32+
33+
For video/media files, the plugin preserves the original container and only transcodes the audio streams.
2734
:::
2835

2936
:::note
@@ -43,10 +50,10 @@ ffmpeg \
4350
-hide_banner \
4451
-loglevel info \
4552
<CUSTOM MAIN OPTIONS HERE> \
46-
-i /path/to/input/audio.wav \
53+
-i /path/to/input/media.file \
4754
<CUSTOM ADVANCED OPTIONS HERE> \
4855
-map 0:0 -c:a:0 <CUSTOM AUDIO OPTIONS HERE> \
49-
-y /path/to/output/audio.mp3
56+
-y /path/to/output/media.file
5057
```
5158
:::
5259

@@ -57,5 +64,5 @@ Enabling the *"Force transcoding ..."* option will force a transcode of the audi
5764

5865
A file will only be forced to be transcoded once. It will then be flagged in a local `.unmanic` file to prevent it being added to the pending tasks list in a loop.
5966

60-
However, a file previously flagged to be ignored by this will still be transcoded to apply any matching smart filters such as scaling, stripping data streams, etc.
67+
However, a file previously flagged to be ignored by this will still be transcoded to apply any matching audio filter changes.
6168
:::

info.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
"compatibility": [
44
2
55
],
6-
"description": "Transcode the audio streams of a audio file",
6+
"description": "Transcode audio streams in audio and video media files",
77
"icon": "https://raw.githubusercontent.com/Unmanic/plugin.audio_transcoder/master/icon.png",
88
"id": "audio_transcoder",
9-
"name": "Transcode Audio Files",
9+
"name": "Transcode Audio",
1010
"priorities": {
1111
"on_library_management_file_test": 10,
1212
"on_worker_process": 1
1313
},
1414
"tags": "audio,ffmpeg",
15-
"version": "0.0.3"
15+
"version": "0.0.4"
1616
}

lib/plugin_stream_mapper.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def __init__(self, worker_log=None):
3737
self.worker_log = worker_log if isinstance(worker_log, list) else None
3838
self.abspath = None
3939
self.settings = None
40+
self.media_file_mode = 'audio_file'
4041
self.complex_audio_filters = {}
4142
self.forced_encode = False
4243
self.execution_stage = False
@@ -48,10 +49,12 @@ def set_default_values(self, settings, abspath, probe):
4849
self.set_probe(probe)
4950
self.set_input_file(abspath)
5051
self.settings = settings
52+
self.media_file_mode = tools.get_media_file_mode(abspath) or 'audio_file'
5153
tools.append_worker_log(
5254
self.worker_log,
53-
"Stream mapper configured (mode='{}', encoder='{}')".format(
55+
"Stream mapper configured (mode='{}', media_mode='{}', encoder='{}')".format(
5456
self.settings.get_setting('mode'),
57+
self.media_file_mode,
5558
self.settings.get_setting('audio_encoder'),
5659
)
5760
)
@@ -279,6 +282,10 @@ def custom_stream_mapping(self, stream_info: dict, stream_id: int):
279282
}
280283

281284
def set_output_file(self, path):
285+
if self.media_file_mode == 'video_file':
286+
self.output_file = os.path.abspath(path)
287+
return self.output_file
288+
282289
encoder_name = self.settings.get_setting('audio_encoder')
283290
encoder = tools.available_encoders(settings=self.settings, probe=self.probe).get(encoder_name)
284291
container_extension = encoder.get_output_file_extension(encoder_name)

lib/tools.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
If not, see <https://www.gnu.org/licenses/>.
2222
2323
"""
24+
import mimetypes
25+
2426
from audio_transcoder.lib.encoders.aac import AacEncoder
2527
from audio_transcoder.lib.encoders.lame import LameEncoder
2628

@@ -59,3 +61,16 @@ def join_filtergraph(filter_id, filter_args, stream_id):
5961
filtergraph += '[{}]'.format(filter_id)
6062
count += 1
6163
return filter_id, filtergraph
64+
65+
66+
def get_media_file_mode(path: str):
67+
file_type = mimetypes.guess_type(path)[0]
68+
if not file_type:
69+
return None
70+
71+
file_type_category = file_type.split('/')[0]
72+
if file_type_category == 'video':
73+
return 'video_file'
74+
if file_type_category == 'audio':
75+
return 'audio_file'
76+
return None

plugin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def on_library_management_file_test(data, task_data_store=None, file_metadata=No
128128
settings = Settings(library_id=data.get('library_id'), apply_default_fallbacks=False)
129129
abspath = data.get('path')
130130

131-
probe = Probe.init_probe(data, logger, allowed_mimetypes=['audio'])
131+
probe = Probe.init_probe(data, logger, allowed_mimetypes=['audio', 'video'])
132132
if not probe or not probe.file(abspath):
133133
return
134134

@@ -156,7 +156,7 @@ def on_worker_process(data, task_data_store=None, file_metadata=None):
156156
abspath = data.get('file_in')
157157

158158
tools.append_worker_log(worker_log, "Probing file: {}".format(abspath))
159-
probe = Probe(logger, allowed_mimetypes=['audio'])
159+
probe = Probe(logger, allowed_mimetypes=['audio', 'video'])
160160
if not probe.file(abspath):
161161
tools.append_worker_log(worker_log, "Probe failed - skipping file")
162162
return

0 commit comments

Comments
 (0)