Various script snippets for working with video files in Linux. Things may break, look at the tool manual pages before using -- that's what I have to do every time.

Fix video orientation metadata for GoPro etc videos

Nowadays video metadata can include orientation details. Some video playback and editing tools respect this metadata. Some can't fix the orientation without affecting video quality like reducing resoltion (kdenlive).

But the metadata can be fixed on Linux using exiftool. First install exiftool on Debian or Ubuntu:

$ sudo apt install libimage-exiftool-perl

Then check video orientation. In this example correct rotation was 180 degrees but camera for some reason set it to 270 degrees:

$ exiftool  GH018219.MP4 | grep -i rotation
Auto Rotation                   : Unknown (L)
Rotation                        : 270

Fix by changing the rotation to correct 180 degrees:

$ exiftool -rotation=180 GH018219.MP4
$ exiftool  GH018219_reilit_sukset_alakulma.MP4 | grep -i rotation
Auto Rotation                   : Unknown (L)
Rotation                        : 180

Now rotation is correct and video playback and editing tools which respect rotation metadata do the right thing. exiftool does save the original file too incase something went wrong.

mp4 compatible with flashplayer and streaming in general

Move some index from the end to the front of the .mp4/.mov container:

$ qt-faststart infile.mp4 outfile.mp4

Video stabilization

Works out of the box with latest transcode from Debian multimedia repo for unstable/sid. White scenery like snow has some trouble but at least the tools work.

Basic usage, calculate data and render:

$ transcode -J stabilize -i P1060621.MOV
$ transcode -J transform -i P1060621.MOV -y xvid -o P1060621_stable.MOV

For really shaky camera, e.g. out of breath skier filming other skiers:

$ transcode -J stabilize=shakiness=8:fieldsize=60:mincontrast=0.04 -i P1060621.MOV
$ transcode -J transform -i P1060621.MOV -y xvid -o P1060621_stable.MOV

Calculate data with preview:

$ transcode -J stabilize=shakiness=8:show=1,preview -i P1060621.MOV

Links:

http://isenmann.wordpress.com/2011/03/22/deshaking-videos-with-linux/

http://public.hronopik.de/vid.stab/features.php?lang=en

http://314bits.com/blog/2010/09/stabilize-video-in-ubuntu-linux/

Still photos to DVD editing in kino

$ for i in *.JPG *.jpg; do montage -geometry 720x576 -background black -quality 100 $i dvd_$i; done

Slow 720p60 video to 720p30, no audio track yet

$ for f in *_*MP4 ; do ( ffmpeg -i "$f" -f yuv4mpegpipe -pix_fmt yuv420p -y /dev/stdout | \
yuvcorrect -T INTERLACED_TOP_FIRST 2>/dev/null |  yuvfps -s 30000:1001 -r 30000:1001 | \
yuvcorrect -T PROGRESSIVE 2>/dev/null | mpeg2enc --no-constraints -f3 -nn -a3 -Ktmpgenc \
-lh -b24000 -V488 -r32 -G18 -q9 -s -o "$f".slow.m2v) || break; done

To fix the m2v playback:

$ ffmpeg -i yourclip.m2v -acodec copy -vcodec copy test.avi

http://www.kdenlive.org/mantis/view.php?id=2424

Source: http://renomath.org/video/linux/slowmot/

Flip video 90 degrees

$ mencoder -oac copy -ovc lavc -lavcopts vcodec=mjpeg -vf rotate=1 foo.AVI -o testi.avi

Convert interlaced 50 fps AVCHD videos to DNxHD format for kdenlive

$ for f in $( find . -iname "*_*.mts" -or -iname "*_*.m2ts" ); do \
if [ ! -e "$f.dnxhd.mov" ]; then echo "$f"; \
ffmpeg -i "$f" -vcodec dnxhd -b 120000k -deinterlace -acodec \
copy -r 25 "$f.dnxhd.mov" ; \
fi; \
done

Old and buggy de-AVCHD script

$ for f in *.mts ; do ( echo $f; \
ffmpeg -i $f -s hd720 -acodec pcm_s16le -ac 2 \
-vcodec mpeg4 -sameq -deinterlace \
avi/$(basename $f .mts).mov ); done

Older and even buggier de-AVCHD script

$ mencoder -noskip -mc 0 -oac lavc -demuxer lavf \
-vf pullup,softskip -ovc lavc -lavcopts \
acodec=libfaac:abitrate=256:vcodec=mpeg4:vqscale=2:aspect=16/9 \
-of lavf -lavfopts format=mov -pp vf=fd \
000001.mts -o testi.avi

Convert video clips to a format suitable for kino editing, works at least with ffmpeg from Debian sarge

for f in *.avi; do echo $f; ffmpeg -i $f -target pal-dv \
$( basename $f .avi).dv; done

Videos to flash

$ ffmpeg -i foo.dv -r 25 -ar 22050 -ac 1 -f flv -s 320x262 foo.flv

Convert videos for viewers, high quality

$  ffmpeg -i raaka.dv -b 300k -vtag XVID -acodec libmp3lame -ac 2 -ab 64000 -s cif -mbd rd -flags +4mv+trell+aic+ilme+ildct -cmp 2 -subcmp 2 -g 300 -vcodec mpeg4 -deinterlace -pass 1 testi_xvid.avi

$ ffmpeg -i lyngen_2005.mpeg -r 25 -b 300 -vtag XVID -acodec mp3 -deinterlace -ar 32000 -ac 1 lyngen_2005.avi

$ ffmpeg -i testi.avi -r 20 -b 150 -vtag XVID -acodec mp3 -ac 1 bar.avi

or like this if multiple source files, maybe also deinterlace?

$ cat foo1.dv foo2.dv foo3.dv | ffmpeg -f dv -i - -r 20 -b 800 -vtag XVID -acodec mp3 -ac 1 bar.avi

Low quality videos for viewers

$ ffmpeg -i f-koulu_07012007.mpeg -r 15 -b 80k -vtag XVID -acodec mp3 -ac 1 -ab 32 -s qcif -deinterlace bar4.avi

Rip DVD for Nokia N810, DOES NOT WORK!

$ mencoder dvd://2 -oac mp3lame -lameopts abr:br=64 -af volnorm \
-ovc lavc -lavcopts vcodec=mpeg4:aspect=16/9:vbitrate=400 \
-vf scale=480:360 -idx -ffourcc DIVX -ofps 13 -alang fi,en -o test.avi

Working DVD rip for Nokia N810

$ mencoder dvd://0 -oac mp3lame -lameopts abr:br=64 -ovc lavc \
-lavcopts vcodec=mpeg4:mbd=1:vbitrate=300 -vf scale=352:208 \
-ffourcc DIVX -ofps 15 -o testi2.avi

DVD rip 2014

Track 18, chapter 2, into tiny format.

$ HandBrakeCLI -i /dev/dvd -t 18 -O -r 15 -d -b 600 -B 64 \
-6 mono -E lame -w 352 -l 208 -o test2.avi