add twitter-convert

This commit is contained in:
Sridhar Ratnakumar 2024-03-06 08:54:01 -05:00
parent 167c43b474
commit 532f335b3c
5 changed files with 40 additions and 0 deletions

View file

@ -2,6 +2,7 @@
self: super: {
fuckport = self.callPackage ./fuckport.nix { };
twitter-convert = self.callPackage ./twitter-convert { };
nixci = flake.inputs.nixci.packages.${system}.default;
nix-health = flake.inputs.nix-browser.packages.${system}.nix-health;
actual = flake.inputs.actual.packages.${system}.default;

View file

@ -0,0 +1,18 @@
{ writeShellApplication, ffmpeg-full, ... }:
# 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-full ];
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"
'';
}

View file

@ -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")"

View file

@ -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