#!/bin/bash
# Check if required parameters are provided
if [ $# -lt 3 ]; then
echo "Usage: $0 <video_url> <output_name> <output_type> [start_time] [end_time]"
echo "Output types: webm (4chan with audio), webm-na (4chan no audio)"
echo "Example: $0 https://www.youtube.com/watch?v=dQw4w9WgXcQ my_clip webm 00:00:10 00:00:25"
exit 1
fi
# Assign parameters to variables
URL=$1
OUTPUT_NAME=$2
OUTPUT_TYPE=$3
START_TIME=${4:-0}
END_TIME=${5:-}
# Function to check file size
check_file_size() {
local file=$1
local size=$(du -k "$file" | cut -f1)
if [ $size -gt 6000 ]; then
echo "Warning: $file is larger than 6000KB ($size KB)"
fi
}
# Download video with yt-dlp
yt-dlp $COOKIE_OPTION -f "bv*+ba/b" $URL -o "temp_video.%(ext)s"
# Get the extension of the downloaded file
EXT=$(ls temp_video.* | sed 's/.*\.//')
# Prepare FFmpeg input options
if [ -n "$END_TIME" ]; then
INPUT_OPTIONS="-ss $START_TIME -to $END_TIME"
else
INPUT_OPTIONS="-ss $START_TIME"
fi
# Process based on output type
case $OUTPUT_TYPE in
webm)
ffmpeg $INPUT_OPTIONS -i "temp_video.$EXT" -c:v libvpx -c:a libvorbis -crf 4 -b:v 1500K -vf scale=1280:-1 -threads 4 "${OUTPUT_NAME}.webm"
check_file_size "${OUTPUT_NAME}.webm"
echo "Output file: ${OUTPUT_NAME}.webm"
;;
webm-na)
ffmpeg $INPUT_OPTIONS -i "temp_video.$EXT" -c:v libvpx -an -crf 4 -b:v 1500K -vf scale=1280:-1 -threads 4 "${OUTPUT_NAME}_nosound.webm"
check_file_size "${OUTPUT_NAME}_nosound.webm"
echo "Output file: ${OUTPUT_NAME}_nosound.webm"
;;
*)
echo "Invalid output type. Use webm, webm-na."
exit 1
;;
esac
# Clean up temporary file
rm "temp_video.$EXT"
echo "Conversion complete."
I also have an iphone case for sending videos to friends with an iphone, and a bit for using browser cookies to rip twitter links I'm sent. Saved in my ~/bin as vid-clip