#!/bin/bash # Check if video.txt file exists if [ ! -f video.txt ]; then echo "video.txt file not found!" exit 1 fi # Path to Bento4 binaries BENTO4_PATH="/data/bento4/bin" # Function to check if a command exists command_exists () { type "$1" &> /dev/null ; } # Check if yt-dlp is installed if ! command_exists yt-dlp ; then echo "yt-dlp is not installed. Please install it first." exit 1 fi # Check if ffmpeg is installed if ! command_exists ffmpeg ; then echo "ffmpeg is not installed. Please install it first." exit 1 fi # Check if Bento4 mp4hls is installed if [ ! -x "$BENTO4_PATH/mp4hls" ]; then echo "Bento4 mp4hls is not installed or not executable. Please check the path." exit 1 fi # Check if aws-cli is installed if ! command_exists aws ; then echo "aws-cli is not installed. Please install it first." exit 1 fi # Read URLs from video.txt while IFS= read -r URL; do # Output folder with dynamic name based on current date OUTPUT_FOLDER=$(date +"%Y%m%d_%H%M%S") mkdir -p "$OUTPUT_FOLDER" # Download subtitles using yt-dlp yt-dlp --write-subs --skip-download -o "$OUTPUT_FOLDER/%(title)s.%(ext)s" "$URL" # Check if the subtitle file exists SUBTITLE_FILE=$(find "$OUTPUT_FOLDER" -type f -name "*.vtt") if [ -z "$SUBTITLE_FILE" ]; then echo "No subtitle file found. Subtitles will be skipped in packaging." HAS_SUBTITLES=false else # Rename the subtitle file to english.vtt mv "$SUBTITLE_FILE" "$OUTPUT_FOLDER/english.vtt" if [ $? -eq 0 ]; then echo "Subtitles downloaded and renamed to english.vtt." HAS_SUBTITLES=true else echo "Failed to rename subtitle file. Subtitles will be skipped in packaging." HAS_SUBTITLES=false fi fi # Download the best video and audio formats available yt-dlp -o "$OUTPUT_FOLDER/%(title)s.%(ext)s" "$URL" --merge-output-format mp4 # Extract the downloaded video file name VIDEO_FILE=$(find "$OUTPUT_FOLDER" -type f -name "*.mp4") # Extract the first two words of the video title VIDEO_TITLE=$(basename "$VIDEO_FILE" .mp4) FIRST_TWO_WORDS=$(echo "$VIDEO_TITLE" | awk '{print $1" "$2}' | tr '[:upper:]' '[:lower:]' | tr -cd '[:alnum:] ') OUTPUT_DIR=$(echo "${FIRST_TWO_WORDS// /_}_trailer") # Separate audio and save as 192kbps MP4 ffmpeg -i "$VIDEO_FILE" -vn -c:a aac -b:a 192k "$OUTPUT_FOLDER/audio_192kbps.mp4" # Function to convert video to specific resolution with optimized settings convert_video() { local input_file=$1 local resolution=$2 local output_file=$3 ffmpeg -i "$input_file" -vf "scale=-2:$resolution" -c:v libx264 -preset slow -crf 23 -an "$output_file" } # Convert video to 1080p, 720p, and 480p with optimized settings convert_video "$VIDEO_FILE" 1080 "$OUTPUT_FOLDER/video_1080p.mp4" convert_video "$VIDEO_FILE" 720 "$OUTPUT_FOLDER/video_720p.mp4" convert_video "$VIDEO_FILE" 480 "$OUTPUT_FOLDER/video_480p.mp4" # Wait for 3 seconds before packaging sleep 3 # Package the video, audio, and optionally subtitles using Bento4 mp4hls if [ "$HAS_SUBTITLES" = true ]; then "$BENTO4_PATH/mp4hls" "$OUTPUT_FOLDER/video_1080p.mp4" "$OUTPUT_FOLDER/video_720p.mp4" "$OUTPUT_FOLDER/video_480p.mp4" "[+language=en]$OUTPUT_FOLDER/audio_192kbps.mp4" "[+format=webvtt,+language=eng]$OUTPUT_FOLDER/english.vtt" --output-dir="$OUTPUT_FOLDER/$OUTPUT_DIR" else "$BENTO4_PATH/mp4hls" "$OUTPUT_FOLDER/video_1080p.mp4" "$OUTPUT_FOLDER/video_720p.mp4" "$OUTPUT_FOLDER/video_480p.mp4" "[+language=en]$OUTPUT_FOLDER/audio_192kbps.mp4" --output-dir="$OUTPUT_FOLDER/$OUTPUT_DIR" fi echo "Download, conversion, and packaging completed. Files saved in $OUTPUT_FOLDER/$OUTPUT_DIR" # Define variables for S3 upload LOCAL_FOLDER="$OUTPUT_FOLDER/$OUTPUT_DIR" BUCKET_NAME="innorelay-demo" BUCKET_PATH="v2/trailers/${OUTPUT_DIR}" # Upload to S3 aws s3 cp "$LOCAL_FOLDER" "s3://${BUCKET_NAME}/${BUCKET_PATH}/" --recursive if [ $? -eq 0 ]; then echo "Files successfully uploaded to s3://${BUCKET_NAME}/${BUCKET_PATH}/" else echo "Failed to upload files to S3." exit 1 fi # Clean up: delete the output folder rm -rf "$OUTPUT_FOLDER" if [ $? -eq 0 ]; then echo "Temporary files deleted." else echo "Failed to delete temporary files." exit 1 fi done < video.txt