112 lines
3.2 KiB
Bash
112 lines
3.2 KiB
Bash
#!/bin/bash
|
|
|
|
output_folder="/var/www/html/watermarked_videos/"
|
|
output_base_url="https://ca2324.ddns.net:8443/watermarked_videos/"
|
|
# Input parameters
|
|
input_video_url="$1"
|
|
watermark_png_url="$2"
|
|
pos_x="${3:-10}" # Default to 10 if not provided
|
|
pos_y="${4:-10}" # Default to 10 if not provided
|
|
size_w="${5:-0}" # Default to 0 (keep aspect ratio) if not provided
|
|
size_h="${6:-0}" # Default to 0 (keep aspect ratio) if not provided
|
|
|
|
# Validate required inputs
|
|
if [ -z "$input_video_url" ] || [ -z "$watermark_png_url" ]; then
|
|
echo "Error: Missing required parameters" >&2
|
|
echo "Usage: $0 input_video_url watermark_png_url [pos_x] [pos_y] [size_w] [size_h]" >&2
|
|
exit 1
|
|
fi
|
|
|
|
# Create temp directory
|
|
temp_dir="/tmp/watermark_$$"
|
|
mkdir -p "$temp_dir" || {
|
|
echo "Error: Failed to create temporary directory" >&2
|
|
exit 1
|
|
}
|
|
|
|
# Download input video
|
|
video_filename=$(basename "$input_video_url")
|
|
local_video_path="$temp_dir/$video_filename"
|
|
if ! curl -s -L -o "$local_video_path" "$input_video_url"; then
|
|
echo "Error: Failed to download input video from $input_video_url" >&2
|
|
rm -rf "$temp_dir"
|
|
exit 1
|
|
fi
|
|
|
|
# Download watermark image
|
|
watermark_filename=$(basename "$watermark_png_url")
|
|
local_watermark_path="$temp_dir/$watermark_filename"
|
|
if ! curl -s -L -o "$local_watermark_path" "$watermark_png_url"; then
|
|
echo "Error: Failed to download watermark image from $watermark_png_url" >&2
|
|
rm -rf "$temp_dir"
|
|
exit 1
|
|
fi
|
|
|
|
# Validate downloaded files
|
|
if [ ! -f "$local_video_path" ]; then
|
|
echo "Error: Downloaded video file not found" >&2
|
|
rm -rf "$temp_dir"
|
|
exit 1
|
|
fi
|
|
|
|
if [ ! -f "$local_watermark_path" ]; then
|
|
echo "Error: Downloaded watermark file not found" >&2
|
|
rm -rf "$temp_dir"
|
|
exit 1
|
|
fi
|
|
|
|
# Generate a UUID-style output filename
|
|
output_uuid=$(uuidgen)
|
|
output_file="$temp_dir/$output_uuid.mp4"
|
|
|
|
# Prepare ffmpeg filter for watermark
|
|
if [ "$size_w" -eq 0 ] && [ "$size_h" -eq 0 ]; then
|
|
# No resizing
|
|
filter_complex="overlay=$pos_x:$pos_y"
|
|
else
|
|
# Resize watermark before overlay
|
|
filter_complex="overlay=$pos_x:$pos_y:scale=$size_w:$size_h"
|
|
fi
|
|
|
|
# Add watermark using ffmpeg and encode to HEVC (H.265)
|
|
if ! ffmpeg -y -loglevel error -i "$local_video_path" -i "$local_watermark_path" \
|
|
-filter_complex "$filter_complex" \
|
|
-c:v libx265 -tag:v hvc1 -crf 24 -preset veryfast \
|
|
-c:a copy \
|
|
-movflags +faststart \
|
|
"${output_file}" 1>&2 ; then
|
|
echo "Error: Failed to add watermark to video" >&2
|
|
rm -rf "$temp_dir"
|
|
exit 1
|
|
fi
|
|
|
|
# Move output file to the output folder
|
|
if ! mv "$output_file" "$output_folder"; then
|
|
echo "Error: Failed to move output file to $output_folder" >&2
|
|
rm -rf "$temp_dir"
|
|
exit 1
|
|
fi
|
|
# Clean up temporary files
|
|
rm -rf "$temp_dir"
|
|
# Generate the final output file path
|
|
output_file="$output_folder/$output_uuid.mp4"
|
|
# Generate the final output URL
|
|
output_file_url="${output_base_url}${output_uuid}.mp4"
|
|
|
|
# Return JSON with parameters and output filename
|
|
cat << EOF
|
|
{
|
|
"input_video_url": "$input_video_url",
|
|
"watermark_png_url": "$watermark_png_url",
|
|
"pos_x": $pos_x,
|
|
"pos_y": $pos_y,
|
|
"size_w": $size_w,
|
|
"size_h": $size_h,
|
|
"output_file_name": "$output_file",
|
|
"output_file_url": "$output_file_url"
|
|
}
|
|
EOF
|
|
|
|
# Note: We're not deleting the temp directory so that the output file remains available
|
|
|
|
exit 0 |