convert movie format to mp4 ?
this code are scanning for example mkv and convert all to mp4
#!/bin/bash
# Help message function
print_help() {
echo "Usage: $0 <file_extension>"
echo "Example: $0 mkv"
}
# Check if argument is provided
if [ $# -eq 0 ]; then
print_help
exit 1
fi
# Supported video file formats
supported_formats=("avi" "mkv" "mov" "mp4" "wmv" "flv" "mpg" "mpeg" "3gp")
# Check if provided file extension is supported
if [[ ! " ${supported_formats[@]} " =~ " $1 " ]]; then
echo "Unsupported file format. Supported formats: ${supported_formats[*]}"
print_help
exit 1
fi
# Loop through all files with the provided extension in the current directory
for file in *."$1"; do
# Check if the file exists
if [ -e "$file" ]; then
# Get the filename without extension
filename="${file%.*}"
# Convert the file to mp4 using ffmpeg
ffmpeg -i "$file" "${filename}2.mp4"
# Optional: Check if conversion was successful and remove the original file
if [ $? -eq 0 ]; then
echo "Conversion of $file successful."
rm "$file"
else
echo "Conversion of $file failed."
fi
else
echo "No files with .$1 extension found."
exit 1
fi
done
why not.. we do it in php to… but now you most write a dir to.
<?php
// Function to convert video files to mp4
function convertVideosToMp4($folder, $extension) {
// Supported video file formats
$supportedFormats = ["avi", "mkv", "mov", "mp4", "wmv", "flv", "mpg", "mpeg", "3gp"];
// Check if provided file extension is supported
if (!in_array($extension, $supportedFormats)) {
echo "Unsupported file format. Supported formats: " . implode(", ", $supportedFormats) . PHP_EOL;
return;
}
// Open the directory
if ($handle = opendir($folder)) {
// Loop through all files in the directory
while (false !== ($file = readdir($handle))) {
// Check if the file has the provided extension
if (pathinfo($file, PATHINFO_EXTENSION) == $extension) {
// Get the filename without extension
$filename = pathinfo($file, PATHINFO_FILENAME);
// Convert the file to mp4 using ffmpeg and rename to {filename}2.mp4
$command = "ffmpeg -i \"$folder/$file\" \"$folder/{$filename}2.mp4\"";
exec($command, $output, $return_var);
// Check if conversion was successful and remove the original file
if ($return_var == 0) {
echo "Conversion of $file successful." . PHP_EOL;
unlink("$folder/$file");
} else {
echo "Conversion of $file failed." . PHP_EOL;
}
}
}
// Close the directory handle
closedir($handle);
} else {
echo "Unable to open directory: $folder" . PHP_EOL;
}
}
// Check if folder and extension are provided
if ($argc < 3) {
echo "Usage: php convert.php <folder_path> <file_extension>" . PHP_EOL;
echo "Example: php convert.php /path/to/folder mkv" . PHP_EOL;
exit(1);
}
// Get folder path and extension from command line arguments
$folderPath = $argv[1];
$extension = $argv[2];
// Call function to convert videos to mp4
convertVideosToMp4($folderPath, $extension);
?>
why not… we do it in python to
import os
import subprocess
import sys
def convert_videos_to_mp4(folder, extension):
# Supported video file formats
supported_formats = ["avi", "mkv", "mov", "mp4", "wmv", "flv", "mpg", "mpeg", "3gp"]
# Check if provided file extension is supported
if extension not in supported_formats:
print(f"Unsupported file format. Supported formats: {', '.join(supported_formats)}")
return
# Loop through all files in the folder
for file in os.listdir(folder):
# Check if the file has the provided extension
if file.endswith(f".{extension}"):
# Get the filename without extension
filename = os.path.splitext(file)[0]
# Convert the file to mp4 using ffmpeg and rename to {filename}2.mp4
command = f"ffmpeg -i \"{os.path.join(folder, file)}\" \"{os.path.join(folder, filename)}2.mp4\""
subprocess.run(command, shell=True)
# Check if conversion was successful and remove the original file
if os.path.exists(os.path.join(folder, f"{filename}2.mp4")):
print(f"Conversion of {file} successful.")
os.remove(os.path.join(folder, file))
else:
print(f"Conversion of {file} failed.")
# Check if folder and extension are provided
if len(sys.argv) < 3:
print("Usage: python convert.py <folder_path> <file_extension>")
print("Example: python convert.py /path/to/folder mkv")
sys.exit(1)
# Get folder path and extension from command line arguments
folder_path = sys.argv[1]
extension = sys.argv[2]
# Call function to convert videos to mp4
convert_videos_to_mp4(folder_path, extension)