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.subtitle_handle.py
6  
7 Written by: Josh.5 <jsunnex@gmail.com>
8 Date: 19 Sep 2019, (5:23 PM)
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  
34 class SubtitleHandle(object):
35 """
36 SubtitleHandle
37  
38 Handle FFMPEG operations pertaining to subtitle streams
39 """
40  
41 def __init__(self, file_probe, container):
42 self.file_probe = file_probe
43 self.container = container
44 self.subtitle_args = {}
45  
46 # Configurable settings
47 self.remove_subtitle_streams = False
48  
49 # Check if destination container supports subtitles
50 if not container.container_supports_subtitles():
51 # Destination container does not support subtitles,
52 # Force them to be removed
53 self.remove_subtitle_streams = True
54  
55 def args(self):
56 """
57 Return a dictionary of streams to map and streams to encode
58 :return:
59 """
60 # Read stream data
61 self.subtitle_args['streams_to_map'] = []
62 self.subtitle_args['streams_to_encode'] = []
63 subtitle_tracks_count = 0
64 for stream in self.file_probe['streams']:
65 # If this is a subtitle stream, then process the args
66 if stream['codec_type'] == 'subtitle':
67 # Remove subtitles means add no args
68 if self.remove_subtitle_streams:
69 continue
70  
71 # Add stream
72 # Check container for support of current stream (If copy is possible)
73 # TODO: Add support for user selection of subtitle format
74 supported_subtitles = self.container.supported_subtitles()
75 # TODO: Select best/or configured subtitle codec, then fetch that codec class.
76 # Use the subtitle class rather than this array
77 if stream['codec_name'] in supported_subtitles:
78 # If dest container supports the current subtitle codec, just copy it
79 self.subtitle_args['streams_to_encode'] = self.subtitle_args['streams_to_encode'] + [
80 "-c:s:{}".format(subtitle_tracks_count), "copy"
81 ]
82 subtitle_tracks_count += 1
83 else:
84 # The dest container does not support the current subtitle stream.
85 # Transcode the stream to a format that the destination container does support
86 # TODO: Check if it can be re-encoded. It is not possible to switch between image and text format
87 # If dest container supports the current subtitle codec, just copy it
88 # unsupported subtitles will need to be removed, otherwise ffmpeg will not convert
89 unsupported_subtitles = self.container.unsupported_subtitles()
90 if stream['codec_name'] in unsupported_subtitles:
91 continue
92 else:
93 self.subtitle_args['streams_to_encode'] = self.subtitle_args['streams_to_encode'] + [
94 "-c:s:{}".format(subtitle_tracks_count), "{}".format(supported_subtitles[0])
95 ]
96 subtitle_tracks_count += 1
97  
98 # Map this stream if it was marked above as compatible with the destination
99 self.subtitle_args['streams_to_map'] = self.subtitle_args['streams_to_map'] + [
100 "-map", "0:{}".format(stream['index'])
101 ]
102  
103 return self.subtitle_args
104  
105 def remove_subtitles(self):
106 """
107 Remove the subtitles stream from result file
108  
109 :return:
110 """
111 self.remove_subtitle_streams = True