Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Check if file, number of clips, and duration are provided
- if [ -z "$1" ] || [ -z "$2" ]; then
- echo "Usage: $0 /path/to/video.mp4 <number_of_clips> [clip_duration]"
- exit 1
- fi
- video_file="$1"
- num_clips="$2"
- clip_duration="${3:-10}" # Default 10 sec se non specificato
- # Get video duration in seconds using ffprobe
- duration=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$video_file")
- total_seconds=$(printf "%.0f\n" "$duration")
- # Get original FPS using ffprobe (per log, non forzato)
- original_fps=$(ffprobe -v error -select_streams v:0 -show_entries stream=r_frame_rate -of default=noprint_wrappers=1:nokey=1 "$video_file" | awk -F/ '{print ($1/$2)}' 2>/dev/null || echo "30") # Fallback a 30 se errore
- echo "FPS originali del video: $original_fps"
- # Generate the caption base from the file name (without extension) - sanitizza punti per nomi piΓΉ puliti
- filename=$(basename "$video_file")
- caption_base="${filename%.*}"
- caption_base=$(echo "$caption_base" | sed 's/\./_/g') # Sostituisci . con _ per evitare issues
- # Scale filter per 720p senza padding (proporzionale, gestisce portrait senza bande nere)
- scale_filter="scale=1280:720:force_original_aspect_ratio=decrease"
- # Loop to generate N random clips
- for ((i=1; i<=num_clips; i++)); do
- # Random start time, ensuring enough room for clip
- max_start=$((total_seconds - clip_duration))
- if [ $max_start -le 0 ]; then
- start_time=0
- else
- start_time=$((RANDOM % max_start))
- fi
- # End time
- end_time=$((start_time + clip_duration))
- # Format times as HH:MM:SS
- start_formatted=$(date -d@$start_time -u +%H:%M:%S 2>/dev/null || printf "%02d:%02d:%02d" $((start_time/3600)) $(((start_time%3600)/60)) $((start_time%60)))
- end_formatted=$(date -d@$end_time -u +%H:%M:%S 2>/dev/null || printf "%02d:%02d:%02d" $((end_time/3600)) $(((end_time%3600)/60)) $((end_time%60)))
- # Caption with clip number and timestamps
- caption="${caption_base}_clip${i}_${start_formatted}-${end_formatted}"
- # Output file
- output_clip="${caption}.webm"
- # Comando FFmpeg ottimizzato per <4MB su 4chan (CRF 40, audio 64k, cpu-used 2)
- ffmpeg_cmd="ffmpeg -ss $start_time -i \"$video_file\" -t $clip_duration -vf \"$scale_filter\" -c:v libvpx-vp9 -crf 40 -b:v 0 -cpu-used 2 -c:a libopus -b:a 64k -threads 0 -f webm \"$output_clip\" -y"
- # Debug: Stampa il comando
- echo "Eseguendo: $ffmpeg_cmd"
- # Esegui e controlla errore
- if eval "$ffmpeg_cmd"; then
- # Controlla dimensione file (<4MB = 4194304 bytes)
- file_size=$(stat -f%z "$output_clip" 2>/dev/null || stat -c%s "$output_clip") # Cross-platform
- if [ "$file_size" -lt 4194304 ]; then
- echo "Creata clip separata: $output_clip (${file_size} bytes, da $start_time sec per $clip_duration sec, FPS: $original_fps) - OK per 4chan!"
- else
- echo "Creata clip separata: $output_clip (${file_size} bytes) - AVVISO: SOPRA 4MB! Prova CRF 45 o audio 48k."
- fi
- else
- echo "Errore nella creazione di $output_clip (codice FFmpeg: $?)."
- fi
- done
- echo "Done! Tutte le clip separate in WebM 720p VP9 (<4MB) sono state create nella directory corrente."
Advertisement
Add Comment
Please, Sign In to add comment