Skip to content

Latest commit

 

History

History
87 lines (73 loc) · 6.57 KB

File metadata and controls

87 lines (73 loc) · 6.57 KB

ffmpeg is FLOSS (no cost), plus is the best tool to transcode/trim/demux/mux. ffmpeg is on Windows&Linux&Android OS&iOS&OSX.

[This post allows all uses.] Is now on SubStack.

Tools compatible with this howto:

Table of Contents

Howto

You can use ../sh/Transcode.sh to do all of this for you; what follows is the manual route. [Notice: versus stock Android, have moved default paths /Music/ to /Sounds/, /Movies/ to /Visuals/.]

[Notice: if /storage/emulated/0/ (directory root) is not found, replace with /sdcard/.]

[Notice: Can use examples with FFmpeg Media Encoder or Termux as-is (use absolute paths).]

Produce .mp4

Example visuals.mp4 was 4gb, to compress to 224mb used:

nice ffmpeg -i "/storage/emulated/0/Visuals/screen-20240629-045526.mp4" -framerate 30 -c:v libx264 -crf 32 -preset slower "/storage/emulated/0/Visuals/visuals.mp4"

. The libx264 codec compresses visuals best. -preset slower instructs it to compress more. You can replace -crf 32 with -b:v 2m to set an exact goal of “compress to 2mbps”.

[Notice: On some devices, Android OS’s permissions require to output to /storage/emulated/0/Download/]

Suppose you want to mux sounds.mp4 with visuals.mp4,

but you want to skip sounds.mp4’s 4 second intro, plus limit output to 2 minutes:

Produce .m4a

To demux sounds, pass -ss 4 to skip 4 seconds, pass -t 2:00 to output 2 minutes, pass -map 0:a:0 (zero-indexed) to demux first input as sounds, pass -c copy for instant process, output as .m4a:

nice ffmpeg -i "/storage/emulated/0/Download/sounds.mp4 -ss 4 -t 2:00 -map 0:a:0 -c copy "/storage/emulated/0/Sounds/demux.m4a"

Mix visuals plus sounds into .mp4

Now demux.m4a is 2 minutes, but visuals.m4a is much longer; pass -stream_loop -1 to mux sounds as loop to match visuals.mp4:

nice ffmpeg -i "/storage/emulated/0/Visuals/visuals.mp4" -stream_loop -1 -i "/storage/emulated/0/Sounds/demux.m4a" -map 0:v:0 -c copy -map 1:a:0 -shortest "/storage/emulated/0/Visuals/mux.mp4"

Suppose you want the mix the sounds from visuals.mp4 with the loop from mux.mp4:

nice ffmpeg -i "/storage/emulated/0/Visuals/visuals.mp4" -stream_loop -1 -i "/storage/emulated/0/Sounds/demux.m4a" -map 0:a:0 -map 1:a:0 -filter_complex amix=inputs=2:duration=shortest "/storage/emulated/0/Sounds/demux2.m4a"
nice ffmpeg -i "/storage/emulated/0/Visuals/visuals.mp4" -i "/storage/emulated/0/Sounds/demux2.m4a" -map 0:v:0 -c copy -map 1:a:0 -shortest "/storage/emulated/0/Visuals/mux2.mp4"

[Notice: -c copy is not compatible with -filter_complex; unless you want to reincode the visuals (slow), is 2 steps to do this]

Produce .gif

Suppose you wish to produce a 10fps HD .gif from the first 24 seconds of visual.mp4:

nice ffmpeg -i "/storage/emulated/0/Visuals/visual.mp4" -map 0:v:0 -r 10 -s 1920x1080 -t 24 "/storage/emulated/0/Visuals/visual.gif"

or, if you have ImageMagick installed (apt install magick):

nice ffmpeg -i "/storage/emulated/0/Visuals/visual.mp4" -map 0:v:0 -r 10 -s 1920x1080 -t 24 -f image2pipe -vcodec ppm - | convert -delay $(expr 100 / 10) - "/storage/emulated/0/Visuals/visual.gif"

will use more disk but has dither and palette improved. Optimization (lossless compression, such as: duplicate frames and duplicate palettes are reduced).

gifsicle -O2 "/storage/emulated/0/Visuals/visual.gif" --batch

External resources

Lists of commands&options which ffmpeg can use:

How to use extra tools (which ffmpeg's GPLv2 version has): https://github.com/FFmpeg/FFmpeg/blob/master/LICENSE.md

mux.mp4/mux2.mp4 syntax was used to produce:

visual.gif syntax was used to produce:

Video Transcoder - Apps on Google (a visual interface to ffmpeg) was cool versus most “video editor” apps -- but is not available for new versions of Android OS, can not loop (just has trim + convert (which can act as demux) + resize + compress), is slow (can not pass -c copy to ffmpeg, thus always reincodes inputs.)