Skip to content

Instantly share code, notes, and snippets.

@pituz
Last active August 29, 2015 14:04
Show Gist options
  • Save pituz/8f0635a4b3222266e088 to your computer and use it in GitHub Desktop.
Save pituz/8f0635a4b3222266e088 to your computer and use it in GitHub Desktop.
converts currently playing audio track + picture from clipboard to webm video
#!/usr/bin/env zsh
# exit on errors
set -e
tempdir="/tmp"
if [[ -n $1 ]]
then
audio_file=$1
track_length="$(ffprobe $1 2>&1|sed -n 's/.*Duration: \([^,]\+\),.*/\1/p')"
elif pgrep mocp > /dev/null
then
audio_file="$(mocp -i | sed -n 's/^File: //p')"
output_name="$(mocp -i | sed -n 's/^Title: //p')"
track_length="$(mocp -i | sed -n 's/^TotalTime: //p')"
elif pgrep deadbeef > /dev/null
then
audio_file="$(deadbeef --nowplaying %F)"
output_name="$(deadbeef --nowplaying %t)"
track_length="$(deadbeef --nowplaying %l)"
else
echo "nothing to do"
exit 1
fi
if [[ -n $2 ]]; then
# picture got as second argument
picture_file=$2
elif [[ "$(xclip -t TARGETS -o)" =~ image ]]; then
# clipboard has a picture!
xclip -t image/png -o $tempdir/p.png
picture_file=$tempdir/p.png
else
# get picture path from clipboard
picture_file="$(xclip -o)"
# workaround for geeqie (it copies file path with single quotes)
[[ picture_file =~ ^'.+'$ ]] && picture_file="$(echo $picture_file | sed "s/^'//; s/'$//; s/'\\\\'//;")"
fi
[[ -z $output_name ]] && output_name=${audio_file:t:r}
# replace slashes with backslashes
[[ $output_name =~ '/' ]] && output_name=${output_name:gs/\//\\}
aopts=(-q:a 5)
#aopts=(-c:a libopus -application audio -vbr on -b:a 95k -strict -2)
aopts=(-c:a copy)
[[ -f $output_name.webm ]] && echo "$f exists" && false
ffmpeg -hide_banner -i $audio_file -vn -sn $aoptsi -map_metadata 0 -y $tempdir/a.webm
for pass in 1 2
ffmpeg -hide_banner -loop 1 -r 1 -i $picture_file \
-t $track_length -pass $pass -auto-alt-ref 1 -lag-in-frames 16 -y $tempdir/v.webm
#-vf 'setpts=10*PTS' \
mkvmerge /tmp/v.webm /tmp/a.webm -o $output_name.webm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment