nix

configuration files that power my machines
Log | Files | Refs | README | LICENSE

seek (1406B)


      1 #!/bin/bash
      2 
      3 if [ $# -eq 0 ]; then
      4 	echo "Usage: $0 <percentage>"
      5 	echo "Example: $0 25  # seeks to 25% of current track"
      6 	exit 1
      7 fi
      8 
      9 percentage=$1
     10 
     11 if ! [[ "$percentage" =~ ^[0-9]+(\.[0-9]+)?$ ]] || (($(echo "$percentage > 100" | bc -l))); then
     12 	echo "Error: Please provide a valid percentage (0-100)"
     13 	exit 1
     14 fi
     15 
     16 status=$(mpc status)
     17 
     18 if ! echo "$status" | grep -q "playing\|paused"; then
     19 	echo "Error: No track is currently loaded"
     20 	exit 1
     21 fi
     22 
     23 time_info=$(echo "$status" | grep -o '[0-9]*:[0-9]*/[0-9]*:[0-9]*')
     24 
     25 if [ -z "$time_info" ]; then
     26 	echo "Error: Could not get track duration information"
     27 	exit 1
     28 fi
     29 
     30 total_time=$(echo "$time_info" | cut -d'/' -f2)
     31 
     32 # Convert total time to seconds
     33 IFS=':' read -ra time_parts <<<"$total_time"
     34 if [ ${#time_parts[@]} -eq 2 ]; then
     35 	# Format is MM:SS
     36 	total_seconds=$((${time_parts[0]} * 60 + ${time_parts[1]}))
     37 elif [ ${#time_parts[@]} -eq 3 ]; then
     38 	# Format is HH:MM:SS
     39 	total_seconds=$((${time_parts[0]} * 3600 + ${time_parts[1]} * 60 + ${time_parts[2]}))
     40 else
     41 	echo "Error: Could not parse time format"
     42 	exit 1
     43 fi
     44 
     45 # Calculate target position in seconds
     46 target_seconds=$(echo "scale=0; $total_seconds * $percentage / 100" | bc)
     47 
     48 # Convert back to MM:SS format for mpc
     49 target_minutes=$((target_seconds / 60))
     50 target_seconds_remainder=$((target_seconds % 60))
     51 target_time=$(printf "%d:%02d" $target_minutes $target_seconds_remainder)
     52 
     53 mpc seek "$target_time"