27 March, 2014

Tcpdump Examples

As much as I like using Wireshark, I'm a sucker for a command interface.  The trouble is properly defining a filter has been difficult to say the least, boring simple post, but below is a list of brief examples and descriptions.

#!/bin/bash

#--grab the first 10 packets and write to file
  tcpdump -c 10 -i wlan0 -w /tmp/data.pcap

#--grab the first 10 packets to/from host and write to file
  tcpdump -c 10 -i wlan0 host 192.168.1.125 -w /tmp/data.pcap

#--grab the first 10 packets from host and write to file
  tcpdump -c 10 -i wlan0 src host 192.168.1.125 -w /tmp/data.pcap

#--grab the first 10 packets to host and write to file
  tcpdump -c 10 -i wlan0 dst host 192.168.1.125 -w /tmp/data.pcap

#--grab the first 10 packets to/from port and write to file
  tcpdump -c 10 -i wlan0 port 80 -w /tmp/data.pcap

#--grab the first 10 packets to/from net and write to file
  tcpdump -c 10 -i wlan0 net 192.168.1 -w /tmp/data.pcap

#--grab the first 10 packets from network and write to file
  tcpdump -c 10 -i wlan0 src net 192.168.1 -w /tmp/data.pcap

#--grab the first 10 packets from network and write to file
  tcpdump -c 10 -i wlan0 dst net 192.168.1 -w /tmp/data.pcap

#--grab the first 10 packets from network and write to file
  tcpdump -c 10 -i wlan0 '(dst host 192.168.1.125) and (port 443)' -w /tmp/data.pcap

08 December, 2013

Useful FFMpeg Filter






FFMpeg offers more than standard encoding/decoding.  It also allows for running a series of filters, both audio and video, that may prove useful in many a situation.

Let's start with an good quality input video;


$ ffmpeg -y -i video.avi -ss 10 -t 40 -qscale 0 -s 360x240 clip01.mpg















Ok, now that we have a video let's look at a few things we may choose to do with it using our newly discovered filters.

Adjusting Video Speed

Suppose you wish to speed the video up, perhaps to expedite review of a surveillance video or perhaps minimizing the exposure to the latest Sandra Bullock film.

We can double the playback speed by specifying a 0.5 playback coefficient;

$ ffmpeg -y -i clip01.mpg -filter:v "setpts=0.5*PTS" clip02.mpg
















You'll notice however from the previous video that while the video element is playing at double speed, the audio hasn't changed.  We can speed up the audio similarly by adding an appropriate audio filter.



$ ffmpeg -y -i clip01.mpg -filter:v "setpts=0.5*PTS" -filter:a "atempo=2.0" clip03.mpg


















Attempting to speed up faster than 2x will present a hurdle with speeding up the audio filter, which specifies a coefficient range of [0.5, 2.0].  You can get around that by chaining filters;

$ ffmpeg -i clip01.mpg -filter:v "setpts=0.25*PTS" -filter:a "atempo=2.0,atempo=2.0" clip04.mpg






Cropping Video

Suppose we wish to crop the inner middle of the video.  Given the video is 360x240, we need to grab 180x120 inset by 90,60.

$ ffmpeg -y -i clip01.mpg -filter:v "crop=180:120:90:60" clip05.mpg






Processing Edge Detection

Perhaps less useful, but certainly interesting, you can perform edge detection via a Canny Edge Detection algorithm;
$ ffmpeg -y -i clip01.mpg -filter:v "edgedetect=low=0.1:high=0.4" clip06.mpg







Flipping the Video

You can flip the video left-right or top-down;
$ ffmpeg -y -i clip01.mpg -filter:v "hflip" clip07.mpg















$ ffmpeg -y -i clip01.mpg -filter:v "vflip" clip08.mpg











Creating Video Using Source Filter

Occasionally, you want to generate a video using a test source; in other words, without an input video to start with.
$ ffmpeg -f lavfi -i rgbtestsrc -t 30 -pix_fmt yuv420p clip09.mpg















$ ffmpeg -f lavfi -i testsrc=duration=20:size=1280x720:rate=30 -vcodec mpeg2video clip10.mpg

29 November, 2013

Extracting Video from DVDs

Time and again I find a need to snag video from a DVD and perform some routine video processing.

FFMpeg works nicely with standard video files, less nicely with DVD structures.  While you can certainly extract video directly from the DVD VOB files (ref: http://en.wikibooks.org/wiki/Inside_DVD-Video/Directory_Structure) it's less than desirable since the locating the specific VOB file that contains the segment of video your interested in can be a bit of a setback.

Enter Mplayer;
Mplayer can readily play DVD contents in a user friendly way.  It can also dump the file to a more usable file if you can persuade it to do so.

You can extract the full main title into a single VOB file can be done by playing the DVD somewhat normally, but adding a few arguments;


$ mplayer dvd:// -dumpstream -dumpfile /tmp/video.vob


user@River:~$ 
The above will extract the main title into a single VOB file.  Examining with FFMpeg and you'll find that the output file has a video stream and a single audio stream;



$ ffprobe -i /tmp/video.vob

ffprobe version 1.1.2 Copyright (c) 2007-2013 the FFmpeg developers

  built on Feb 10 2013 17:42:38 with gcc 4.4.5 (Debian 4.4.5-8)

  configuration: --enable-filter=split

  libavutil      52. 13.100 / 52. 13.100

  libavcodec     54. 86.100 / 54. 86.100

  libavformat    54. 59.106 / 54. 59.106

  libavdevice    54.  3.102 / 54.  3.102

  libavfilter     3. 32.100 /  3. 32.100

  libswscale      2.  1.103 /  2.  1.103

  libswresample   0. 17.102 /  0. 17.102

[mpeg @ 0x2cd4c60] max_analyze_duration 5000000 reached at 5004678

Input #0, mpeg, from '/tmp/video.vob':

  Duration: 01:37:58.02, start: 0.280633, bitrate: 5012 kb/s

    Stream #0:0[0x1e0]: Video: mpeg2video (Main), yuv420p, 720x480 [SAR 32:27 DAR 16:9], 26.50 fps, 59.94 tbr, 90k tbn, 59.94 tbc

    Stream #0:1[0x80]: Audio: ac3, 48000 Hz, 5.1(side), fltp, 448 kb/s


You may choose an alternative container rather than a VOB by specifying an alternative extension, such as an AVI container by specifying output file video.avi.

Now, we have something we can readily work with.

Let's snag a 30 sec clip starting at 90 seconds in:




$ ffmpeg -i /tmp/video.vob -qscale 0 -ss 90 -t 30 /tmp/clip.mpg



If you're only interested in the video, you can extract only the video by specifying;


$ mplayer dvd:// -dumpvideo -dumpfile /tmp/video01.avi


Or, if you're only interested in listening to the Big Bang Theory like an audio tape, extract only the audio;




$ mplayer dvd:// -dumpaudio -dumpfile /tmp/video01.aac




Cheers.

24 October, 2013

Create Counting Video


On a couple of occasions, I've had the need to create a source video for video processing and testing video processing commands.  A common need is to create a counting video by generating a series of frames and stitching them together at a pre-defined frame rate.

Below is a mechanism for doing so;


$ cat createVideo 
#!/bin/bash

ws=/var/tmp/cache
rm -rf $ws
mkdir -p $ws

for i in `seq 300`; do
  N=$(printf %03d $i)
  convert -size 640x480 -background white -fill black -pointsize 120 -gravity center label:$i $ws/frame-$N.jpg
done

ffmpeg -y -r 2/1 -i $ws/frame-%03d.jpg -r 30/1 $ws/video.avi
mplayer $ws/video.avi


Cheers.

18 October, 2013

Formatting Numerics With Bash

A repeated need I've encountered is a need to format a numeric with leading zeros, similar to the common form used in C.  Typically, I take a over-complicated approach of comparing the number to >100 or >10 and pre-pending leading zeros.

After investigating alternative approaches with good 'ole Google, the better approach is shown below;


$ cat /tmp/go 
#!/bin/bash

for i in `seq 100`; do
  N=$(printf %03d $i)
  echo $N
done

Cheers.

11 October, 2013

FFMpeg Cropping Videos

Recently had a need to crop videos using FFMpeg, thought it worth sharing. Starting with the initial video:

$ ffmpeg -i ExSlyoVTX3I.flv -vf crop=160:120:10:10 -sameq /tmp/out.mp4

The result is a cropped video 160x120 with an offset of 10,10 from the upper left-hand corner.


Cheers.

05 October, 2013

Google ProtoBuff + ZeroMq -- Python

In our last post, we combined ZeroMq & Google Protobuf, a combination that enables heterogeneous distributed computing systems.  The last post integrated the two using C++, this post will focus on integrating the two using Python.  In the end, we'll have an example that can communicate with the previous C++ example seamlessly.

Below is a Makefile, used to create and clean up Google Protobuf message files.


$ cat Makefile

msgs:

 ${SH} protoc -I=. --python_out=. Messages.proto



clean:

 ${RM} Messages_pb2.py


Our sender script is below, notice the change is that of the content exchanged, specifically the serialized protobuff message.


$ cat sender
#!/usr/bin/python
import zmq;
import time;
import Messages_pb2
context = zmq.Context();
pub=context.socket(zmq.PUB);
pub.bind("tcp://127.0.0.1:8000");
for i in range(0,10):
  p=Messages_pb2.Person();
  p.id=i;
  p.name="fatslowkid";
  print "iteration",i
  pub.send(p.SerializeToString());
  time.sleep(1);

The receiver;

$ cat receiver
#!/usr/bin/python
import zmq;
import time;
import Messages_pb2
context = zmq.Context();
sub=context.socket(zmq.SUB);
sub.connect("tcp://127.0.0.1:8000");
filter=""
sub.setsockopt(zmq.SUBSCRIBE, filter);
for i in range(0,20):
  print "waiting on msg"
  M=sub.recv();
  p=Messages_pb2.Person();
  p.ParseFromString(M);
  print "received",p
  print "> " + p.name;
  print p.id;

The sender/receiver can be used together, or used with the previous C++ example.

Cheers.