Back to Silas S. Brown's home page

Notes on video conversion with mencoder

These are some quick notes I made to provide starting points for mencoder command lines. It is meant for conversions that are legal (own work, personal use of products without DRM, etc). I am not an mencoder expert but just collected some options that worked for me.

Input from: DVD | video file | rtmp stream | Flash audio | edits | audio file | FM | language merge | desktop | pictures
Output to: Android, iPhone or Mac | Nokia S60, S40 etc | Windows 10 | Zoom Cloud Meetings | Old Windows or Windows Mobile | MP3 audio | DVD, VCD or SVCD


Input options

DVD

.flv and other video files

rtmp streams embedded in complex Javascript etc

You need root access to a GNU/Linux machine with rtmpdump

Flash audio

On a GNU/Linux machine with ~/.asoundrc set to
pcm.teeraw { type empty
slave.pcm "tee:default,'/tmp/out.raw',raw" }

use FLASH_ALSA_DEVICE=teeraw firefox and (e.g.) sox -t raw -r 44100 -c 2 -b 16 -s /tmp/out.raw /tmp/out.wav then proceed with out.wav. (To reduce risk of /tmp/out.raw being overwritten, start sox before interacting with the browser to stop the stream.) This method was last tested in 2017 and is unlikely to work in the latest non-Flash browsers.

Edit decision lists

Use -edl filename, where each line of the file is start-second end-second 0 (use mplayer to find the seconds and to test the EDL). You can add up the total number of minutes you've cut by piping the EDL through python2 -c 'import sys;print sum(float(l.split()[1])-float(l.split()[0]) for l in sys.stdin)/60.0'

Audio file (static or blank picture)

This might be useful for porting audio to old DVD players that don't support MP3 etc. You need to first add the audio to blank video using ffmpeg: (command adapted from Stefano Sabatini's suggestion on ffmpeg-user)
ffmpeg -i input-audio.wav -s 640x480 -f rawvideo -pix_fmt rgb24 -r 25 -i /dev/zero -shortest -vcodec libx264 -preset medium -tune stillimage -crf 24 -acodec copy output.mkv
then proceed with output.mkv (convert it to .mpg as per DVD output below, etc) You can also use ffmpeg to attach audio to a still image:
ffmpeg -i audio.mp3 -loop 1 -i picture.jpg -shortest -vcodec mpeg4 -b:v 800k audio.avi
(then convert audio.avi into the required output format)
Or to replace the soundtrack of an existing video (for language dubbing etc),
ffmpeg -i video.mp4 -i sound.wav -map 0:v -map 1:a -vcodec copy output.mkv
then proceed with output.mkv

FM radio

As above, but if recording the audio to MP3 you might want to avoid wasting space encoding any frequencies above 15kHz. This is because FM stereo requires a 4kHz guard band around the 19kHz pilot signal, therefore any audio you get above 15kHz must be the result of equipment noise; therefore the option of a 32kHz sample rate (Nyquist cutoff 16kHz) makes sense, but your ADC circuitry might not do that well so it's probably best to convert in software:
rec --input-buffer=1048576 -t raw -r 44100 -c 2 -b 16 -e signed-integer - | sox -t raw -r 44100 -c 2 -b 16 -e signed-integer - -t raw -r 32000 -c 2 -b 16 -e signed-integer - | lame -h -s 32 -r - -o output.mp3
(older versions of sox might need -2 -s or -w -s instead of -b 16 -e signed-integer in three places of the above command)

Two language versions

If a video is available in two versions with different-language sound tracks:

To play the two videos on separate devices but time-synchronised over a local network:

(press v to toggle subtitles or f to toggle full-screen; I've obfuscated the options that use technical terms which some viewers might misunderstand as a racist insult)

To play the two videos on the same machine, one with sound only and sent to HDMI, the other with full-screen picture, and sound sent to Bluetooth, set the system to use PulseAudio (not PipeWire: Raspberry Pi OS 12 needs sudo raspi-config / Advanced / Audio to set it back to PulseAudio) and do:
mplayer -noconsolecontrols -vo null -udp-$'\x73\x6c\x61\x76e' -udp-seek-threshold 0.1 -ao pulse::alsa_output.platform-(value from pacmd list-sinks).hdmi.hdmi-stereo video2.mp4 & mplayer -fs -nosub -udp-$'m\!x61s\x74\x65r' -ao pulse::bluez_sink.(MAC address).a2dp_sink video1.mp4
(also works with URLs instead of saved video files, in which case I suggest adding -cache-min 1 -cache 1048576 to each command if you have 4GB+ of RAM)

To have the two languages in two stereo channels on the same player, first create a WAV file:

Then, to keep the picture from the first file, do:
ffmpeg -i input1.mp4 -i audio.wav -map 0:v -map 1:a -vcodec copy output.mkv
and proceed with output.mkv (if it lacks sound, it's probably an ffmpeg version issue: try -b:v 10000k output.mp4 instead of -vcodec copy output.mkv).

Or if the pictures differ as well (for example on-screen text has been translated) then you might wish to show both pictures at once. The following command will do that, positioning them diagonally to reduce peripheral-motion annoyance when trying to watch only one; it works only if both videos are the same size.

ffmpeg -i input1.mp4 -i input2.mp4 -i audio.wav -filter_complex '[0:v]pad=iw*2:ih*2:0:ih[tmp]; [tmp][1:v]overlay=W/2:0[pic]' -map '[pic]' -map 2:a -b:v 3000k output.mp4

If the videos are additionally of different sizes, you'll need a longer version of this command:

W1=$(ffprobe -show_entries stream=width input1.mp4 |grep ^width=|head -1|cut -d= -f2); W2=$(ffprobe -show_entries stream=width input2.mp4 |grep ^width=|head -1|cut -d= -f2); H1=$(ffprobe -show_entries stream=height input1.mp4 |grep ^height=|head -1|cut -d= -f2); H2=$(ffprobe -show_entries stream=height input2.mp4 |grep ^height=|head -1|cut -d= -f2); ffmpeg -i input1.mp4 -i input2.mp4 -i $Audio -filter_complex "[0:v]pad=$[$W1+$W2]:$[$H1+$H2]:0:$H2[tmp]; [tmp][1:v]overlay=$W1:0[pic]" -map '[pic]' -map 2:a -b:v 3000k output.mp4

Recording the desktop

Linux/X11 region around mouse, audio from microphone:
ffmpeg -f alsa -i pulse -f x11grab -follow_mouse 100 -show_region 1 -r 25 -i "$DISPLAY" output.mkv DOSBox (audio from programs): in dosbox.conf (or ~/Library/Preferences/DOSBox* on Mac) set the captures= directory to something (e.g. /tmp) and the default start/stop keybinding Ctrl-Alt-F5 (or Ctrl-F6 for audio only). Beware at least some versions of DOSBox can partially lose videos in some circumstances.

Mac OS X: "QuickTime Player" despite its name is not just a player. Its File menu contains options to record the screen with optional microphone narration, or to record from camera. Results are placed in ~/Movies and I think version 10.9+ of OS X also lets you record part of the screen (otherwise you have to post-process with mencoder's -vf crop=w:h:x:y if you don't want the whole screen).

Series of pictures

You could just do (for example) mencoder 'mf://*.jpg' -mf fps=0.2 but it's sometimes possible for mencoder to crash on signal 11 as it tries to scale pictures, so it might be worth pre-scaling, for example for PAL DVD: for N in *.jpg ; do jpegtopnm "$N" | pnmscale -xysize 720 576 | pnmpad -black -width 720 -height 576 | pnmtopng > "$(echo "$N"|sed -e s/jpg$/png/)";done (followed by the mencoder command on *.png, or on @listfile to ensure it processes them in the correct order)

Output options

Android, iPhone or Mac

-of lavf -lavfopts format=mp4 -vf dsize=480:352:2,scale=0:0,harddup -ovc x264 -sws 9 -x264encopts bitrate=512:bframes=0:chroma_me:me=umh:frameref=6:global_header:level_idc=30:nocabac:partitions=all:subq=5:threads=auto:trellis=1 -oac faac -faacopts mpeg=4:object=2:raw:br=128 -o output.mp4

Nokia S60, S40 etc

-ofps 24 -of lavf -lavfopts format=mp4 -vf dsize=320:240:2,scale=0:0 -oac lavc -ovc lavc -lavcopts aglobal=1:vglobal=1:acodec=libfaac:abitrate=96:vcodec=mpeg4 -o output.mp4

Windows 10 or Mac with large screen

Playable on recent versions of Windows Media Player (Windows 10 should work), and also playable on Mac QuickTime:
-of lavf -lavfopts format=mp4 -ovc x264 -sws 9 -x264encopts bframes=0:chroma_me:me=umh:frameref=6:global_header:level_idc=30:nocabac:partitions=all:subq=5:threads=auto:trellis=1 -oac faac -faacopts mpeg=4:object=2:raw:br=128 -o output.mp4

Zoom Cloud Meetings

Zoom added a built-in video-share function to version 5.4.3 but only on Windows and Mac (use Share Screen / Advanced / Video and choose a file), but on GNU/Linux the method below is still needed (and might also work for other video-conferencing software that has "screen sharing" but no option to feed a video to it directly). If your PulseAudio distorts the video's sound on the remote side due to bad resampling, you can correct this in .config/pulse/daemon.conf---Charles Z Henry suggests:
resample-method = speex-fixed-7
default-sample-rate = 48000
alternate-sample-rate = 44100

---this might need a restart of your desktop session.

"Share computer audio" can silently fail on some Fedora 34 setups, reportedly because Fedora changed from PulseAudio to PipeWire and PipeWire's PulseAudio plugin is not supported as well as real PulseAudio (it seems it works on some machines but not others). It may be necessary to unmute, set "Suppress background noise" as low as possible, be quiet, hope the fan doesn't spin up, and accept degraded audio going from the speaker to the microphone.

To show static pictures (e.g. slides) : VLC can take multiple images on the command line (and can set "minimal interface" on the menus + not full-screen); by default each image is played for 10 seconds, but Space pauses, then on each slide advance, press n and Space to advance to the next slide and re-pause.

Patching one Zoom meeting into another Zoom meeting
The Mac version of Zoom lets you run a second instance if you launch it via a /j/ URL, and this second instance can be set to share the screen of the first instance of Zoom (tested on Zoom 5.9.1 on macOS 12.2.1). This might be useful if a group wants to watch a presentation and then break off for separate discussion without needing control of the main meeting's breakout-room facility. The "share computer audio" option does not work when sharing the screen of another Zoom instance: you must use speaker to microphone. But if you've installed BlackHole (or SoundFlower on older Macs) you can set one Zoom instance to output sound to a Multi-Output Device (created in the "Audio/MIDI Setup" application) and the other instance to take microphone input from the BlackHole device (you'll then need to change this setting if you want to speak later).

Old Windows or Windows Mobile

MP3 audio

(any picture will be discarded)
-ovc copy -of rawaudio -oac mp3lame -lameopts vbr=2:q=9:aq=0 -o output.mp3
"Play All" bookmarklet
If you are on a (probably RedHat) setup where the only working audio player is the browser, you can drag this link to your bookmarks bar: Play All then press it after loading a file:// directory listing containing sound files. (Alternatively, serve that directory to a mobile device with headphones and arrange for the script to be injected into the directory listing. Or install mocp.)

DVDs and VCDs

(based on mencoder documentation with some changes) See also spaced-out audio tracks with cdrdao
All material © Silas S. Brown unless otherwise stated.