2010年11月26日星期五

How To Encode Video With ffmpeg

Contents

  • 1 Summary
  • 2 Encoding to WMV
  • 3 Translating Bike cam videos for use in Powerdirector
  • 4 Exporting from Cyberlink PowerDirector for Youtube and my web site
  • 5 Re-encoding video from Cyberlink PowerDirector for my web site
  • 6 The Rest of my Notes File
    • 6.1 1(a). TO GENERATE .MPG FILES FOR USE IN KINO FROM NOKIA E90 PHONE-RECORDED VIDEOS, RUN THESE COMMANDS ON A LINUX BOX:
    • 6.2 To generate .mp4 files from a dir full of .mov files, run this command on a Linux box:
    • 6.3 To generate video thumbnails, run this command on a Linux box:
    • 6.4 TO TRANSLATE VIDEOS FROM THE HELMET CAM RUN THIS SCRIPT (replacing filenames obviously):
    • 6.5 TO TRIM VIDEOS, run this command on a linux box:



Summary


ffmpeg is a very powerful tool and there is already lots and lots of documentation on the web about how and when to use it. This document aims to be nothing more than my notes on ffmpeg, showing the commands that have worked for me in the past for encoding video to and from various formats.


Encoding to WMV

ffmpeg -i input.wmv -acodec wmav2 -s 640x360 -vcodec wmv2 -b 500k output.wmv
NOTE: The above command worked on openSUSE 11.2 with ffmpeg SVN-r22648.
The resulting video file looked a little strange in VLC when I tried to play it, but worked fine in Windows Media Player.


Translating Bike cam videos for use in Powerdirector

for i in `ls -1 *.AVI`;
do
  ffmpeg -i $i -vn -acodec pcm_s16le `echo $i | sed 's/\.AVI//g'`.2ch.wav
  sox `echo $i | sed 's/\.AVI//g'`.2ch.wav -c1 `echo $i | sed 's/\.AVI//g'`.1ch.wav
  rm -f `echo $i | sed 's/\.AVI//g'`.2ch.wav
  ffmpeg -i $i -i `echo $i | sed 's/\.AVI//g'`.1ch.wav -acodec libfaac \
  -ab 128k -vcodec libx264 -b 1500k -map 0.0 -map 1.0 `echo $i | sed 's/\.AVI//g'`.tmp.mp4
  rm -f `echo $i | sed 's/\.AVI//g'`.1ch.wav
  MP4Box -add `echo $i | sed 's/\.AVI//g'`.tmp.mp4 `echo $i | sed 's/\.AVI//g'`.mp4
  rm -f `echo $i | sed 's/\.AVI//g'`.tmp.mp4
done

Exporting from Cyberlink PowerDirector for Youtube and my web site

Export as WMV > Windows Media Video 9 HD Standard Quality for Youtube.

Export as Portable MPEG-4 > iPhone/iPod > Best Quality


Re-encoding video from Cyberlink PowerDirector for my web site

Once the video is "Produced" as a Portable MP4 file for iPhone/iPod at Best Quality, run this command on the video:

cd Small/
ffmpeg -i video.orig.mp4 -acodec libfaac -ab 128k -s 640x360 -vcodec libx264 \
-vpre hq -b 672k video.tmp.mp4
MP4Box -add video.tmp.mp4 video.mp4
rm -f video.orig.mp4 video.tmp.mp4

The Rest of my Notes File

TO GENERATE .mp4 FILES DIRECT FROM NOKIA E90 PHONE-RECORDED VIDEOS, RUN THESE COMMANDS ON A LINUX BOX:

ffmpeg -i video.mp4 -vn -acodec pcm_s16le video.wav

ffmpeg -sameq -i video.mp4 -i video.wav -acodec libfaac -ab 128k -s 480x360 \
-vcodec libx264 -b 500k video.new.mp4

MP4Box -add video.new.mp4 video.final.mp4

1(a). TO GENERATE .MPG FILES FOR USE IN KINO FROM NOKIA E90 PHONE-RECORDED VIDEOS, RUN THESE COMMANDS ON A LINUX BOX:

ffmpeg -i video.mp4 -vn -acodec pcm_s16le video.wav

ffmpeg -sameq -i video.mp4 -i video.wav -ab 128k video.mpg


1(b). TO GENERATE .DV FILES FOR USE IN KINO FROM iPHONE-RECORDED VIDEOS, RUN THESE COMMANDS ON A LINUX BOX:

ffmpeg -sameq -i video.mov -target pal-dv video.dv


2. When edited in Kino (on openSUSE 11.1), export as MPEG - "8 - DVD", Deinterlace: None, Aspect Ratio: Autodetect

Once exported from Kino (as a .mpeg file), run this command to transcode:

ffmpeg -i video.mpeg -vn -acodec pcm_s16le video.wav

#ffmpeg -sameq -i video.mpeg -i video.wav -acodec libfaac -ab 128k \
-s 480x360 -vcodec libx264 -b 500k Small/video.tmp.mp4
#ffmpeg -sameq -i video.mpeg -i video.wav -acodec libfaac -ab 128k \
-s 640x480 -vcodec libx264 -b 896k Large/video.tmp.mp4

ffmpeg -i video.mpeg -i video.wav -acodec libfaac -ab 128k \
-s 480x360 -vcodec libx264 -vpre hq -b 500k Small/video.tmp.mp4
ffmpeg -i video.mpeg -i video.wav -acodec libfaac -ab 128k \
-s 640x480 -vcodec libx264 -vpre hq -b 896k Large/video.tmp.mp4

MP4Box -add Small/video.tmp.mp4 Small/video.mp4
MP4Box -add Large/video.tmp.mp4 Large/video.mp4

To generate .mp4 files from a dir full of .mov files, run this command on a Linux box:

# for i in `ls -1 *.mov`; do ffmpeg -sameq -i $i `echo $i | sed 's/.mov//'`.mp4; done
If the above command doesn't work on .avi files, try this one:

# for i in `ls -1 *.avi`; do ffmpeg -sameq -i $i -ar 16000 -r 15 `echo $i | sed 's/.avi//'`.mp4; done
If the quality then looks worse, try this one:

# for i in `ls -1 *.avi`; do ffmpeg -qscale 3 -i $i -ar 16000 -r 15 `echo $i | sed 's/.avi//'`.mp4; done
If it then complains that the audio is 8 bits instead of 16, the output the audio to a WAV and then re-encode:

# for i in `ls -1 *.avi`; do ffmpeg -i $i -acodec pcm_s16le `echo $i | sed 's/.avi//'`.wav; ffmpeg \
-sameq -i `echo $i | sed 's/.avi//'`.wav -i $i -ar 16000 -r 15 `echo $i | sed 's/.avi//'`.mp4; done
If your final videos are in "MPEG-4 Visual" format instead of "AVC", then run this on a dir full of badly-encoded files:

# for i in `ls -1 *.mp4`; do ffmpeg -sameq -i $i -vcodec libx264 `echo $i | sed 's/.mp4//'`.new.mp4; \
rm $i; mv `echo $i | sed 's/.mp4//'`.new.mp4 $i; done

To generate video thumbnails, run this command on a Linux box:

# ffmpeg -i video.mp4 -r 0.2 -f image2 video-%03d.jpg
Or, for a directory full of .mp4 video files:

# for i in `ls -1 *.mp4`; do ffmpeg -i $i -r 0.2 -f image2 `echo $i | sed 's/.mp4//'`-%03d.jpg; done
Once you have chosen your JPG thumbnails and renamed them accordingly, run this to generate the small thumbnails:

# for i in `ls -1 *.jpg`; do convert $i -geometry 150x150 `echo $i | sed 's/.jpg//'`_small.jpg; done

TO TRANSLATE VIDEOS FROM THE HELMET CAM RUN THIS SCRIPT (replacing filenames obviously):

#!/bin/bash

ffmpeg -i avi00001.avi -vn -acodec pcm_s16le avi00001.2ch.wav

sox avi00001.2ch.wav -c1 avi00001.1ch.wav

##########
# 640x480
##########
ffmpeg -y -i AVI00001.AVI -an -pass 1 -s 640x480 -vcodec libx264 -b 896k\
-flags +loop -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 \
-me_method dia -subq 1 -trellis 0 -refs 1 -bf 3 -b_strategy 1 -coder 1 \
-me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 \
-bt 896k -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -threads 0 AVI00001.mp4

ffmpeg -y -i avi00001.avi -i avi00001.1ch.wav -acodec libfaac -ab 128k \
-pass 2 -s 640x480 -vcodec libx264 -b 896k -flags +loop -cmp +chroma \
-partitions +parti8x8+parti4x4+partp8x8+partp4x4+partb8x8 \
-flags2 +dct8x8+wpred+bpyramid+mixed_refs -me_method umh -subq 7 \
-trellis 1 -refs 6 -bf 3 -directpred 3 -b_strategy 1 -coder 1 -me_range 16 \
-g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt 896k -qcomp 0.6 \
-qmin 10 -qmax 51 -qdiff 4 -threads 0 -map 0.0 -map 1.0 avi00001.mp4


##########
# 480x360
##########
ffmpeg -y -i avi00001.avi -an -pass 1 -s 480x360 -vcodec libx264 -b 500k \
-flags +loop -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 \
-me_method dia -subq 1 -trellis 0 -refs 1 -bf 3 -b_strategy 1 -coder 1 \
-me_range 16 -g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 \
-bt 500k -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -threads 0 avi00001.mp4

ffmpeg -y -i avi00001.avi -i avi00001.1ch.wav -acodec libfaac -ab 128k \
-pass 2 -s 480x360 -vcodec libx264 -b 500k -flags +loop -cmp +chroma \
-partitions +parti8x8+parti4x4+partp8x8+partp4x4+partb8x8 \
-flags2 +dct8x8+wpred+bpyramid+mixed_refs -me_method umh -subq 7 \
-trellis 1 -refs 6 -bf 3 -directpred 3 -b_strategy 1 -coder 1 -me_range 16 \
-g 250 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt 500k -qcomp 0.6 \
-qmin 10 -qmax 51 -qdiff 4 -threads 0 -map 0.0 -map 1.0 avi00001.mp4

TO TRIM VIDEOS, run this command on a linux box:

ffmpeg -ss 00:06:40 -t 00:10:23 -i video.mp4 -vcodec copy -acodec copy video.new.mp4

没有评论:

发表评论