Skip to content

Commit

Permalink
Add possibility for extra ffmpeg parameters in config, fix for undefi…
Browse files Browse the repository at this point in the history
…ned stream languages
  • Loading branch information
Cuchulain committed Oct 12, 2023
1 parent 4bd2430 commit 470f023
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 18 deletions.
10 changes: 9 additions & 1 deletion config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
'parameter': '-b:v 3400k', # or '-q:v 50' for example
}
},
'extra': '',
},
'files': {
'output': {
Expand All @@ -53,4 +54,11 @@ def get_values():
toml.dump(DEFAULT_VALUES, open(full_path, 'w'))
print("The configuration file was not found, a new one was created here: {}".format(full_path))

return toml.load(full_path)
parameters = toml.load(full_path)

merged_parameters = parameters | DEFAULT_VALUES

if parameters != merged_parameters:
toml.dump(merged_parameters, open(full_path, 'w'))

return merged_parameters
51 changes: 34 additions & 17 deletions recode_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def get_ffmpeg_parameters(file_path, params):
subtitles = {}
forced_subtitles = {}
default_audio = None
default_subtitles = None
video = None
index = 0

Expand All @@ -42,16 +43,20 @@ def get_ffmpeg_parameters(file_path, params):
recode_parameters.append("-map 0:{}".format(index))

if stream['codec_type'] == 'audio':
audios[stream['tags']['language']] = index
audios[get_stream_language(stream)] = index
if default_audio is None:
default_audio = index
if stream['disposition']['default']:
default_audio = index

if stream['codec_type'] == 'subtitle':
subtitles[stream['tags']['language']] = index
subtitles[get_stream_language(stream)] = index
if stream['disposition']['forced']:
forced_subtitles[stream['tags']['language']] = index
forced_subtitles[get_stream_language(stream)] = index
if default_subtitles is None:
default_subtitles = index
if stream['disposition']['default']:
default_subtitles = index

index += 1

Expand Down Expand Up @@ -86,34 +91,38 @@ def get_ffmpeg_parameters(file_path, params):
recode_parameters.append("-filter:v scale={}:{}".format(new_width, new_height))

audio_index = default_audio
audio_lang = metadata['streams'][default_audio]['tags']['language']
subtitle_index = None
audio_lang = get_stream_language(metadata['streams'][default_audio])
subtitles_index = default_subtitles
subtitles_lang = get_stream_language(metadata['streams'][default_subtitles])
audio_title = get_language_title(
audio_lang,
metadata['streams'][default_audio]
)
subtitle_title = None
subtitles_title = get_language_title(
subtitles_lang,
metadata['streams'][default_subtitles]
)

for lang in list(params['preferred_languages']['audio']):
if lang in audios:
audio_index = audios[lang]
audio_lang = lang
if audio_lang in forced_subtitles:
subtitle_index = forced_subtitles[lang]
subtitle_title = get_language_title(
subtitles_index = forced_subtitles[lang]
subtitles_title = get_language_title(
lang,
metadata['streams'][subtitle_index]
metadata['streams'][subtitles_index]
) + ' (forced)'
break

if subtitle_index is None:
if subtitles_index is None:
for lang in list(params['preferred_languages']['subtitles']):
if lang in subtitles and lang not in forced_subtitles:
if lang not in audios:
subtitle_index = subtitles[lang]
subtitle_title = get_language_title(
subtitles_index = subtitles[lang]
subtitles_title = get_language_title(
lang,
metadata['streams'][subtitle_index]
metadata['streams'][subtitles_index]
)
break

Expand All @@ -131,18 +140,25 @@ def get_ffmpeg_parameters(file_path, params):
else:
recode_parameters.append("-c:a copy")

if subtitle_index is not None:
if subtitles_index is not None:
recode_parameters.append('-map 0:{} -disposition:s:0 default -metadata:s:s:0 title="{}"'
.format(subtitle_index, subtitle_title))
.format(subtitles_index, subtitles_title))

if metadata['streams'][subtitle_index]['codec_name'] not in params['codecs']['subtitle']['allowed']:
if metadata['streams'][subtitles_index]['codec_name'] not in params['codecs']['subtitle']['allowed']:
recode_parameters.append("-c:s {}".format(params['codecs']['subtitle']['fallback']))
else:
recode_parameters.append("-c:s copy")

return " ".join(recode_parameters)


def get_stream_language(stream):
try:
return stream['tags']['language']
except KeyError:
return 'und'


def get_language_title(language, stream_info=None):
default = language

Expand Down Expand Up @@ -200,7 +216,8 @@ def get_command(command, file_path):
)

ffmpeg_parameters = get_ffmpeg_parameters(input_file, parameters['recoding'])
ffmpeg_command = 'ffmpeg -i "{}" {} "{}"'.format(input_file, ffmpeg_parameters, output_file)
ffmpeg_command = ('ffmpeg -i "{}" {} {} "{}"'
.format(input_file, ffmpeg_parameters, parameters['recoding']['extra'], output_file))

print("\nCall this command:\n{}\n".format(ffmpeg_command))

Expand Down

0 comments on commit 470f023

Please sign in to comment.