# Variables
INPUT_DIR  := .
OUTPUT_DIR := 1080p
#FFMPEG_FLAGS := -c:v libx264 -crf 23 -preset medium -c:a aac -b:a 128k
FFMPEG_FLAGS := -hide_banner -y -vf "scale='if(gt(iw,ih),min(1920,iw),-2)':'if(gt(iw,ih),-2,min(1080,ih))'" -c:v libx264 -crf 20 -preset veryslow -c:a aac -b:a 128k -movflags +faststart
#FFMPEG_FLAGS := -vf "scale='min(1920,iw)':-1" -c:v libx264 -crf 22 -preset veryslow -c:a aac -b:a 128k -movflags +faststart

# Find all .mp4 files in the current directory
SRCS := $(wildcard $(INPUT_DIR)/*.mp4)

# Define the output files by prefixing the output directory to the source filenames
OBJS := $(patsubst $(INPUT_DIR)/%.mp4, $(OUTPUT_DIR)/%.mp4, $(SRCS))

# Default target
all: $(OUT_DIR_PATH) $(OBJS)

# Rule to create the output directory if it doesn't exist
$(OUTPUT_DIR):
	mkdir -p $(OUTPUT_DIR)

# Pattern rule to process each file
# $< is the input file, $@ is the output file
$(OUTPUT_DIR)/%.mp4: %.mp4 | $(OUTPUT_DIR)
	#ffmpeg -i "$<" $(FFMPEG_FLAGS) "$@"
	ffmpeg -y -i "$<" $(FFMPEG_FLAGS) "tmp/$(notdir $@)"
	mv -v "tmp/$(notdir $@)" "$@"

# Clean target to remove processed files
#clean:
#	rm -rf $(OUTPUT_DIR)

.PHONY: all clean
