-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscreenshot.sh
69 lines (50 loc) · 2.07 KB
/
screenshot.sh
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
#!/bin/bash
# Description: Automatically take 8 screenshots of a video using mpv
# Screenshots are saved in 01.png, 02.png, ..., 08.png
# Author: Wisperer
# Usage: ./screenshots.sh <videofilename> <# of screenshots>
# Requirements: mpv, grep, sed
### Global variables
filename="$1"
### Error handling
if [ -z "$filename" ];
then
echo "ERROR: No video file supplied. Please enter a video file as argument."
exit 1;
fi
# For some unknown reason, the last screenshot is not generated by mplayer.
# Therefore, always add 1 more to the total number of screenshots that you want.
NUM_OF_SCREENSHOTS=9
if [ ! -z $2 ];
then
NUM_OF_SCREENSHOTS=$2
echo "WARNING: Overwrite default number of screenshots to ${NUM_OF_SCREENSHOTS}."
sleep 3s
fi
# Get the total length of the video in seconds.
# Use mplayer to display the info of the video and then get the value of ID_LENGTH, the total number of seconds of the video.
total_length=$(mplayer -identify -frames 0 -vc null -vo null -ao null "$filename" | grep ID_LENGTH | sed 's/ID_LENGTH=//' | sed 's/\..*//')
# Reference https://github.com/mpv-player/mpv/blob/master/TOOLS/mpv_identify.sh
# Remove 4 seconds from the video so that it doesn't take screenshot at the ends.
let total_length4=4
# time_slice: At which time interval should mplayer take screenshots.
let time_slice=${total_length}/${NUM_OF_SCREENSHOTS}
# time_at: When should mplayer take screenshots.
time_at=${time_slice};
# Looping to take screenshots.
for ((i=1; i <= NUM_OF_SCREENSHOTS ; i++))
do
# Create unique filename.
padding=$(printf %03d ${i})
name=$(basename "$1")
# Take the screenshot.
# mplayer -loop 1 -nosound -frames 1 -ss ${time_at} -vo png:z=9 "$filename"
mpv --quiet --no-audio --vo=image --vo-image-format=png --start=${time_at} --frames=1 "$filename"
# Increment to the next time slice.
let time_at+=${time_slice}
sleep 2s
# Move the screenshot 00000001.png to 0X.png
mkdir ~/Pictures/"${name}"
mv "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/"00000001.* ~/Pictures/"${name}"/${padding}.png
sleep 2s
done