Guest User

ffmpeg clips splitting

a guest
Nov 11th, 2025
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.23 KB | None | 0 0
  1. #!/bin/bash
  2. # Check if file, number of clips, and duration are provided
  3. if [ -z "$1" ] || [ -z "$2" ]; then
  4.     echo "Usage: $0 /path/to/video.mp4 <number_of_clips> [clip_duration]"
  5.     exit 1
  6. fi
  7.  
  8. video_file="$1"
  9. num_clips="$2"
  10. clip_duration="${3:-10}"  # Default 10 sec se non specificato
  11.  
  12. # Get video duration in seconds using ffprobe
  13. duration=$(ffprobe -v error -show_entries format=duration -of default=noprint_wrappers=1:nokey=1 "$video_file")
  14. total_seconds=$(printf "%.0f\n" "$duration")
  15.  
  16. # Get original FPS using ffprobe (per log, non forzato)
  17. 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
  18. echo "FPS originali del video: $original_fps"
  19.  
  20. # Generate the caption base from the file name (without extension) - sanitizza punti per nomi piΓΉ puliti
  21. filename=$(basename "$video_file")
  22. caption_base="${filename%.*}"
  23. caption_base=$(echo "$caption_base" | sed 's/\./_/g')  # Sostituisci . con _ per evitare issues
  24.  
  25. # Scale filter per 720p senza padding (proporzionale, gestisce portrait senza bande nere)
  26. scale_filter="scale=1280:720:force_original_aspect_ratio=decrease"
  27.  
  28. # Loop to generate N random clips
  29. for ((i=1; i<=num_clips; i++)); do
  30.     # Random start time, ensuring enough room for clip
  31.     max_start=$((total_seconds - clip_duration))
  32.     if [ $max_start -le 0 ]; then
  33.         start_time=0
  34.     else
  35.         start_time=$((RANDOM % max_start))
  36.     fi
  37.  
  38.     # End time
  39.     end_time=$((start_time + clip_duration))
  40.  
  41.     # Format times as HH:MM:SS
  42.     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)))
  43.     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)))
  44.  
  45.     # Caption with clip number and timestamps
  46.     caption="${caption_base}_clip${i}_${start_formatted}-${end_formatted}"
  47.  
  48.     # Output file
  49.     output_clip="${caption}.webm"
  50.  
  51.     # Comando FFmpeg ottimizzato per <4MB su 4chan (CRF 40, audio 64k, cpu-used 2)
  52.     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"
  53.  
  54.     # Debug: Stampa il comando
  55.     echo "Eseguendo: $ffmpeg_cmd"
  56.  
  57.     # Esegui e controlla errore
  58.     if eval "$ffmpeg_cmd"; then
  59.         # Controlla dimensione file (<4MB = 4194304 bytes)
  60.         file_size=$(stat -f%z "$output_clip" 2>/dev/null || stat -c%s "$output_clip")  # Cross-platform
  61.         if [ "$file_size" -lt 4194304 ]; then
  62.             echo "Creata clip separata: $output_clip (${file_size} bytes, da $start_time sec per $clip_duration sec, FPS: $original_fps) - OK per 4chan!"
  63.         else
  64.             echo "Creata clip separata: $output_clip (${file_size} bytes) - AVVISO: SOPRA 4MB! Prova CRF 45 o audio 48k."
  65.         fi
  66.     else
  67.         echo "Errore nella creazione di $output_clip (codice FFmpeg: $?)."
  68.     fi
  69. done
  70.  
  71. echo "Done! Tutte le clip separate in WebM 720p VP9 (<4MB) sono state create nella directory corrente."
  72.  
Advertisement
Add Comment
Please, Sign In to add comment