Skip to content

Instantly share code, notes, and snippets.

@nikhan
Created January 1, 2016 04:45
Show Gist options
  • Star 70 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save nikhan/26ddd9c4e99bbf209dd7 to your computer and use it in GitHub Desktop.
Save nikhan/26ddd9c4e99bbf209dd7 to your computer and use it in GitHub Desktop.
twitter ffmpeg
ffmpeg -i test.mov -vcodec libx264 -vf 'scale=640:trunc(ow/a/2)*2' -acodec aac -vb 1024k -minrate 1024k -maxrate 1024k -bufsize 1024k -ar 44100 -strict experimental -r 30 out.mp4
@Platane
Copy link

Platane commented Mar 1, 2017

@pirmax https://twittercommunity.com/t/ffmpeg-mp4-upload-to-twitter-unsupported-error/68602/2
worked for me,
I guess the option missing is the one about pixel format, -pix_fmt yuv420p

@RichardBronosky
Copy link

This worked for me on a screencast created via QuickTime Player

ffmpeg -i $input -vcodec libx264 -pix_fmt yuv420p -strict -2 -acodec aac ${input%.*}.mp4

https://twitter.com/RichardBronosky/status/932433069465899009

@foone
Copy link

foone commented May 18, 2018

I use this variant. This fixes scaling on windows (which breaks with the single quotes), includes Platane's fix, and also downmixes to stereo so that surround sound videos don't break it:

ffmpeg -i in.mkv -pix_fmt yuv420p -vcodec libx264 -vf scale=640:-1 -acodec aac -vb 1024k -minrate 1024k -maxrate 1024k -bufsize 1024k -ar 44100  -ac 2  -strict experimental -r 30  out.mp4

@RichardBronosky
Copy link

If all you need to do is convert to mp4, this leaves everything else unchanged.

ffmpeg -i in.mov -c copy out.mp4

@brandonprry
Copy link

Foone's command works for me.

@galaxy001
Copy link

-t 2:20 is also needed.

@hertzsprung
Copy link

It's also necessary to round to an even number of pixel dimensions:

ffmpeg -i test.mov -vcodec libx264 -vf "pad=ceil(iw/2)*2:ceil(ih/2)*2" -pix_fmt yuv420p -strict experimental -r 30 -t 2:20 -acodec aac -vb 1024k -minrate 1024k -maxrate 1024k -bufsize 1024k -ar 44100 -ac 2 out.mp4

@Pomax
Copy link

Pomax commented Oct 27, 2020

Also, based on Twitter's JSON response, the aspect ratio can only be 3:1 max

@nhed
Copy link

nhed commented Jul 3, 2021

I definitely needed a -t as I was trying to send a 5 second video (using the standard UI) and the screen error was generic ... had to examine the JSON in the response to know they thought it was too short.

@tinevez
Copy link

tinevez commented Jan 5, 2022

@Pomax remark fixed my issue. The aspect ratio was the problem in my case but nothing tells it is the case.

@olleharstedt
Copy link

@hertzsprung This one worked for me

@warp16
Copy link

warp16 commented Jul 31, 2022

@hertzsprung thank you, your command worked for a troublesome DVD rip.

@DangerBlack
Copy link

@hertzsprung This one worked for me

@jamylak
Copy link

jamylak commented Oct 19, 2022

It's also necessary to round to an even number of pixel dimensions:

ffmpeg -i test.mov -vcodec libx264 -vf "pad=ceil(iw/2)*2:ceil(ih/2)*2" -pix_fmt yuv420p -strict experimental -r 30 -t 2:20 -acodec aac -vb 1024k -minrate 1024k -maxrate 1024k -bufsize 1024k -ar 44100 -ac 2 out.mp4

Great!

@ctrlcctrlv
Copy link

ctrlcctrlv commented Oct 26, 2022

-vf "pad=ceil(iw/2)*2:ceil(ih/2)*2"

This is sadly insufficient for narrow/tall inputs.

Aspect ratio must be between 1:3 and 3:1.

I'm sorry, this is complex.

ffmpeg_ar

#!/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")"

Example I/O

ffmpeg_ar /tmp/modem.mkv
height=100
width=1000
aspect=10.000
height=333
aspect=3.003

ffmpeg

You can then:

export $( ffmpeg_ar /tmp/in.mkv );
ffmpeg -i in.mkv -filter_complex 'fps=30,format=yuv420p,pad='"ceil($width/2)*2:ceil($height/2)*2"':(ow-iw)/2:(oh-ih)/2' -c:v h264_nvenc -t 02:20 -c:a aac -ac 2 -ar 44100 -r 30 /tmp/out.mp4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment