-
Notifications
You must be signed in to change notification settings - Fork 3
/
cmplayer
executable file
·102 lines (89 loc) · 3.73 KB
/
cmplayer
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
#!/bin/sh
#
# A script to play video in *framebuffer* with *mplayer* and put it in the
# current tmux pane if in a *tmux* session. (*These* are requirements)
#
# TODO: pass mplayer options
if [ $# = 0 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ] || [ -n "$DISPLAY" ]; then
cat << EOF
Usage: [CHARW=<w>] [CHARH=<h>] [FB=<fb>] $(basename "$0") video ...
Note: You can specify the character size with environment variables if the
script didn't get the correct ones by
$ CHARW=10 CHARH=20 $(basename "$0") video
Note: This script is meant to be used under framebuffer (e.g. tty).
If \$DISPLAY variable is not empty, unset it with \`unset DISPLAY\` or
$ DISPLAY= $(basename "$0") video
Note: By default 'q' only quits the current mplayer process and goes on to the
next video. If you want to quit the whole video playing with 'q', you
have to put the following line in '~/.mplayer/input.conf':
q quit 1
The '1' can be anything that is not 0.
EOF
exit
fi
command -v mplayer > /dev/null 2>&1 || { echo Please install mplayer; exit; }
# Hide the cursor and texts
clear; tput civis
# Clean upon quit
trap 'clear; tput cnorm' INT TERM QUIT EXIT
# The framebuffer device name under /dev/
FB=${FB:-fb0}
# Calculate position and available size
IFS=, read -r screen_w screen_h < "/sys/class/graphics/$FB/virtual_size"
if [ -n "$TMUX_PANE" ]; then
# Calculate character width and height in pixels
cols=$(tmux display -p "#{client_width}")
rows=$(tmux display -p "#{client_height}")
charw=${CHARW:-$((screen_w / cols))}
charh=${CHARH:-$((screen_h / rows))}
# The following command get the pane's location and size (in unit of characters)
# The format of #{window_visible_layout} is loosely summarised recursively as:
# <format> = <width>x<height>,<x>,<y>(,<id>|{<format>}|[<format>])
# We only need the ones with ids, which means they don't have child structures.
IFS=x, read -r w h x y _ << EOF
$(tmux display -p -F "#{window_visible_layout}" \
| grep -o "\([0-9]*\)x\([0-9]*\),\([0-9]*\),\([0-9]*\),${TMUX_PANE#%}")
EOF
pane_w=$((w * charw))
pane_h=$((h * charh))
pane_x=$((x * charw))
pane_y=$((y * charh))
else
# Use framebuffer size if not in tmux
pane_x=0
pane_y=0
pane_w=$screen_w
pane_h=$screen_h
fi
# Play each of the videos separately (deal with different video sizes)
for video in "$@"; do
# Get the real size of the video
IFS=,: read -r orig_w orig_h sar_x sar_y << EOF
$(ffprobe "$video" -v quiet -select_streams v:0 -of csv=s=,:p=0 \
-show_entries stream="width,height,sample_aspect_ratio")
EOF
# Skip if it is not a video
[ "$orig_w" -gt 0 ] || continue
# fix the aspect ratio if sar is present
[ "$sar_x" != "N/A" ] && [ "$sar_x" = "$sar_x" ] && [ "$sar_y" = "$sar_y" ] &&
orig_w=$((orig_w * sar_x / sar_y))
# Calculate the actual video size and position that could fit in the pane
if [ $((orig_w * pane_h)) -gt $((orig_h * pane_w)) ]; then
video_w=$pane_w
video_h=$((pane_w * orig_h / orig_w))
video_x=$pane_x
video_y=$((pane_y + (pane_h - video_h) / 2))
else
video_w=$((pane_h * orig_w / orig_h))
video_h=$pane_h
video_x=$((pane_x + (pane_w - video_w) / 2))
video_y=$pane_y
fi
# This line and the line after mplayer exits is a hack to refresh tmux,
# mplayer seems to black out the whole framebuffer when it starts
[ -n "$TMUX_PANE" ] && { sleep 1; tmux refresh; } &
# Mute all output and play the video!
mplayer "$video" -vo fbdev2 -msglevel all=-1 -nolirc -noconfig user \
-vf scale="$video_w:$video_h" -geometry "$video_x:$video_y" || exit
[ -n "$TMUX_PANE" ] && tmux refresh
done