Skip to content

Instantly share code, notes, and snippets.

@hraban
Last active June 16, 2022 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hraban/ab04b28d6748cdc288cf2edd28cd661c to your computer and use it in GitHub Desktop.
Save hraban/ab04b28d6748cdc288cf2edd28cd661c to your computer and use it in GitHub Desktop.
Convert a movie file to gif and copy its contents to clipboard
#!/usr/bin/env bash
# Convert a movie file to gif and copy its contents to clipboard.
#
# Technically: creates a .gif file in a temp dir and copies the path to
# clipboard, but that should be transparent.
set -euo pipefail
input="${1?Usage: $0 <input>}"
f="$(mktemp).gif"
# Don't delete the file when done: the path is copied to the clipboard, not the contents.
ffmpeg -i "$input" -vf "fps=10,scale=320:-1:flags=lanczos" -c:v pam -f image2pipe - | magick convert -delay 10 - -loop 0 -layers optimize "$f"
# Copy only the path, leave the file on disk. It's the only way I know of to
# copy a GIF to clipboard in macos.
osascript -e 'set the clipboard to (POSIX file "'"$f"'")'
# Sources:
# - https://stackoverflow.com/a/30578507/4359699
# - https://superuser.com/a/556031/442895
# - https://stackoverflow.com/a/51447031/4359699
# Copyright © 2022 Hraban Luyat
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment