Advertisement
Guest User

Untitled

a guest
Oct 6th, 2024
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.04 KB | Source Code | 0 0
  1. #!/bin/bash
  2. set -x #echo on
  3. SOURCE_VIDEO="$1"
  4. START_TIME="$2"
  5. END_TIME="$3"
  6.  
  7. # Get total number of frames for progress tracking
  8. TOTAL_FRAMES=$(ffprobe -v error -select_streams v:0 -count_packets -show_entries stream=nb_read_packets -of csv=p=0 "$SOURCE_VIDEO")
  9. if [ -z "$TOTAL_FRAMES" ]; then
  10.     echo "Error: Unable to retrieve the total number of frames."
  11.     exit 1
  12. fi
  13.  
  14. # Initialize variables for tracking progress
  15. frames_processed=0
  16. start_frame=""
  17. end_frame=""
  18. start_diff=999999
  19. end_diff=999999
  20.  
  21. # Function to display progress and information dynamically
  22. display_progress() {
  23.     local progress=$(( frames_processed * 100 / TOTAL_FRAMES ))
  24.     echo -ne "\rProcessed frames: $frames_processed / $TOTAL_FRAMES ($progress%) | Closest start frame: $start_frame | Closest end frame: $end_frame"
  25. }
  26. # Process frames
  27. ffprobe -show_frames -select_streams v:0 \
  28.         -print_format csv "$SOURCE_VIDEO" 2>&1 |
  29. grep -n frame,video,1 |
  30. awk 'BEGIN { FS="," } { print $1 " " $5 }' |
  31. sed 's/:frame//g' |
  32. awk -v start="$START_TIME" -v end="$END_TIME" '
  33. BEGIN {
  34.    start_frame="";
  35.    end_frame="";
  36.    start_diff=999999;
  37.    end_diff=999999;
  38.    between_frames="";
  39. }
  40. {
  41.    split($2, time, ".");
  42.    current = time[1];
  43.    
  44.    if (current > end) {
  45.        exit;  
  46.    }
  47.    
  48.    if (start_frame == "" && current >= start) {
  49.        start_frame = $1;
  50.        start_diff = current - start;
  51.    } else if (current >= start && (current - start) < start_diff) {
  52.        start_frame = $1;
  53.        start_diff = current - start;
  54.    }
  55.    
  56.    if (current <= end && (end - current) < end_diff) {
  57.        end_frame = $1;
  58.        end_diff = end - current;
  59.    }
  60.    
  61.    if (current >= start && current <= end) {
  62.        between_frames = between_frames $1 ",";
  63.    }
  64. }
  65. END {
  66.    print "\nProcessing completed."
  67.    print "Closest keyframe to start time: " start_frame;
  68.    print "Closest keyframe to end time: " end_frame;
  69.    print "All keyframes between start and end:";
  70.    print substr(between_frames, 1, length(between_frames)-1);
  71. }'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement