Skip to content

Commit

Permalink
Add basic subtitle conversion/removal
Browse files Browse the repository at this point in the history
  • Loading branch information
raydouglass committed Aug 24, 2024
1 parent 8714fec commit 31843b4
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
9 changes: 9 additions & 0 deletions media_management_scripts/commands/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
DEFAULT_CRF,
DEFAULT_PRESET,
Resolution,
SubtitleCodec,
VideoCodec,
AudioCodec,
)
Expand Down Expand Up @@ -155,6 +156,14 @@ def __call__(self, parser, namespace, values, option_string=None):
choices=[ac.ffmpeg_codec_name for ac in AudioCodec],
)

convert_parent_parser.add_argument(
"--subtitles",
"--subs",
dest="subtitle_codec",
default=SubtitleCodec.COPY.ffmpeg_codec_name,
choices=[sc.ffmpeg_codec_name for sc in SubtitleCodec],
)

convert_parent_parser.add_argument(
"--hardware-nvidia",
"--hw-nv",
Expand Down
12 changes: 9 additions & 3 deletions media_management_scripts/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
DEFAULT_PRESET,
DEFAULT_CRF,
Resolution,
SubtitleCodec,
resolution_name,
VideoCodec,
AudioCodec,
Expand Down Expand Up @@ -204,15 +205,20 @@ def convert_with_config(
args.extend(["-ac:a:{}".format(index), "8"])
index += 1

if config.include_subtitles:
args.extend(["-c:s", "copy"])
include_subtitles = (
config.include_subtitles
and config.subtitle_codec != SubtitleCodec.NONE.ffmpeg_codec_name
)

if include_subtitles:
args.extend(["-c:s", config.subtitle_codec])

if not mappings:
if metadata.video_streams:
args.extend(["-map", "0:v"])
if metadata.audio_streams:
args.extend(["-map", "0:a"])
if metadata.subtitle_streams:
if metadata.subtitle_streams and include_subtitles:
args.extend(["-map", "0:s"])
else:
for m in mappings:
Expand Down
13 changes: 13 additions & 0 deletions media_management_scripts/support/encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class AudioCodec(Enum):
AAC = "aac"
AC3 = "ac3"
DTS = "dts"
FLAC = "flac"
COPY = "copy"

@property
Expand Down Expand Up @@ -203,3 +204,15 @@ def from_value(value):
if preset.value == value:
return preset
raise ValueError(f"Invalid preset: {value}")


class SubtitleCodec(Enum):
COPY = "copy"
SRT = "srt"
ASS = "ass"
WEBVTT = "webvtt"
NONE = "none"

@property
def ffmpeg_codec_name(self):
return self.value
2 changes: 2 additions & 0 deletions media_management_scripts/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
DEFAULT_CRF,
DEFAULT_PRESET,
Resolution,
SubtitleCodec,
VideoCodec,
AudioCodec,
)
Expand Down Expand Up @@ -106,6 +107,7 @@ class ConvertConfig(NamedTuple):
scale: Optional[int] = None
video_codec: str = VideoCodec.H264.ffmpeg_encoder_name
audio_codec: str = AudioCodec.AAC.ffmpeg_codec_name
subtitle_codec: str = SubtitleCodec.COPY.ffmpeg_codec_name
hardware_nvidia: bool = False
hardware_apple: bool = False

Expand Down

0 comments on commit 31843b4

Please sign in to comment.