summaryrefslogtreecommitdiff
path: root/ar/.local/bin/mpvplay
blob: 7a43bd3f14c66eb07169514da8ab29c38b163287 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#!/bin/sh

# --- Configuration ---
scripts_dir="${XDG_SCRIPTS_HOME:-${HOME}/.local/bin}"
db_path="$HOME/.local/share/history/mpv.sqlite"
playlist_dir="$HOME/.config/mpv/playlists"
mpv_opts="--x11-name=video"
ytdl_format="bestvideo[height<=1080]+bestaudio/best[height<=1080]"

# Managed mount points: "mountpoint|mount_cmd|unmount_cmd" (one per line)
MANAGED_MOUNTS="$HOME/Secret|$scripts_dir/ecrypt|$scripts_dir/ecrypt
/mnt/second|$scripts_dir/mounter|$scripts_dir/unmounter"

# Video file extensions for find commands
VIDEO_EXTS='-iname "*.mp4" -o -iname "*.mkv" -o -iname "*.mov" -o -iname "*.flv" -o -iname "*.wmv" -o -iname "*.webm" -o -iname "*.mpeg" -o -iname "*.mpg" -o -iname "*.avi" -o -iname "*.ts" -o -iname "*.3gp" -o -iname "*.rmvb"'

# --- Utility Functions ---
find_videos() {
  eval "find \"$1\" -maxdepth 1 -type f \\( $VIDEO_EXTS \\)" | sort -V
}

has_videos() {
  eval "find \"$1\" -maxdepth 1 -type f \\( $VIDEO_EXTS \\) -print -quit" | grep -q .
}

is_mounted() { findmnt "$1" >/dev/null 2>&1; }

# Return the managed mount point for a given file path
get_managed_mount() {
  while IFS='|' read -r mp mcmd ucmd; do
    [ -n "$mp" ] && echo "$1" | grep -q "^$mp/" && echo "$mp" && return 0
  done <<EOF
$MANAGED_MOUNTS
EOF
}

mount_path() {
  while IFS='|' read -r mp mcmd ucmd; do
    [ -n "$mp" ] && echo "$1" | grep -q "^$mp" && ! is_mounted "$mp" && $mcmd && return 0
  done <<EOF
$MANAGED_MOUNTS
EOF
}

unmount_path() {
  while IFS='|' read -r mp mcmd ucmd; do
    [ -n "$mp" ] && [ "$mp" = "$1" ] && is_mounted "$mp" && $ucmd && return 0
  done <<EOF
$MANAGED_MOUNTS
EOF
}

unmount_all() {
  while IFS='|' read -r mp mcmd ucmd; do
    [ -n "$mp" ] && is_mounted "$mp" && $ucmd
  done <<EOF
$MANAGED_MOUNTS
EOF
}

play() { mpv $mpv_opts "$@"; }

# --- URL Playback ---
loginurl() {
  notify-send "Authentication required"
  username="$(echo | dmenu -i -p "Enter a username:")"
  [ -z "$username" ] && exit
  password="$(echo | dmenu -i -P -p "Enter a password:")"
  [ -z "$password" ] && exit
  if ! play --ytdl-format="$ytdl_format" --ytdl-raw-options=username="$username",password="$password" "$url"; then
    notify-send "Failed to play $url" "Check your username or password"
    exit 1
  fi
}

play_url() {
  url=$(xclip -selection clipboard -o)
  echo "$url" | grep -qE '^https?://' || return 1
  if yt-dlp --simulate --dump-json "$url" >/dev/null 2>&1; then
    notify-send "Playing video from URL:" "$url"
    play --ytdl-format="$ytdl_format" "$url"
  else
    loginurl
  fi
}

# --- Local File Playback ---
play_media() {
  _path=""
  for _a in "$@"; do
    case "$_a" in --*) ;; *) _path="$_a" ;; esac
  done
  _mp=$(get_managed_mount "$_path")
  if [ -n "$_mp" ]; then
    mount_path "$_path"
    play "$@" && unmount_path "$_mp" || exit
  else
    play "$@" || exit
  fi
}

play_playlist() {
  # Check if playlist references any managed mount path
  _mp=""
  while IFS='|' read -r mp mcmd ucmd; do
    [ -n "$mp" ] && grep -q "$mp/" "$1" 2>/dev/null && _mp="$mp" && break
  done <<EOF
$MANAGED_MOUNTS
EOF
  if [ -n "$_mp" ]; then
    play --playlist="$1" && unmount_path "$_mp" || exit
  else
    play --playlist="$1" || exit
  fi
}

make_tmp_playlist() {
  [ -d "$playlist_dir" ] || mkdir -p "$playlist_dir"
  tmplist="$playlist_dir/tmplist.m3u"
  [ -f "$tmplist" ] && rm -f "$tmplist"
  find_videos "$1" >"$tmplist"
  play_playlist "$tmplist"
  rm -f "$tmplist"
}

list_and_play() {
  dir="$1"
  has_videos "$dir" || { echo "No video files in $dir." && exit; }

  choice=$(printf "List files\nEnter filenames" | dmenu -i -p "Choose an option:")
  case "$choice" in
  "Enter filenames")
    search_term=$(echo | dmenu -i -p "File names:")
    [ -z "$search_term" ] && exit
    notify-send "Finding videos named with '$search_term'.."
    files=$(eval "find \"$dir\" -type f \\( $VIDEO_EXTS \\) -iname \"*$search_term*\"" | sort -V)
    [ -z "$files" ] && echo "No files named with \"$search_term\"." && exit
    tmpplaylist=$(mktemp /tmp/mpv_playlist_XXXXXX.m3u)
    echo "$files" >"$tmpplaylist"
    play_playlist "$tmpplaylist"
    rm -f "$tmpplaylist"
    ;;
  "List files")
    files_with_paths=$(find_videos "$dir")
    selected_file=$(printf "All\n%s" "$files_with_paths" | sed 's|.*/||' | dmenu -i -l 21 -p "Select a file:")
    [ -z "$selected_file" ] && exit
    [ "$selected_file" = "All" ] && make_tmp_playlist "$dir" && return
    full_path=$(echo "$files_with_paths" | grep -F "$selected_file")
    [ -f "$full_path" ] && play_media "$full_path" && return
    ;;
  esac
}

# --- Directory Browser ---
list_nonempty_subdirs() {
  find "$1" -mindepth 1 -maxdepth 1 -type d ! -name ".*" 2>/dev/null | while read -r d; do
    [ -n "$(ls -A "$d" 2>/dev/null)" ] && echo "$d"
  done
}

browse_local() {
  dir_list="$HOME/Downloads
$HOME/Torrents/complete
$HOME/Videos"

  # Add managed mount points (always shown, even if not yet mounted)
  while IFS='|' read -r mp mcmd ucmd; do
    [ -n "$mp" ] && [ -d "$mp" ] && dir_list="$dir_list
$mp"
  done <<EOF
$MANAGED_MOUNTS
EOF

  # Add other mounted media directories (excluding managed ones already listed)
  for base in "/media/$USER" "/mnt"; do
    [ -d "$base" ] || continue
    extra=$(list_nonempty_subdirs "$base" | while read -r d; do
      echo "$dir_list" | grep -qxF "$d" || echo "$d"
    done)
    [ -n "$extra" ] && dir_list="$dir_list
$extra"
  done

  init_dir=$(printf "%s" "$dir_list" | dmenu -i -p "Choose your initial directory:")
  [ -z "$init_dir" ] && exit
  mount_path "$init_dir"

  selected_dir="$init_dir"
  while true; do
    subdir_names=$(list_nonempty_subdirs "$selected_dir" | while read -r d; do basename "$d"; done | sort -V)
    [ -z "$subdir_names" ] && list_and_play "$selected_dir" && break

    if has_videos "$selected_dir"; then
      options="Files
$subdir_names"
    else
      options="$subdir_names"
    fi

    pick=$(printf "%s" "$options" | dmenu -i -p "Select a directory or 'Files':")
    [ -z "$pick" ] && exit
    [ "$pick" = "Files" ] && list_and_play "$selected_dir" && break
    selected_dir="$selected_dir/$pick"
  done
}

# --- Playlist and History ---
choose_playlist() {
  [ -d "$playlist_dir" ] || mkdir -p "$playlist_dir"
  playlist=$(find "$playlist_dir" -maxdepth 1 -type f -name "*.m3u" -exec basename {} .m3u \; | sort -V | dmenu -i -p "Select a playlist:")
  [ -z "$playlist" ] && exit
  play_playlist "$playlist_dir/$playlist.m3u"
}

history_play() {
  [ -f "$db_path" ] || {
    echo "Error: SQLite database not found at $db_path" >&2
    exit 1
  }

  history=$(
    sqlite3 "$db_path" <<'SQL'
WITH LatestFiles AS (
    SELECT path, title, time_pos, MAX(date) AS max_date
    FROM loaded_items
    GROUP BY path
),
FormattedHistory AS (
    SELECT path, title,
        CASE WHEN time_pos IS NOT NULL THEN
            printf('%02d:%02d:%02d', time_pos / 3600, (time_pos % 3600) / 60, time_pos % 60)
        ELSE '00:00:00' END AS formatted_time,
        max_date
    FROM LatestFiles
)
SELECT path || ' | ' || title || ' | ' || formatted_time
FROM FormattedHistory
ORDER BY max_date DESC;
SQL
  )

  [ -z "$history" ] && {
    echo "No history items found." >&2
    exit 1
  }

  temp_file=$(mktemp)
  echo "$history" | while IFS= read -r line; do
    file_path=$(printf '%s\n' "$line" | awk -F ' \\| ' '{print $1}')
    [ -f "$file_path" ] && printf '%s\n' "$line" >>"$temp_file"
  done

  [ -s "$temp_file" ] || {
    echo "No valid history items (all files missing)." >&2
    rm -f "$temp_file"
    exit 1
  }

  chosen=$(dmenu -i -l 20 -p "Choose a file to play:" <"$temp_file")
  rm -f "$temp_file"
  [ -z "$chosen" ] && exit

  file_path=$(printf '%s\n' "$chosen" | awk -F ' \\| ' '{print $1}')
  formatted_time=$(printf '%s\n' "$chosen" | awk -F ' \\| ' '{print $3}')
  time_pos=$(printf '%s\n' "$formatted_time" | awk -F: '{print ($1 * 3600) + ($2 * 60) + $3}')

  if [ "$time_pos" -gt 0 ]; then
    play_media --start="$time_pos" "$file_path"
  else
    play_media "$file_path"
  fi
}

# --- Main ---
trap 'unmount_all; exit' EXIT INT

content_choice=$(printf "URL\nLocal Files\nPlaylist\nHistory" | dmenu -i -p "Choose media source:")
case "$content_choice" in
"URL") play_url ;;
"Local Files") browse_local ;;
"Playlist") choose_playlist ;;
"History") history_play ;;
*) exit ;;
esac