kapsikkum-unmanic – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3  
4 """
5 unmanic.audio_codec_handle.py
6  
7 Written by: Josh.5 <jsunnex@gmail.com>
8 Date: 21 Sep 2019, (7:53 AM)
9  
10 Copyright:
11 Copyright (C) Josh Sunnex - All Rights Reserved
12  
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19  
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22  
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
26 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
27 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
28 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
29 OR OTHER DEALINGS IN THE SOFTWARE.
30  
31 """
32  
33 from . import audio_codecs
34  
35  
36 class AudioCodecHandle(object):
37 """
38 AudioCodecHandle
39  
40 Handle FFMPEG operations pertaining to audio codec streams
41 """
42  
43 def __init__(self, file_probe):
44 self.file_probe = file_probe
45 self.encoding_args = {}
46 self.audio_tracks_count = 0
47  
48 # Configurable settings
49 self.disable_audio_encoding = False
50  
51 self.enable_audio_stream_transcoding = False
52 self.audio_codec_transcoding = 'aac' # Default to aac
53 self.audio_encoder_transcoding = 'aac' # Default to aac
54  
55 self.enable_audio_stream_stereo_cloning = False
56 self.audio_codec_cloning = 'aac' # Default to aac
57 self.audio_encoder_cloning = 'aac' # Default to aac
58 self.audio_stereo_stream_bitrate = '128k' # Default to 128k
59  
60 def copy_stream(self, stream):
61 """
62 Copy the audio stream. It will not be modified
63  
64 :param stream:
65 :return:
66 """
67 self.encoding_args['streams_to_encode'] = self.encoding_args['streams_to_encode'] + [
68 "-c:a:{}".format(self.audio_tracks_count), "copy"
69 ]
70 # Map this stream
71 self.encoding_args['streams_to_map'] = self.encoding_args['streams_to_map'] + [
72 "-map", "0:{}".format(stream['index'])
73 ]
74 self.audio_tracks_count += 1
75  
76 def transcode_stream(self, stream):
77 """
78 Transcode the audio stream to the configured audio codec
79  
80 :param stream:
81 :return:
82 """
83 self.encoding_args['streams_to_encode'] = self.encoding_args['streams_to_encode'] + [
84 "-c:a:{}".format(self.audio_tracks_count), self.audio_encoder_transcoding,
85 ]
86 # Map this stream
87 self.encoding_args['streams_to_map'] = self.encoding_args['streams_to_map'] + [
88 "-map", "0:{}".format(stream['index'])
89 ]
90 self.audio_tracks_count += 1
91  
92 def clone_stereo_stream(self, stream):
93 """
94 Generate a stereo clone of a given stream
95  
96 :param stream:
97 :return:
98 """
99 try:
100 audio_tag = ''.join([i for i in stream['tags']['title'] if not i.isdigit()]).rstrip(
101 '.') + 'Stereo'
102 except:
103 audio_tag = 'Stereo'
104  
105 # Map a duplicated stream
106 self.encoding_args['streams_to_map'] = self.encoding_args['streams_to_map'] + [
107 "-map", " 0:{}".format(stream['index'])
108 ]
109  
110 self.encoding_args['streams_to_encode'] = self.encoding_args['streams_to_encode'] + [
111 "-c:a:{}".format(self.audio_tracks_count), self.audio_encoder_cloning,
112 "-b:a:{}".format(self.audio_tracks_count), self.audio_stereo_stream_bitrate,
113 "-ac:a:{}".format(self.audio_tracks_count), "2",
114 "-metadata:s:a:{}".format(self.audio_tracks_count), "title='{}'".format(audio_tag),
115 ]
116 self.audio_tracks_count += 1
117  
118 def args(self):
119 """
120 Return a dictionary of streams to map and streams to encode
121 :return:
122 """
123 # Read stream data
124 self.encoding_args['streams_to_map'] = []
125 self.encoding_args['streams_to_encode'] = []
126 for stream in self.file_probe['streams']:
127 # If this is a audio stream, then process the args
128 if stream['codec_type'] == 'audio':
129  
130 if self.disable_audio_encoding:
131 # Audio re-encoding is disabled. Just copy the stream
132 self.copy_stream(stream)
133 else:
134 # Transcode stream if configured to do so
135 if self.enable_audio_stream_transcoding:
136 # If the current audio codec of this stream is the same as the configured
137 # destination codec, then do not re-encode this audio stream
138 if stream['codec_name'] == self.audio_codec_transcoding:
139 self.copy_stream(stream)
140 else:
141 self.transcode_stream(stream)
142 else:
143 self.copy_stream(stream)
144  
145 # If we have enabled stream cloning and this stream has more than 2 channels
146 if self.enable_audio_stream_stereo_cloning and stream['channels'] > 2:
147 self.clone_stereo_stream(stream)
148  
149 return self.encoding_args
150  
151 def set_audio_codec_with_default_encoder_cloning(self, codec_name):
152 """
153 Set the audio encoder for cloned streams
154  
155 :return:
156 """
157 codec = audio_codecs.grab_module(codec_name)
158 self.audio_codec_cloning = codec_name
159 self.audio_encoder_cloning = codec.codec_default_encoder()
160  
161 def set_audio_codec_with_default_encoder_transcoding(self, codec_name):
162 """
163 Set the audio encoder for transcoding streams
164  
165 :return:
166 """
167 codec = audio_codecs.grab_module(codec_name)
168 self.audio_codec_transcoding = codec_name
169 self.audio_encoder_transcoding = codec.codec_default_encoder()