2022-08-28 17:06:47 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2022-12-16 14:51:10 +01:00
|
|
|
import subprocess
|
2022-12-17 16:26:51 +01:00
|
|
|
|
|
|
|
import json
|
|
|
|
|
2022-12-17 16:32:13 +01:00
|
|
|
import os
|
|
|
|
|
2022-12-17 16:26:51 +01:00
|
|
|
import csv
|
|
|
|
|
|
|
|
# ffmpeg wrapper
|
2022-08-28 17:06:47 +02:00
|
|
|
import ffmpy
|
2022-11-15 17:24:09 +01:00
|
|
|
|
2022-08-28 17:06:47 +02:00
|
|
|
# argument parsing
|
|
|
|
import argparse
|
|
|
|
|
2022-12-17 16:26:51 +01:00
|
|
|
|
|
|
|
def valid_duration(filename: str, filetype: str):
|
|
|
|
"""
|
|
|
|
Check given file for presence of duration metadata
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
filename (str): Path to file
|
|
|
|
filetype (str): Should be INPUT or OUTPUT, to define if the issue appeared after encoding or before
|
|
|
|
"""
|
|
|
|
# NOTE check all files for an intact/valid duration
|
|
|
|
# Valid file example output:
|
|
|
|
# {
|
|
|
|
# "format": {
|
|
|
|
# "duration": "1425.058000"
|
|
|
|
# }
|
|
|
|
# }
|
|
|
|
# Invalid file:
|
|
|
|
# {
|
|
|
|
# "format": {
|
|
|
|
#
|
|
|
|
# }
|
|
|
|
# }
|
|
|
|
ff = ffmpy.FFprobe(
|
|
|
|
inputs={filename: None},
|
|
|
|
global_options=("-show_entries format=duration -v quiet -print_format json"),
|
|
|
|
)
|
|
|
|
|
|
|
|
proc = subprocess.Popen(
|
|
|
|
ff.cmd, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, shell=True
|
|
|
|
)
|
|
|
|
|
|
|
|
info = json.loads(proc.stdout.read().rstrip().decode("utf8"))
|
|
|
|
|
|
|
|
# write broken files to error.csv files in current directory
|
|
|
|
if "duration" not in info["format"]:
|
|
|
|
with open("error.csv", "a", newline="") as file:
|
|
|
|
write = csv.writer(file)
|
|
|
|
write.writerow((filetype, filename))
|
|
|
|
|
|
|
|
|
2022-11-15 17:24:09 +01:00
|
|
|
parser = argparse.ArgumentParser(description="")
|
2022-08-28 17:06:47 +02:00
|
|
|
|
|
|
|
# Input file
|
2022-11-15 17:24:09 +01:00
|
|
|
parser.add_argument("-i", "--input-file", required=True, type=str, help="Input file")
|
2022-08-28 17:06:47 +02:00
|
|
|
|
|
|
|
# Media title
|
2022-11-15 17:24:09 +01:00
|
|
|
parser.add_argument("-t", "--title", required=True, type=str, help="Media title")
|
2022-08-28 17:06:47 +02:00
|
|
|
|
|
|
|
# Video stuff
|
2022-11-15 17:24:09 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"-vc",
|
|
|
|
"--video-codec",
|
|
|
|
required=False,
|
|
|
|
type=str,
|
|
|
|
help="Output video codec. Defaults to 'copy'",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-crf",
|
|
|
|
"--crf",
|
|
|
|
required=False,
|
|
|
|
type=int,
|
|
|
|
help="Codec crf. No effect if video codec is 'copy'. Defaults to 20",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-vt", "--video-tune", required=False, type=str, help="Video codec tune"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-vp",
|
|
|
|
"--video-preset",
|
|
|
|
required=False,
|
|
|
|
type=str,
|
|
|
|
help="Video compression preset. Defaults to 'medium'",
|
|
|
|
)
|
2022-08-28 17:06:47 +02:00
|
|
|
|
|
|
|
# Audio stuff
|
2022-11-15 17:24:09 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"-ac",
|
|
|
|
"--audio-codec",
|
|
|
|
required=False,
|
|
|
|
type=str,
|
|
|
|
help="Output audio codec. Defaults to 'copy'",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-ab",
|
|
|
|
"--audio-bitrate",
|
|
|
|
required=False,
|
|
|
|
type=str,
|
|
|
|
help="Output audio bitrate. No effect if audio codec is 'copy'. Defaults to '192k'",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-aj",
|
|
|
|
"--audio-japanese",
|
|
|
|
required=True,
|
|
|
|
type=str,
|
|
|
|
help="Stream identifier for japanese audio",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-ae",
|
|
|
|
"--audio-english",
|
|
|
|
required=False,
|
|
|
|
type=str,
|
|
|
|
help="Stream identifier for english audio",
|
|
|
|
)
|
2022-08-28 17:06:47 +02:00
|
|
|
|
|
|
|
# Subtitle stuff
|
2022-11-15 17:24:09 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"-sn", "--subtitle-name", required=True, type=str, help="Name for subtitles"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"-si",
|
|
|
|
"--subtitle-stream",
|
|
|
|
required=True,
|
|
|
|
type=str,
|
|
|
|
help="Stream identifier for subtitles",
|
|
|
|
)
|
2022-08-28 17:06:47 +02:00
|
|
|
|
|
|
|
# Output file
|
2022-11-15 17:24:09 +01:00
|
|
|
parser.add_argument("-o", "--output-file", required=True, type=str, help="Output file")
|
2022-08-28 17:06:47 +02:00
|
|
|
|
|
|
|
# Execute or print commands
|
2022-11-15 17:24:09 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"-e",
|
|
|
|
"--execute",
|
|
|
|
action="store_true",
|
|
|
|
help="Execute script. If not set, shows the commands that would be run.",
|
|
|
|
)
|
2022-08-28 17:06:47 +02:00
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
title = args.title
|
|
|
|
|
|
|
|
inputfile = args.input_file
|
|
|
|
|
|
|
|
# Default video codec is copy
|
|
|
|
if args.video_codec is None:
|
|
|
|
videocodec = "copy"
|
|
|
|
else:
|
|
|
|
videocodec = args.video_codec
|
|
|
|
|
|
|
|
# Default crf of 20
|
|
|
|
if args.crf is None:
|
|
|
|
crf = 20
|
|
|
|
else:
|
|
|
|
crf = args.crf
|
|
|
|
|
|
|
|
if args.video_tune is None:
|
|
|
|
tune = ""
|
|
|
|
else:
|
|
|
|
tune = "-tune " + args.video_tune
|
|
|
|
|
|
|
|
if args.video_preset is None:
|
|
|
|
preset = "medium"
|
|
|
|
else:
|
|
|
|
preset = args.video_preset
|
|
|
|
|
|
|
|
# Default audio codec is copy
|
|
|
|
if args.audio_codec is None:
|
|
|
|
audiocodec = "copy"
|
|
|
|
else:
|
|
|
|
audiocodec = args.audio_codec
|
|
|
|
|
|
|
|
# Default audio codec is copy
|
|
|
|
if args.audio_bitrate is None:
|
|
|
|
audiobitrate = "192k"
|
|
|
|
else:
|
|
|
|
audiobitrate = args.audio_bitrate
|
|
|
|
|
|
|
|
outputfile = args.output_file
|
|
|
|
|
|
|
|
japaneseaudio = args.audio_japanese
|
|
|
|
|
2022-08-28 21:19:05 +02:00
|
|
|
# Map english audio, if set
|
2022-08-28 17:06:47 +02:00
|
|
|
if args.audio_english is None:
|
|
|
|
englishaudio = ""
|
|
|
|
else:
|
|
|
|
englishaudio = "-map " + args.audio_english
|
|
|
|
|
|
|
|
subtitle = args.subtitle_name
|
|
|
|
subtitlestream = args.subtitle_stream
|
|
|
|
|
2022-08-28 21:19:05 +02:00
|
|
|
# Flag to actually execute command
|
2022-08-28 17:06:47 +02:00
|
|
|
execute = args.execute
|
|
|
|
|
2022-12-17 16:26:51 +01:00
|
|
|
# check input file for valid duration
|
|
|
|
valid_duration(inputfile, "INPUT")
|
|
|
|
|
2022-11-15 17:23:55 +01:00
|
|
|
# NOTE Breaks if filename contains quotes: '
|
2022-11-15 17:24:09 +01:00
|
|
|
ff = ffmpy.FFmpeg(
|
|
|
|
inputs={inputfile: None},
|
|
|
|
outputs={
|
|
|
|
outputfile: "-metadata title='{title}' -disposition 0"
|
|
|
|
" "
|
|
|
|
"-c:v {videocodec} -crf {crf} {tune} -preset {preset} -map 0:v:0 -metadata:s:v:0 title='Video' -disposition:v:0 default"
|
|
|
|
" "
|
|
|
|
"-c:a {audiocodec} -b:a {audiobitrate} -map {jpnaudiostream}? {engaudiomap}"
|
|
|
|
" "
|
|
|
|
"-metadata:s:a:0 title='Japanese' -metadata:s:a:0 language=jpn -metadata:s:a:1 title='English' -metadata:s:a:1 language=eng -disposition:a:0 default"
|
|
|
|
" "
|
|
|
|
"-c:s copy -map {substream} -metadata:s:s:0 title='{subtitle}' -metadata:s:s:0 language=eng -disposition:s:0 default"
|
|
|
|
" ".format(
|
|
|
|
title=title,
|
|
|
|
videocodec=videocodec,
|
|
|
|
crf=crf,
|
|
|
|
tune=tune,
|
|
|
|
preset=preset,
|
|
|
|
audiocodec=audiocodec,
|
|
|
|
audiobitrate=audiobitrate,
|
|
|
|
jpnaudiostream=japaneseaudio,
|
|
|
|
engaudiomap=englishaudio,
|
|
|
|
substream=subtitlestream,
|
|
|
|
subtitle=subtitle,
|
|
|
|
)
|
|
|
|
},
|
|
|
|
)
|
2022-08-28 17:06:47 +02:00
|
|
|
|
|
|
|
if execute:
|
|
|
|
ff.run()
|
|
|
|
else:
|
|
|
|
print(ff.cmd)
|
2022-12-16 14:51:10 +01:00
|
|
|
|
2022-12-17 16:26:51 +01:00
|
|
|
# check output file for valid duration
|
|
|
|
valid_duration(outputfile, "OUTPUT")
|
2022-12-17 16:32:13 +01:00
|
|
|
|
|
|
|
if os.path.isfile("error.csv"):
|
|
|
|
print(
|
|
|
|
"Some media might have errors. Please check the error.csv file in this directory"
|
|
|
|
)
|