Skip to content

Commit

Permalink
feat: Add input validation and scaling factor to convert_mkv_to_mp4 f…
Browse files Browse the repository at this point in the history
…unction

- Implemented input checks to ensure the input folder exists and contains .mkv files.
- Added a scale factor parameter to the convert_mkv_to_mp4 function to allow resizing of the output videos.
- Improved error handling and user feedback for better usability.
  • Loading branch information
healthonrails committed Mar 7, 2024
1 parent e169e4c commit 58de7a4
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions annolid/utils/videos.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@
import subprocess


def convert_mkv_to_mp4(input_folder, output_folder):
def convert_mkv_to_mp4(input_folder, output_folder, scale_factor):
# Ensure the input folder exists
if not os.path.exists(input_folder):
print("Input folder does not exist.")
return

# Ensure the output folder exists, create it if it doesn't
if not os.path.exists(output_folder):
os.makedirs(output_folder)

# Get a list of all .mkv files in the input folder
mkv_files = [f for f in os.listdir(input_folder) if f.endswith('.mkv')]

if not mkv_files:
print("No .mkv files found in the input folder.")
return

for mkv_file in mkv_files:
input_path = os.path.join(input_folder, mkv_file)
output_path = os.path.join(
Expand All @@ -18,7 +27,7 @@ def convert_mkv_to_mp4(input_folder, output_folder):
# Run ffmpeg command to convert mkv to mp4 and resize
cmd = [
'ffmpeg', '-i', input_path,
'-vf', 'scale=iw/2:ih/2',
'-vf', f'scale=iw*{scale_factor}:ih*{scale_factor}',
'-c:v', 'libx264', '-crf', '23',
'-c:a', 'aac', '-b:a', '128k',
output_path
Expand All @@ -35,4 +44,10 @@ def convert_mkv_to_mp4(input_folder, output_folder):
input_folder = input("Enter the input folder path containing .mkv files: ")
output_folder = input(
"Enter the output folder path for converted .mp4 files: ")
convert_mkv_to_mp4(input_folder, output_folder)
scale_factor = float(
input("Enter the scale factor (e.g., 0.5 for half size): "))

if scale_factor <= 0:
print("Scale factor must be greater than 0.")
else:
convert_mkv_to_mp4(input_folder, output_folder, scale_factor)

0 comments on commit 58de7a4

Please sign in to comment.