diff --git a/configurations/home/srid@zest.nix b/configurations/home/srid@zest.nix index 611a27f..e371c91 100644 --- a/configurations/home/srid@zest.nix +++ b/configurations/home/srid@zest.nix @@ -33,5 +33,6 @@ in home.packages = [ inputs.disc-scrape.packages.${pkgs.stdenv.hostPlatform.system}.default pkgs.zellij-one + pkgs.twitter-convert ]; } diff --git a/packages/twitter-convert/default.nix b/packages/twitter-convert/default.nix new file mode 100644 index 0000000..b760277 --- /dev/null +++ b/packages/twitter-convert/default.nix @@ -0,0 +1,18 @@ +{ writeShellApplication, ffmpeg, ... }: + +# Some useful resources for this undocumented shit: +# - https://stackoverflow.com/q/59056863/55246 +# - https://gist.github.com/nikhan/26ddd9c4e99bbf209dd7 +writeShellApplication { + name = "twitter-convert"; + runtimeInputs = [ ffmpeg ]; + meta.description = '' + Convert a video for uploading to X / Twitter. + + You may need a Basic or Premium tier to upload longer videos. + ''; + text = '' + export ARSCRIPT="${./ffmpeg_ar}" + ${./ffmpeg_twitter} "$1" + ''; +} diff --git a/packages/twitter-convert/ffmpeg_ar b/packages/twitter-convert/ffmpeg_ar new file mode 100755 index 0000000..a40aa8d --- /dev/null +++ b/packages/twitter-convert/ffmpeg_ar @@ -0,0 +1,12 @@ +#!/bin/bash +eval $(ffprobe -v error -show_entries stream=width,height -of default=noprint_wrappers=1 "$1") +#results in e.g. +#width=300 +#height=1200 +echo "height=$height"$'\n'"width=$width" +newenv=$(bc <<< "width=$width; height=$height;"$'\nscale=3; aspect=width / height;\nprint "aspect=", aspect; + print "''\n'$'";\n\nif (aspect>3) { r=(height) * (aspect/3); print "height=";} + if (aspect<(1/3)) {r=(width) / (aspect/(1/3)); print "width=";}\nscale=0 + if (r) { print r/1 }') +[ ! -z "$newenv" ] && echo "$newenv" && export $newenv +echo "aspect=""$(bc <<< "scale=3; $width/$height")" diff --git a/packages/twitter-convert/ffmpeg_twitter b/packages/twitter-convert/ffmpeg_twitter new file mode 100755 index 0000000..6cd45fb --- /dev/null +++ b/packages/twitter-convert/ffmpeg_twitter @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +set -xe + +export $($ARSCRIPT "$1") +ffmpeg -i "$1" \ + -filter_complex 'fps=30,format=yuv420p,pad='"ceil($width/2)*2:ceil($height/2)*2"':(ow-iw)/2:(oh-ih)/2' -c:v h264_videotoolbox -c:a aac -ac 2 -ar 44100 -r 30 \ + output.mp4 \ No newline at end of file