Raspberry Pi_Eng_22.1.5 Converting Video Format Using FFMPEG Program


Published Book on Amazon


All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 1
All of IOT Starting with the Latest Raspberry Pi from Beginner to Advanced – Volume 2


출판된 한글판 도서


최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (상)
최신 라즈베리파이(Raspberry Pi)로 시작하는 사물인터넷(IOT)의 모든 것 – 초보에서 고급까지 (하)


Original Book Contents


22.1.5  Converting Video Format Using <FFMPEG> Program

 

Video taken by Raspberry Pi camera use "H.264" format, which is a format that is not widely used by ordinary users. You can use <FFMPEG> program to convert it to a popular video format.

 

   Installing <FFMPEG> program

 

To use <FFMPEG> program, you need to install it as follows:

 

[Command Format]

sudo   apt-get   install   ffmpeg

 

If you execute the command, it checks installation of the program and confirms whether or not to install the program. If you choose "Continue", the installation process begins as shown below.

 

pi@raspberrypi ~ $ sudo apt-get install ffmpeg

Reading package lists... Done

Building dependency tree

Reading state information... Done

~

~ Skip

~

0 upgraded, 8 newly installed, 0 to remove and 39 not upgraded.

Need to get 5,174 kB of archives.

After this operation, 13.4 MB of additional disk space will be used.

Do you want to continue [Y/n]? y

~

~ Skip

~

Setting up libavfilter2:armhf (6:0.8.17-1+rpi1) ...

Setting up ffmpeg (6:0.8.17-1+rpi1) ...

W: Operation was interrupted before it could finish

 

 

   Converting video format

 

To convert a video using <FFMPEG> program, use the command in the format as follows.

 

[Command Format]

ffmpeg   [[in-file option]  [-I  in-file]     [out-file options] out-file

 

[Command Overview]         

    This converts the specified video to a file of a different format.

    User privilege           -- Normal user.

 

[Detail Description]

    None

 

[Main option]

-h, -?, -help, --help

Show help.

-formats

Show available formats. The fields preceding the format names have the following meanings:

D   Decoding available

E   Encoding available

-codecs

Show available codecs. The fields preceding the codec names have the following meanings:

D   Decoding available

E   Encoding available

V/A/S   Video/audio/subtitle codec

-i filename

input file name

-vframes number

Set the number of video frames to record.

-r fps

Set frame rate (Hz value, fraction or abbreviation), (default = 25).

-s size

Set frame size. The format is wxh (avserver default = 160x128, ffmpeg default = same as source). The following abbreviations are recognized:

-vcodec codec

Force video codec to codec. Use the "copy" special value to tell that the raw codec data must be copied as is.

 

[Used Example]

In this example, we will convert video of "H.264" format taken previously in Raspberry Pi to MKV format.

 

pi@raspberrypi ~/Image_Camera $ ffmpeg -r 30 -i test_motion1.h264 -vcodec copy test_motion1.mkv

ffmpeg version 0.8.17-6:0.8.17-1+rpi1, Copyright (c) 2000-2014 the Libav developers

  built on Mar 25 2015 00:39:58 with gcc 4.6.3

The ffmpeg program is only provided for script compatibility and will be removed

in a future release. It has been deprecated in the Libav project to allow for

incompatible Shell syntax improvements in its replacement called avconv

(see Changelog for details). Please use avconv instead.

[h264 @ 0x480660] Estimating duration from bitrate, this may be inaccurate

 

Seems stream 0 codec frame rate differs from container frame rate: 2400000.00 (2400000/1) -> 30.00 (30/1)

Input #0, h264, from 'test_motion.h264':

  Duration: N/A, bitrate: N/A

    Stream #0.0: Video: h264 (High), yuv420p, 1920x1080, 30 fps, 30 tbr, 1200k tbn, 2400k tbc

Output #0, matroska, to 'test_modion.mkv':

  Metadata:

    encoder         : Lavf53.21.1

    Stream #0.0: Video: libx264, yuv420p, 1920x1080, q=2-31, 1k tbn, 1200k tbc

Stream mapping:

  Stream #0.0 -> #0.0

Press ctrl-c to stop encoding

frame=  142 fps=  0 q=-1.0 Lsize=    9844kB time=10000000000.00 bitrate=   0.0kbits/s

video:9842kB audio:0kB global headers:0kB muxing overhead 0.017483%

 

If you check after completing the operation, you can see that MKV file is newly created as below.

 

pi@raspberrypi ~/Image_Camera $ ls -l

-rw-r--r-- 1 pi pi 10080058 May 13 16:11 test_motion1.mkv

-rw-r--r-- 1 pi pi 10078296 May 13 14:51 test_motion1.h264

 

 

Now, if you play the vedo file of MKV format, you can view the contents as follows.


Figure 22‑5 Playing video using MKV file