11 October, 2015

FFMpeg is Digital Meth -- Part 1


FFMpeg Options

Global Options






AVOptions

These options are specific to the given container, device or codec.  The '-profile' option above is an example of such an option.  These options are provided directly to the libavformat, libavdevice, and libavcodec libraries.

Main Options

'-fmt (input/output)'

FFMpeg normally auto detects the file format for input or output files, guessed from the file extension so often it is not required to specify the format.  The format can be explicitly specified by using '-fmt', for input or output files.

'-y (global)'

If the output file(s) already exist, FFMpeg will halt and present the user with the option to overwrite the file.  Specifying '-y' overwrites output files without user intervention.

'-t duration (input/output)'

This argument applies both to input and output files.  If specifying a duration as an input option (before -i), the result is limiting the duration of the data read from the input file.  When specified as an output option (before the output filename) the FFMpeg will stop writing after its reached the specified duration.

Specifying '-t 20' as an input file, with two output files will result in decoding and re-encoding the 1st 20 seconds of video creating two equivalent output files:

$ ffmpeg -y -t 20 -i big_buck_bunny_1080p_h264.mov -an /tmp/foo1.mov -an /tmp/foo2.mov

Repeating the command while specifying '-t 10' as an output argument to one of the files will result in one input file being 20 sec duration, the other a 10 second duration.

$ ffmpeg -y -t 20 -i big_buck_bunny_1080p_h264.mov -an /tmp/foo1.mov -an -t 10 /tmp/foo2.mov

'-fs fileSize (input/output)'

Another useful output option allows setting the file size limit.  The input file is read, writing the output file until the file size limit specified has been reached.  The result in the following case will be a video consisting of the first ~22 seconds of the input video.


$ ffmpeg -y -i big_buck_bunny_1080p_h264.mov -an -fs 10M /tmp/foo-10M.mov

$ ls -lh /tmp/foo-10M.mov

-rw-rw-r-- 1 user user 9.8M Sep  5 17:45 /tmp/foo-10M.mov

But what if you want to fast forward to 30 seconds into the input file followed by encoding the following 10 seconds? The '-ss' fits the bill when specified as an input file option.  When specified as an output file option, the end result is similar, accomplished by decoding but discarding the input until the timestamps position is reached.


$ ffmpeg -y -ss 30 -i big_buck_bunny_1080p_h264.mov -t 10 -an /tmp/foo-30-40.mov

Setting the timestamp

Python Progress Bar

Was recently writing some python to process some test cases.  One long'ish testcase would benefit from a progress bar, slapped one together below.

One thing to note, progress doesn't allow going backwards, but indicates in ASCII text the current progress by writing characters and a series of backspaces.



#!/usr/bin/python
import time
import sys
 
class ProgressBar():
  barLen_=0;
  progress_=0;
  def __init__(self,N=10):
    self.barLen_=N;
    print '['+' '*self.barLen_+']',
    print '\b'*(self.barLen_+2),
  def setProgress(self,x):
    sys.stdout.flush();
    k=int(x/100.0*self.barLen_);
    if (k > self.progress_):
      update='\b'+'.'*(k-self.progress_);
      print update,
      self.progress_=k;

N=50
p=ProgressBar(N);

for i in range(0,101):
  p.setProgress(i);
  time.sleep(.05);

The result is of the form:

user@river:~$ ./pBar 
[..............                                  ] 
Enjoy.