Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 110 lines (97 sloc) 3.649 kb
#!/bin/bash
set -e -u
if [[ $# -ne 1 ]]; then
echo "Usage: ${0##*/} <video-file>"
echo "Press 's' during playback to save a screenshot. Works with -vo vdpau."
echo "If nothing happens when you press 's', you may want to add the following lines to ~/.mplayer/input.conf:"
echo "s get_time_pos"
echo "a get_percent_pos"
exit 0
fi
screenshot_dir=~/.mplayer/screenshots
mkdir -p "$screenshot_dir"
next_file() { (
cd "$screenshot_dir"
for file in $1???.$2; do last=$file; done
if [[ $last = $1\?\?\?.$2 ]]; then
printf "%s000.%s" "$1" "$2"
else
printf "%s%03d.%s" "$1" \
$(( 1 + $(echo $last | sed "s/$1\([0-9]*\)\.$2/\1/;s/^0*\(.\)/\1/") )) "$2"
fi
) }
convert_seconds_to_mkvmerge_format() {
local seconds=$1
local hours=$(echo $seconds/3600 | bc)
seconds=$(echo $seconds-$hours*3600 | bc)
local minutes=$(echo $seconds/60 | bc)
seconds=$(echo $seconds-$minutes*60 | bc)
printf '%02d:%02d:%06.3f\n' "$hours" "$minutes" "$seconds"
}
extract_segment() {
local source=$1
local target=$2
local ogg_target=${target%.mkv}.ogg
local start=$(convert_seconds_to_mkvmerge_format $3)
local end=$(convert_seconds_to_mkvmerge_format $4)
local temp_dir=$(mktemp -d)
echo -n 'Please wait, extracting segment...'
mkvmerge -o "$temp_dir/result.mkv" --split timecodes:$start,$end "$source" >/dev/null
mv "$temp_dir/result-002.mkv" "$target"
echo -n 'reencoding audio...'
ffmpeg -y -i "$target" "$temp_dir/result.wav" &>/dev/null
oggenc -o "$ogg_target" "$temp_dir/result.wav" &>/dev/null
rm -r "$temp_dir"
echo 'done.'
}
mode=screenshot
start=
target_size=
while read -r line; do
target_size_temp=$(printf '%s' "$line" | sed 's/^VO: \[[^]]*\] [0-9]\+x[0-9]\+ => \([0-9]\+x[0-9]\+\) .*$/\1/;t;d')
if [[ $target_size_temp ]]; then
echo "Target size for screenshots is $target_size_temp."
target_size="-s $target_size_temp"
fi
time=$(printf '%s' "$line" | sed 's/^ANS_TIME_POSITION=\(.*\)$/\1/;t;d')
switch_mode=$(printf '%s' "$line" | sed 's/^ANS_PERCENT_POSITION=\(.*\)$/\1/;t;d')
[[ $time || $switch_mode ]] || continue
if [[ $switch_mode ]]; then
if [[ $mode = screenshot ]]; then
mode=audio
echo 'Switched to audio mode!'
continue
else
mode=screenshot
start=
echo 'Switched to screenshot mode!'
continue
fi
fi
if [[ $mode = screenshot ]]; then
target=$(next_file screen png)
echo "Saving screenshot at time ${time}s..."
if ffmpeg -ss "$time" -vframes 1 -i "$1" $target_size "$screenshot_dir/$target" &>/dev/null; then
echo "Saved screenshot: $screenshot_dir/$target"
else
echo "Could not save screenshot. An error occured."
fi
else
if [[ ! $start ]]; then
start=$time
echo "Starting capture at time ${time}s..."
else
target=$(next_file segment mkv)
duration=$( echo "$time - $start" | bc )
echo "Extracting segment from time ${start}s to ${time}s (${duration}s in total)..."
#echo ffmpeg -ss "$time" -t "$duration" -i "$1" -vn -acodec copy "$screenshot_dir/$target"
if extract_segment "$1" "$screenshot_dir/$target" $start $time; then
echo "Saved segment: $screenshot_dir/$target"
else
echo "Could not save segment. An error occured."
fi
start=
fi
fi
done < <(/usr/bin/mplayer -quiet "$1")
exit 0
Jump to Line
Something went wrong with that request. Please try again.