Passed the code in formatter
This commit is contained in:
parent
87847859c5
commit
c31007498e
@ -41,15 +41,13 @@ def get_ffmpeg_codecs(codec_type="S"):
|
|||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
# Run ffmpeg -codecs
|
# Run ffmpeg -codecs
|
||||||
result = subprocess.run(["ffmpeg", "-codecs"],
|
result = subprocess.run(
|
||||||
capture_output=True,
|
["ffmpeg", "-codecs"], capture_output=True, text=True, check=True
|
||||||
text=True,
|
)
|
||||||
check=True)
|
|
||||||
output = result.stdout
|
output = result.stdout
|
||||||
|
|
||||||
# Extract codecs using regex
|
# Extract codecs using regex
|
||||||
codec_pattern = re.compile(rf"^[ D.E]+{codec_type}[ A-Z.]+ (\S+)",
|
codec_pattern = re.compile(rf"^[ D.E]+{codec_type}[ A-Z.]+ (\S+)", re.MULTILINE)
|
||||||
re.MULTILINE)
|
|
||||||
codecs = codec_pattern.findall(output)
|
codecs = codec_pattern.findall(output)
|
||||||
|
|
||||||
return set(codecs) # Return as a set for easy lookup
|
return set(codecs) # Return as a set for easy lookup
|
||||||
@ -60,7 +58,7 @@ def get_ffmpeg_codecs(codec_type="S"):
|
|||||||
|
|
||||||
|
|
||||||
def findfreename(filepath, attempt=0):
|
def findfreename(filepath, attempt=0):
|
||||||
'''
|
"""
|
||||||
Given a filepath, it will try to find a free filename by appending a number to the name.
|
Given a filepath, it will try to find a free filename by appending a number to the name.
|
||||||
First, it tries using the filepath passed in the argument, then adds a number to the end. If all fail, it appends (#).
|
First, it tries using the filepath passed in the argument, then adds a number to the end. If all fail, it appends (#).
|
||||||
|
|
||||||
@ -70,10 +68,10 @@ def findfreename(filepath, attempt=0):
|
|||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
str: The first free filepath found.
|
str: The first free filepath found.
|
||||||
'''
|
"""
|
||||||
attempt += 1
|
attempt += 1
|
||||||
filename = str(filepath)[:str(filepath).rindex(".")]
|
filename = str(filepath)[: str(filepath).rindex(".")]
|
||||||
extension = str(filepath)[str(filepath).rindex("."):]
|
extension = str(filepath)[str(filepath).rindex(".") :]
|
||||||
copynumpath = filename + f"({attempt})" + extension
|
copynumpath = filename + f"({attempt})" + extension
|
||||||
|
|
||||||
if not os.path.exists(filepath) and attempt <= 2:
|
if not os.path.exists(filepath) and attempt <= 2:
|
||||||
@ -84,13 +82,13 @@ def findfreename(filepath, attempt=0):
|
|||||||
|
|
||||||
|
|
||||||
def deletefile(filepath):
|
def deletefile(filepath):
|
||||||
'''Delete a file, Returns a boolean
|
"""Delete a file, Returns a boolean
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
filepath : A string containing the full filepath
|
filepath : A string containing the full filepath
|
||||||
Returns:
|
Returns:
|
||||||
Bool : The success of the operation
|
Bool : The success of the operation
|
||||||
'''
|
"""
|
||||||
try:
|
try:
|
||||||
os.remove(filepath)
|
os.remove(filepath)
|
||||||
except OSError:
|
except OSError:
|
||||||
@ -102,7 +100,7 @@ def deletefile(filepath):
|
|||||||
|
|
||||||
|
|
||||||
def ensure_output_path(output_path):
|
def ensure_output_path(output_path):
|
||||||
'''
|
"""
|
||||||
Ensure that the output directory exists. If it doesn't, attempt to create it.
|
Ensure that the output directory exists. If it doesn't, attempt to create it.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@ -110,15 +108,14 @@ def ensure_output_path(output_path):
|
|||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
None
|
None
|
||||||
'''
|
"""
|
||||||
try:
|
try:
|
||||||
os.makedirs(output_path, exist_ok=True)
|
os.makedirs(output_path, exist_ok=True)
|
||||||
# Attempts to create the output directory if it doesn't exist.
|
# Attempts to create the output directory if it doesn't exist.
|
||||||
# exist_ok=True ensures that an OSError is not raised if the directory already exists.
|
# exist_ok=True ensures that an OSError is not raised if the directory already exists.
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
# If an OSError occurs during directory creation, print an error message and exit the program.
|
# If an OSError occurs during directory creation, print an error message and exit the program.
|
||||||
print(f"{cred}Failed to create output directory:{creset} {e}",
|
print(f"{cred}Failed to create output directory:{creset} {e}", file=sys.stderr)
|
||||||
file=sys.stderr)
|
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
|
||||||
@ -140,13 +137,23 @@ def has_supported_subs(video_file, supported_subtitle_codecs, debug=False):
|
|||||||
False otherwise or in case of an error.
|
False otherwise or in case of an error.
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
result = subprocess.run([
|
result = subprocess.run(
|
||||||
'ffprobe', '-v', 'error', '-select_streams', 's', '-show_entries',
|
[
|
||||||
'stream=index,codec_name', '-of', 'json', video_file
|
"ffprobe",
|
||||||
|
"-v",
|
||||||
|
"error",
|
||||||
|
"-select_streams",
|
||||||
|
"s",
|
||||||
|
"-show_entries",
|
||||||
|
"stream=index,codec_name",
|
||||||
|
"-of",
|
||||||
|
"json",
|
||||||
|
video_file,
|
||||||
],
|
],
|
||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
stderr=subprocess.PIPE,
|
stderr=subprocess.PIPE,
|
||||||
text=True)
|
text=True,
|
||||||
|
)
|
||||||
|
|
||||||
if result.returncode != 0:
|
if result.returncode != 0:
|
||||||
if debug:
|
if debug:
|
||||||
@ -161,8 +168,8 @@ def has_supported_subs(video_file, supported_subtitle_codecs, debug=False):
|
|||||||
return False # Assume no supported subtitles if JSON is malformed
|
return False # Assume no supported subtitles if JSON is malformed
|
||||||
|
|
||||||
return any(
|
return any(
|
||||||
stream.get("codec_name") in supported_subtitle_codecs
|
stream.get("codec_name") in supported_subtitle_codecs for stream in streams
|
||||||
for stream in streams)
|
)
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
if debug:
|
if debug:
|
||||||
@ -171,7 +178,7 @@ def has_supported_subs(video_file, supported_subtitle_codecs, debug=False):
|
|||||||
|
|
||||||
|
|
||||||
def find_videos_to_convert(input_path, max_height=720, debug=False):
|
def find_videos_to_convert(input_path, max_height=720, debug=False):
|
||||||
'''
|
"""
|
||||||
Find video files in the specified directory tree that meet conversion criteria.
|
Find video files in the specified directory tree that meet conversion criteria.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@ -181,10 +188,9 @@ def find_videos_to_convert(input_path, max_height=720, debug=False):
|
|||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
list: A list of paths to video files that meet the conversion criteria.
|
list: A list of paths to video files that meet the conversion criteria.
|
||||||
'''
|
"""
|
||||||
# Print a message indicating the start of the scanning process
|
# Print a message indicating the start of the scanning process
|
||||||
print(
|
print(f"{cgreen}Scanning {input_path} for video files to convert.{creset}\n")
|
||||||
f"{cgreen}Scanning {input_path} for video files to convert.{creset}\n")
|
|
||||||
|
|
||||||
# Initialize lists to store all video files and valid video files
|
# Initialize lists to store all video files and valid video files
|
||||||
video_files = []
|
video_files = []
|
||||||
@ -195,13 +201,13 @@ def find_videos_to_convert(input_path, max_height=720, debug=False):
|
|||||||
for file in files:
|
for file in files:
|
||||||
fullpath = os.path.join(root, file)
|
fullpath = os.path.join(root, file)
|
||||||
# Check if the file is a video file with one of the specified extensions
|
# Check if the file is a video file with one of the specified extensions
|
||||||
if (file.lower().endswith(
|
if file.lower().endswith(
|
||||||
('.mp4', '.avi', '.mkv', '.mov', '.wmv', '.flv', '.divx'))
|
(".mp4", ".avi", ".mkv", ".mov", ".wmv", ".flv", ".divx")
|
||||||
and os.path.isfile(fullpath)):
|
) and os.path.isfile(fullpath):
|
||||||
video_files.append(fullpath)
|
video_files.append(fullpath)
|
||||||
|
|
||||||
# List of encodings to try for decoding output from ffprobe
|
# List of encodings to try for decoding output from ffprobe
|
||||||
ENCODINGS_TO_TRY = ['utf-8', 'latin-1', 'iso-8859-1']
|
ENCODINGS_TO_TRY = ["utf-8", "latin-1", "iso-8859-1"]
|
||||||
|
|
||||||
# Iterate through each video file
|
# Iterate through each video file
|
||||||
for video_file in video_files:
|
for video_file in video_files:
|
||||||
@ -209,28 +215,37 @@ def find_videos_to_convert(input_path, max_height=720, debug=False):
|
|||||||
# Run ffprobe command to get video resolution
|
# Run ffprobe command to get video resolution
|
||||||
resolution_output = subprocess.check_output(
|
resolution_output = subprocess.check_output(
|
||||||
[
|
[
|
||||||
'ffprobe', '-v', 'error', '-select_streams', 'v:0',
|
"ffprobe",
|
||||||
'-show_entries', 'stream=width,height', '-of', 'csv=p=0',
|
"-v",
|
||||||
video_file
|
"error",
|
||||||
|
"-select_streams",
|
||||||
|
"v:0",
|
||||||
|
"-show_entries",
|
||||||
|
"stream=width,height",
|
||||||
|
"-of",
|
||||||
|
"csv=p=0",
|
||||||
|
video_file,
|
||||||
],
|
],
|
||||||
stderr=subprocess.STDOUT)
|
stderr=subprocess.STDOUT,
|
||||||
|
)
|
||||||
|
|
||||||
# Attempt to decode the output using different encodings
|
# Attempt to decode the output using different encodings
|
||||||
dimensions_str = None
|
dimensions_str = None
|
||||||
for encoding in ENCODINGS_TO_TRY:
|
for encoding in ENCODINGS_TO_TRY:
|
||||||
try:
|
try:
|
||||||
dimensions_str = resolution_output.decode(
|
dimensions_str = (
|
||||||
encoding).strip().rstrip(',')
|
resolution_output.decode(encoding).strip().rstrip(",")
|
||||||
|
)
|
||||||
|
|
||||||
dimensions = [
|
dimensions = [
|
||||||
int(d) for d in dimensions_str.split(',')
|
int(d) for d in dimensions_str.split(",") if d.strip().isdigit()
|
||||||
if d.strip().isdigit()
|
|
||||||
]
|
]
|
||||||
if len(dimensions) >= 2:
|
if len(dimensions) >= 2:
|
||||||
width, height = dimensions[:2]
|
width, height = dimensions[:2]
|
||||||
else:
|
else:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
f"Unexpected dimensions format: {dimensions_str}")
|
f"Unexpected dimensions format: {dimensions_str}"
|
||||||
|
)
|
||||||
break # Stop trying encodings once successful decoding and conversion
|
break # Stop trying encodings once successful decoding and conversion
|
||||||
except (UnicodeDecodeError, ValueError):
|
except (UnicodeDecodeError, ValueError):
|
||||||
continue # Try the next encoding
|
continue # Try the next encoding
|
||||||
@ -240,7 +255,8 @@ def find_videos_to_convert(input_path, max_height=720, debug=False):
|
|||||||
if debug:
|
if debug:
|
||||||
print(
|
print(
|
||||||
f"{cred}Error processing {video_file}: Unable to decode output from ffprobe using any of the specified encodings.{creset}",
|
f"{cred}Error processing {video_file}: Unable to decode output from ffprobe using any of the specified encodings.{creset}",
|
||||||
file=sys.stderr)
|
file=sys.stderr,
|
||||||
|
)
|
||||||
continue # Skip this video and continue with the next one
|
continue # Skip this video and continue with the next one
|
||||||
|
|
||||||
# Ignore vertical videos
|
# Ignore vertical videos
|
||||||
@ -258,17 +274,20 @@ def find_videos_to_convert(input_path, max_height=720, debug=False):
|
|||||||
decoded_error = e.output.decode("utf-8")
|
decoded_error = e.output.decode("utf-8")
|
||||||
print(
|
print(
|
||||||
f"{cred}Error processing {video_file}: {decoded_error}{creset}\n",
|
f"{cred}Error processing {video_file}: {decoded_error}{creset}\n",
|
||||||
file=sys.stderr)
|
file=sys.stderr,
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
print(
|
print(
|
||||||
f"{cred}Error processing {video_file}: No output from subprocess{creset}\n",
|
f"{cred}Error processing {video_file}: No output from subprocess{creset}\n",
|
||||||
file=sys.stderr)
|
file=sys.stderr,
|
||||||
|
)
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
if debug:
|
if debug:
|
||||||
# Print error message if value error occurs during decoding
|
# Print error message if value error occurs during decoding
|
||||||
print(
|
print(
|
||||||
f"{cred}Error processing {video_file}: ValueError decoding the file{creset}\n",
|
f"{cred}Error processing {video_file}: ValueError decoding the file{creset}\n",
|
||||||
file=sys.stderr)
|
file=sys.stderr,
|
||||||
|
)
|
||||||
|
|
||||||
valid_videos_files.sort()
|
valid_videos_files.sort()
|
||||||
|
|
||||||
@ -276,7 +295,7 @@ def find_videos_to_convert(input_path, max_height=720, debug=False):
|
|||||||
|
|
||||||
|
|
||||||
def convert_videos(input_path, output_path, max_height=720, debug=False):
|
def convert_videos(input_path, output_path, max_height=720, debug=False):
|
||||||
'''
|
"""
|
||||||
Convert videos taller than a specified height to 720p resolution with x265 encoding and MKV container.
|
Convert videos taller than a specified height to 720p resolution with x265 encoding and MKV container.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@ -287,7 +306,7 @@ def convert_videos(input_path, output_path, max_height=720, debug=False):
|
|||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
bool: True if conversion succeeds, False otherwise.
|
bool: True if conversion succeeds, False otherwise.
|
||||||
'''
|
"""
|
||||||
|
|
||||||
# Get supported subtitle codecs
|
# Get supported subtitle codecs
|
||||||
supported_subtitle_codecs = get_ffmpeg_codecs("S")
|
supported_subtitle_codecs = get_ffmpeg_codecs("S")
|
||||||
@ -326,22 +345,30 @@ def convert_videos(input_path, output_path, max_height=720, debug=False):
|
|||||||
# Generate output file path and name
|
# Generate output file path and name
|
||||||
output_file = findfreename(
|
output_file = findfreename(
|
||||||
os.path.join(
|
os.path.join(
|
||||||
output_path,
|
output_path, os.path.splitext(os.path.basename(video_file))[0] + ".mkv"
|
||||||
os.path.splitext(os.path.basename(video_file))[0] + '.mkv'))
|
)
|
||||||
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Get original video width and height
|
# Get original video width and height
|
||||||
video_info = subprocess.run([
|
video_info = subprocess.run(
|
||||||
'ffprobe', '-v', 'error', '-select_streams', 'v:0',
|
[
|
||||||
'-show_entries', 'stream=width,height', '-of', 'csv=p=0',
|
"ffprobe",
|
||||||
video_file
|
"-v",
|
||||||
|
"error",
|
||||||
|
"-select_streams",
|
||||||
|
"v:0",
|
||||||
|
"-show_entries",
|
||||||
|
"stream=width,height",
|
||||||
|
"-of",
|
||||||
|
"csv=p=0",
|
||||||
|
video_file,
|
||||||
],
|
],
|
||||||
stdout=subprocess.PIPE,
|
stdout=subprocess.PIPE,
|
||||||
stderr=subprocess.PIPE,
|
stderr=subprocess.PIPE,
|
||||||
text=True)
|
text=True,
|
||||||
width, height = map(
|
)
|
||||||
int,
|
width, height = map(int, video_info.stdout.strip().rstrip(",").split(","))
|
||||||
video_info.stdout.strip().rstrip(',').split(','))
|
|
||||||
|
|
||||||
# Calculate the scaled width maintaining aspect ratio
|
# Calculate the scaled width maintaining aspect ratio
|
||||||
scaled_width = int(max_height * (width / height))
|
scaled_width = int(max_height * (width / height))
|
||||||
@ -353,38 +380,55 @@ def convert_videos(input_path, output_path, max_height=720, debug=False):
|
|||||||
current_file = output_file
|
current_file = output_file
|
||||||
|
|
||||||
# Check if the video has supported subtitles
|
# Check if the video has supported subtitles
|
||||||
include_subs = has_supported_subs(video_file,
|
include_subs = has_supported_subs(
|
||||||
supported_subtitle_codecs, debug)
|
video_file, supported_subtitle_codecs, debug
|
||||||
|
)
|
||||||
|
|
||||||
# Construct the FFmpeg command dynamically
|
# Construct the FFmpeg command dynamically
|
||||||
ffmpeg_command = [
|
ffmpeg_command = [
|
||||||
'ffmpeg', '-n', '-i', video_file, '-map', '0:v', '-c:v',
|
"ffmpeg",
|
||||||
'libx265', '-vf', f'scale={scaled_width}:{max_height}',
|
"-n",
|
||||||
'-preset', 'medium', '-crf', '28', '-map', '0:a?', '-c:a',
|
"-i",
|
||||||
'copy'
|
video_file,
|
||||||
|
"-map",
|
||||||
|
"0:v",
|
||||||
|
"-c:v",
|
||||||
|
"libx265",
|
||||||
|
"-vf",
|
||||||
|
f"scale={scaled_width}:{max_height}",
|
||||||
|
"-preset",
|
||||||
|
"medium",
|
||||||
|
"-crf",
|
||||||
|
"28",
|
||||||
|
"-map",
|
||||||
|
"0:a?",
|
||||||
|
"-c:a",
|
||||||
|
"copy",
|
||||||
]
|
]
|
||||||
|
|
||||||
# Only include subtitles if they are supported
|
# Only include subtitles if they are supported
|
||||||
if include_subs:
|
if include_subs:
|
||||||
ffmpeg_command.extend(['-map', '0:s?', '-c:s', 'copy'])
|
ffmpeg_command.extend(["-map", "0:s?", "-c:s", "copy"])
|
||||||
|
|
||||||
ffmpeg_command.append(output_file)
|
ffmpeg_command.append(output_file)
|
||||||
|
|
||||||
# Print the FFmpeg command as a single string
|
# Print the FFmpeg command as a single string
|
||||||
if (debug):
|
if debug:
|
||||||
print("FFmpeg command: " + ' '.join(ffmpeg_command))
|
print("FFmpeg command: " + " ".join(ffmpeg_command))
|
||||||
|
|
||||||
# Run the FFmpeg command
|
# Run the FFmpeg command
|
||||||
process = subprocess.run(ffmpeg_command,
|
process = subprocess.run(
|
||||||
|
ffmpeg_command,
|
||||||
check=True,
|
check=True,
|
||||||
stdout=subprocess.DEVNULL,
|
stdout=subprocess.DEVNULL,
|
||||||
stderr=subprocess.STDOUT)
|
stderr=subprocess.STDOUT,
|
||||||
|
)
|
||||||
|
|
||||||
# Check if ffmpeg process returned successfully
|
# Check if ffmpeg process returned successfully
|
||||||
if process.returncode != 0:
|
if process.returncode != 0:
|
||||||
raise subprocess.CalledProcessError(process.returncode,
|
raise subprocess.CalledProcessError(
|
||||||
process.args,
|
process.returncode, process.args, output=process.stderr
|
||||||
output=process.stderr)
|
)
|
||||||
|
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
# If we have a partial video converted, delete it due to the failure
|
# If we have a partial video converted, delete it due to the failure
|
||||||
@ -397,11 +441,13 @@ def convert_videos(input_path, output_path, max_height=720, debug=False):
|
|||||||
decoded_error = e.output.decode("utf-8")
|
decoded_error = e.output.decode("utf-8")
|
||||||
print(
|
print(
|
||||||
f"{cred}Error processing {video_file}: {decoded_error}{creset}\n",
|
f"{cred}Error processing {video_file}: {decoded_error}{creset}\n",
|
||||||
file=sys.stderr)
|
file=sys.stderr,
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
print(
|
print(
|
||||||
f"{cred}Error processing {video_file}: No output from subprocess{creset}\n",
|
f"{cred}Error processing {video_file}: No output from subprocess{creset}\n",
|
||||||
file=sys.stderr)
|
file=sys.stderr,
|
||||||
|
)
|
||||||
|
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
print(f"{cyellow}Conversions cancelled, cleaning up...{creset}")
|
print(f"{cyellow}Conversions cancelled, cleaning up...{creset}")
|
||||||
@ -422,13 +468,12 @@ def convert_videos(input_path, output_path, max_height=720, debug=False):
|
|||||||
if debug:
|
if debug:
|
||||||
print(
|
print(
|
||||||
f"{cred}Error processing {video_file}: {str(e)}{creset}\n",
|
f"{cred}Error processing {video_file}: {str(e)}{creset}\n",
|
||||||
file=sys.stderr)
|
file=sys.stderr,
|
||||||
|
)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
|
||||||
# Print success message if conversion is successful
|
# Print success message if conversion is successful
|
||||||
print(
|
print(f"{ccyan}Successfully converted video to output file:{creset}")
|
||||||
f"{ccyan}Successfully converted video to output file:{creset}")
|
|
||||||
print(f"{output_file}\n")
|
print(f"{output_file}\n")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -443,39 +488,40 @@ def convert_videos(input_path, output_path, max_height=720, debug=False):
|
|||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
'''
|
"""
|
||||||
Main function to parse command line arguments and initiate video conversion.
|
Main function to parse command line arguments and initiate video conversion.
|
||||||
'''
|
"""
|
||||||
|
|
||||||
# Create argument parser
|
# Create argument parser
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
description=
|
description="Convert videos taller than a certain height to 720p resolution with x265 encoding and MKV container."
|
||||||
'Convert videos taller than a certain height to 720p resolution with x265 encoding and MKV container.'
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Define command line arguments
|
# Define command line arguments
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'input_path',
|
"input_path",
|
||||||
nargs='?',
|
nargs="?",
|
||||||
default=os.getcwd(),
|
default=os.getcwd(),
|
||||||
help=
|
help="directory path to search for video files (default: current directory)",
|
||||||
'directory path to search for video files (default: current directory)'
|
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-o',
|
"-o",
|
||||||
'--output-path',
|
"--output-path",
|
||||||
default=os.path.join(os.getcwd(), 'converted'),
|
default=os.path.join(os.getcwd(), "converted"),
|
||||||
help='directory path to save converted videos (default: ./converted)')
|
help="directory path to save converted videos (default: ./converted)",
|
||||||
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-mh',
|
"-mh",
|
||||||
'--max-height',
|
"--max-height",
|
||||||
type=int,
|
type=int,
|
||||||
default=720,
|
default=720,
|
||||||
help='maximum height of videos to be converted (default: 720)')
|
help="maximum height of videos to be converted (default: 720)",
|
||||||
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--debug',
|
"--debug",
|
||||||
action='store_true',
|
action="store_true",
|
||||||
help='enable debug mode for printing additional error messages')
|
help="enable debug mode for printing additional error messages",
|
||||||
|
)
|
||||||
|
|
||||||
# Enable autocomplete for argparse
|
# Enable autocomplete for argparse
|
||||||
argcomplete.autocomplete(parser)
|
argcomplete.autocomplete(parser)
|
||||||
@ -484,8 +530,7 @@ def main():
|
|||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
# Convert videos
|
# Convert videos
|
||||||
convert_videos(args.input_path, args.output_path, args.max_height,
|
convert_videos(args.input_path, args.output_path, args.max_height, args.debug)
|
||||||
args.debug)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
@ -17,7 +17,7 @@ cred = colorama.Fore.RED
|
|||||||
|
|
||||||
|
|
||||||
def autorename(input_path, max_height=720, debug=False):
|
def autorename(input_path, max_height=720, debug=False):
|
||||||
'''
|
"""
|
||||||
Rename video files and folders in the specified directory tree that contain a resolution
|
Rename video files and folders in the specified directory tree that contain a resolution
|
||||||
in their name to the specified max_height resolution.
|
in their name to the specified max_height resolution.
|
||||||
|
|
||||||
@ -25,17 +25,17 @@ def autorename(input_path, max_height=720, debug=False):
|
|||||||
input_path (str): The path of the directory to search for video files and folders.
|
input_path (str): The path of the directory to search for video files and folders.
|
||||||
max_height (int, optional): The maximum height (in pixels) of the video to consider for conversion. Default is 720.
|
max_height (int, optional): The maximum height (in pixels) of the video to consider for conversion. Default is 720.
|
||||||
debug (bool, optional): If True, print debug messages. Default is False.
|
debug (bool, optional): If True, print debug messages. Default is False.
|
||||||
'''
|
"""
|
||||||
# Define patterns to search for various resolutions
|
# Define patterns to search for various resolutions
|
||||||
resolution_patterns = {
|
resolution_patterns = {
|
||||||
2160: r'(4096x2160|3840x2160|2880p|2160p|2160|1440p|4k)',
|
2160: r"(4096x2160|3840x2160|2880p|2160p|2160|1440p|4k)",
|
||||||
1080: r'(1920x1080|1080p|1080)',
|
1080: r"(1920x1080|1080p|1080)",
|
||||||
720: r'(1280x720|720p|720)',
|
720: r"(1280x720|720p|720)",
|
||||||
}
|
}
|
||||||
|
|
||||||
# Wrap each pattern to only match when isolated by non-alphanumeric chars or string boundaries
|
# Wrap each pattern to only match when isolated by non-alphanumeric chars or string boundaries
|
||||||
resolution_patterns = {
|
resolution_patterns = {
|
||||||
res: rf'(?<![a-zA-Z0-9]){pat}(?![a-zA-Z0-9])'
|
res: rf"(?<![a-zA-Z0-9]){pat}(?![a-zA-Z0-9])"
|
||||||
for res, pat in resolution_patterns.items()
|
for res, pat in resolution_patterns.items()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,12 +48,12 @@ def autorename(input_path, max_height=720, debug=False):
|
|||||||
for resolution, pattern in resolution_patterns.items():
|
for resolution, pattern in resolution_patterns.items():
|
||||||
# Only process resolutions greater than max_height
|
# Only process resolutions greater than max_height
|
||||||
if resolution > max_height and re.search(
|
if resolution > max_height and re.search(
|
||||||
pattern, filename, re.IGNORECASE):
|
pattern, filename, re.IGNORECASE
|
||||||
|
):
|
||||||
old_file = os.path.join(dirpath, filename)
|
old_file = os.path.join(dirpath, filename)
|
||||||
new_filename = re.sub(pattern,
|
new_filename = re.sub(
|
||||||
f'{max_height}p',
|
pattern, f"{max_height}p", filename, flags=re.IGNORECASE
|
||||||
filename,
|
)
|
||||||
flags=re.IGNORECASE)
|
|
||||||
new_file = os.path.join(dirpath, new_filename)
|
new_file = os.path.join(dirpath, new_filename)
|
||||||
if old_file != new_file:
|
if old_file != new_file:
|
||||||
files_to_rename.append((old_file, new_file))
|
files_to_rename.append((old_file, new_file))
|
||||||
@ -64,12 +64,12 @@ def autorename(input_path, max_height=720, debug=False):
|
|||||||
for resolution, pattern in resolution_patterns.items():
|
for resolution, pattern in resolution_patterns.items():
|
||||||
# Only process resolutions greater than max_height
|
# Only process resolutions greater than max_height
|
||||||
if resolution > max_height and re.search(
|
if resolution > max_height and re.search(
|
||||||
pattern, dirname, re.IGNORECASE):
|
pattern, dirname, re.IGNORECASE
|
||||||
|
):
|
||||||
old_dir = os.path.join(dirpath, dirname)
|
old_dir = os.path.join(dirpath, dirname)
|
||||||
new_dirname = re.sub(pattern,
|
new_dirname = re.sub(
|
||||||
f'{max_height}p',
|
pattern, f"{max_height}p", dirname, flags=re.IGNORECASE
|
||||||
dirname,
|
)
|
||||||
flags=re.IGNORECASE)
|
|
||||||
new_dir = os.path.join(dirpath, new_dirname)
|
new_dir = os.path.join(dirpath, new_dirname)
|
||||||
if old_dir != new_dir:
|
if old_dir != new_dir:
|
||||||
dirs_to_rename.append((old_dir, new_dir))
|
dirs_to_rename.append((old_dir, new_dir))
|
||||||
@ -82,7 +82,7 @@ def autorename(input_path, max_height=720, debug=False):
|
|||||||
continue
|
continue
|
||||||
os.rename(old_file, new_file)
|
os.rename(old_file, new_file)
|
||||||
if debug:
|
if debug:
|
||||||
print(f'Renamed file: {old_file} -> {new_file}')
|
print(f"Renamed file: {old_file} -> {new_file}")
|
||||||
|
|
||||||
# Rename directories after
|
# Rename directories after
|
||||||
for old_dir, new_dir in sorted(dirs_to_rename, key=lambda x: -len(x[0])):
|
for old_dir, new_dir in sorted(dirs_to_rename, key=lambda x: -len(x[0])):
|
||||||
@ -91,38 +91,38 @@ def autorename(input_path, max_height=720, debug=False):
|
|||||||
continue
|
continue
|
||||||
os.rename(old_dir, new_dir)
|
os.rename(old_dir, new_dir)
|
||||||
if debug:
|
if debug:
|
||||||
print(f'Renamed directory: {old_dir} -> {new_dir}')
|
print(f"Renamed directory: {old_dir} -> {new_dir}")
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
'''
|
"""
|
||||||
Main function to parse command line arguments and initiate file renamings.
|
Main function to parse command line arguments and initiate file renamings.
|
||||||
'''
|
"""
|
||||||
|
|
||||||
# Create argument parser
|
# Create argument parser
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
description=
|
description="Rename video files containing resolutions in their filenames to a specified max_height resolution."
|
||||||
'Rename video files containing resolutions in their filenames to a specified max_height resolution.'
|
|
||||||
)
|
)
|
||||||
|
|
||||||
# Define command line arguments
|
# Define command line arguments
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'input_path',
|
"input_path",
|
||||||
nargs='?',
|
nargs="?",
|
||||||
default=os.getcwd(),
|
default=os.getcwd(),
|
||||||
help=
|
help="directory path to search for video files (default: current directory)",
|
||||||
'directory path to search for video files (default: current directory)'
|
|
||||||
)
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-mh',
|
"-mh",
|
||||||
'--max-height',
|
"--max-height",
|
||||||
type=int,
|
type=int,
|
||||||
default=720,
|
default=720,
|
||||||
help='maximum height of videos to be converted (default: 720)')
|
help="maximum height of videos to be converted (default: 720)",
|
||||||
|
)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'--debug',
|
"--debug",
|
||||||
action='store_true',
|
action="store_true",
|
||||||
help='enable debug mode for printing additional messages')
|
help="enable debug mode for printing additional messages",
|
||||||
|
)
|
||||||
|
|
||||||
# Enable autocomplete for argparse
|
# Enable autocomplete for argparse
|
||||||
argcomplete.autocomplete(parser)
|
argcomplete.autocomplete(parser)
|
||||||
|
|||||||
@ -32,20 +32,48 @@ def process_subtitles(file_path, track, command):
|
|||||||
if command == "remove":
|
if command == "remove":
|
||||||
# Remove only the specified subtitle track while keeping all other streams
|
# Remove only the specified subtitle track while keeping all other streams
|
||||||
ffmpeg_command = [
|
ffmpeg_command = [
|
||||||
"ffmpeg", "-i", file_path, "-map", "0", "-map", f"-0:s:{track}",
|
"ffmpeg",
|
||||||
"-c", "copy", output_file
|
"-i",
|
||||||
|
file_path,
|
||||||
|
"-map",
|
||||||
|
"0",
|
||||||
|
"-map",
|
||||||
|
f"-0:s:{track}",
|
||||||
|
"-c",
|
||||||
|
"copy",
|
||||||
|
output_file,
|
||||||
]
|
]
|
||||||
elif command == "keep":
|
elif command == "keep":
|
||||||
# Keep only the specified subtitle track while preserving video, audio, and metadata
|
# Keep only the specified subtitle track while preserving video, audio, and metadata
|
||||||
ffmpeg_command = [
|
ffmpeg_command = [
|
||||||
"ffmpeg", "-i", file_path, "-map", "0:v", "-map", "0:a", "-map",
|
"ffmpeg",
|
||||||
f"0:s:{track}", "-map", "0:t?", "-c", "copy", output_file
|
"-i",
|
||||||
|
file_path,
|
||||||
|
"-map",
|
||||||
|
"0:v",
|
||||||
|
"-map",
|
||||||
|
"0:a",
|
||||||
|
"-map",
|
||||||
|
f"0:s:{track}",
|
||||||
|
"-map",
|
||||||
|
"0:t?",
|
||||||
|
"-c",
|
||||||
|
"copy",
|
||||||
|
output_file,
|
||||||
]
|
]
|
||||||
elif command == "none":
|
elif command == "none":
|
||||||
# Remove all subtitle tracks
|
# Remove all subtitle tracks
|
||||||
ffmpeg_command = [
|
ffmpeg_command = [
|
||||||
"ffmpeg", "-i", file_path, "-map", "0", "-map", "-0:s", "-c",
|
"ffmpeg",
|
||||||
"copy", output_file
|
"-i",
|
||||||
|
file_path,
|
||||||
|
"-map",
|
||||||
|
"0",
|
||||||
|
"-map",
|
||||||
|
"-0:s",
|
||||||
|
"-c",
|
||||||
|
"copy",
|
||||||
|
output_file,
|
||||||
]
|
]
|
||||||
else:
|
else:
|
||||||
print(f"{cred}Invalid command: {command}{creset}")
|
print(f"{cred}Invalid command: {command}{creset}")
|
||||||
@ -53,10 +81,9 @@ def process_subtitles(file_path, track, command):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
# Execute the ffmpeg command and capture output
|
# Execute the ffmpeg command and capture output
|
||||||
result = subprocess.run(ffmpeg_command,
|
result = subprocess.run(
|
||||||
stdout=subprocess.PIPE,
|
ffmpeg_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True
|
||||||
stderr=subprocess.PIPE,
|
)
|
||||||
text=True)
|
|
||||||
|
|
||||||
if result.returncode == 0:
|
if result.returncode == 0:
|
||||||
print(
|
print(
|
||||||
@ -91,13 +118,16 @@ def main():
|
|||||||
|
|
||||||
# Create argument parser
|
# Create argument parser
|
||||||
parser = argparse.ArgumentParser(
|
parser = argparse.ArgumentParser(
|
||||||
description="Manage subtitle tracks in video files.")
|
description="Manage subtitle tracks in video files."
|
||||||
|
)
|
||||||
|
|
||||||
# Define command line arguments
|
# Define command line arguments
|
||||||
# Add a positional argument for the command
|
# Add a positional argument for the command
|
||||||
parser.add_argument('command',
|
parser.add_argument(
|
||||||
choices=['remove', 'keep', 'none'],
|
"command",
|
||||||
help='Command to run (remove, keep, or none)')
|
choices=["remove", "keep", "none"],
|
||||||
|
help="Command to run (remove, keep, or none)",
|
||||||
|
)
|
||||||
|
|
||||||
# Add other arguments with both short and long options, including defaults
|
# Add other arguments with both short and long options, including defaults
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
@ -105,19 +135,16 @@ def main():
|
|||||||
"--track",
|
"--track",
|
||||||
type=int,
|
type=int,
|
||||||
default=0,
|
default=0,
|
||||||
help=
|
help="Subtitle track index (default is 0). Use 'none' to remove all subtitles.",
|
||||||
"Subtitle track index (default is 0). Use 'none' to remove all subtitles."
|
|
||||||
)
|
)
|
||||||
parser.add_argument("-f",
|
parser.add_argument("-f", "--file", type=str, help="Path to a specific video file.")
|
||||||
"--file",
|
|
||||||
type=str,
|
|
||||||
help="Path to a specific video file.")
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-d",
|
"-d",
|
||||||
"--dir",
|
"--dir",
|
||||||
type=str,
|
type=str,
|
||||||
default=os.getcwd(),
|
default=os.getcwd(),
|
||||||
help="Directory to process (default is current directory).")
|
help="Directory to process (default is current directory).",
|
||||||
|
)
|
||||||
|
|
||||||
# Enable autocomplete for command-line arguments
|
# Enable autocomplete for command-line arguments
|
||||||
argcomplete.autocomplete(parser)
|
argcomplete.autocomplete(parser)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user