mass-video-transcode/convert.py

75 lines
3.2 KiB
Python
Raw Permalink Normal View History

2020-05-08 13:32:23 +00:00
#!/usr/bin/python3
# Import the required libraries
2020-05-08 13:32:23 +00:00
from os import listdir, system
from os.path import isfile, join
# Set the in and out folders
InFolder="FilesIn"
OutFolder="FilesOut"
# Chose actions for subs, video, audio, and images
2021-08-06 12:10:15 +00:00
handbrake = True
SubtitleAction = "copy"
AudioAction = "libvorbis"
VideoAction = "h264_nvenc"
ImageAction = "jpeg"
2020-05-08 13:32:23 +00:00
# Make a list with all the files in the appropriate folder
onlyfiles = [f for f in listdir(InFolder) if isfile(join(InFolder, f))]
2020-05-08 13:32:23 +00:00
# Check file type and do the appropriate action
2020-05-08 13:32:23 +00:00
for f in onlyfiles:
InFilePath = "./" + InFolder + "/" + f
2023-05-19 16:04:21 +00:00
print("file: " + InFilePath)
2021-08-06 12:10:15 +00:00
# Video
if f[-4:] == ".mkv" or f[-4:] == ".mp4":
print(f + " is a " + f[-3:] + " file, transcoding...")
OutFilePath = "./" + OutFolder + "/" + f[:-4] + ".mkv"
# Transcode
2021-08-06 12:10:15 +00:00
if handbrake:
system("HandBrakeCLI -i \"" + InFilePath + "\" -o \"" + OutFilePath + "\" -e nvenc_h264 -q 22 -E vorbis -s 1 --subtitle-default none --verbose 0 && rm \"" + InFilePath + "\"")
#system("HandBrakeCLI -i \"" + InFilePath + "\" -o \"" + OutFilePath + "\" -e nvenc_h264 -q 22 -E vorbis --all-audio --all-subtitles --subtitle-default none --verbose 0 && rm \"" + InFilePath + "\"")
else:
system("ffmpeg -loglevel 0 -i \"" + InFilePath + "\" -c:s " + SubtitleAction + " -c:a " + AudioAction + " -c:v " + VideoAction + " \"" + OutFilePath + "\" && rm \"" + InFilePath + "\"")
# Audio
if f[-5:] == ".opus":
print(f + " is a " + f[-4:] + " file, converting...")
OutFilePath = "./" + OutFolder + "/" + f[:-5] + ".ogg"
# Transcode
2023-05-19 16:04:21 +00:00
system("ffmpeg -loglevel 0 -vn -i \"" + InFilePath + "\" -c:a " + AudioAction + " \"" + OutFilePath + "\" && rm \"" + InFilePath + "\"")
2021-08-06 12:10:15 +00:00
if f[-4:] == ".m4a":
print(f + " is a " + f[-3:] + " file, converting...")
OutFilePath = "./" + OutFolder + "/" + f[:-4] + ".ogg"
# Transcode
2023-05-19 16:04:21 +00:00
system("ffmpeg -loglevel 0 -vn -i \"" + InFilePath + "\" -c:a " + AudioAction + " \"" + OutFilePath + "\" && rm \"" + InFilePath + "\"")
# If we run it through again, we convert the OGG to an MP3
if f[-4:] == ".ogg":
print(f + " is a " + f[-3:] + " file, converting...")
OutFilePath = "./" + OutFolder + "/" + f[:-4] + ".mp3"
# Transcode
system("ffmpeg -loglevel 0 -vn -i \"" + InFilePath + "\" -c:a " + "libmp3lame" + " \"" + OutFilePath + "\" && rm \"" + InFilePath + "\"")
if f[-5:] == ".webm":
print(f + " is a " + f[-4:] + " file, converting...")
OutFilePath = "./" + OutFolder + "/" + f[:-5] + ".ogg"
# Transcode
system("ffmpeg -loglevel 0 -vn -i \"" + InFilePath + "\" -c:a " + AudioAction + " \"" + OutFilePath + "\" && rm \"" + InFilePath + "\"")
2021-08-06 12:10:15 +00:00
# Images
if f[-5:] == ".webp":
print(f + " is a " + f[-4:] + " file, converting...")
OutFilePath = "./" + OutFolder + "/" + f[:-4] + ImageAction
# Convert
2021-08-06 12:10:15 +00:00
system("convert \"" + InFilePath + "\" \"" + OutFilePath + "\" && rm \"" + InFilePath + "\"")
# Print status message
print("That was all the files.")
2020-05-08 13:32:23 +00:00
# Thank the user
print("Thank you for using my tool!")