FFmpeg3.2安裝(CentOS release 6.5)

yasm安裝
Yasm是一個完全重寫的NASM彙編。目前,它支持x86和AMD64指令集。 
接受NASM和GNU彙編器(gas)語法,產出二進制, ELF32 , ELF64 , COFF , Mach - O的( 32和64 ) , RDOFF2 ,的Win32和Win64對象的格式,並生成STABS 調試信息的來源,DWARF 2 ,CodeView 8格式。

下載源:http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
安裝
wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
tar -zxvf yasm-1.3.0.tar.gz
./configure
make
make install
 

lame安裝
用於mp3編碼。由於專利版權問題,ffmpeg默認不帶mp3格式編碼器,但是有mp3格式解碼器。

下載源:https://sourceforge.net/projects/lame/files/lame/3.99/lame-3.99.5.tar.gz/download
安裝
wget https://sourceforge.net/projects/lame/files/lame/3.99/lame-3.99.5.tar.gz/download
#下載下來的文件名是down重命名爲實際的名字
mv download lame-3.99.5.tar.gz
tar -zxvf lame-3.99.5.tar.gz
cd ./lame-3.99.5
./configure
make
make install
#編譯安裝後並沒有加入系統庫中,添加如下軟連接加入系統庫
ln -s /usr/local/lib/libmp3lame.so.0.0.0 /usr/lib64/libmp3lame.so.0

FFmpeg安裝
FFmpeg是一套可以用來記錄、轉換數字音頻、視頻,並能將其轉化爲流的開源計算機程序。採用LGPL或GPL許可證。它提供了錄製、轉換以及流化音視頻的完整解決方案。

下載源:http://www.ffmpeg.org/releases/ffmpeg-3.2.tar.gz
安裝
wget http://www.ffmpeg.org/releases/ffmpeg-3.2.tar.gz
tar -zxvf ffmpeg-3.2.tar.gz
cd ffmpeg-3.2
#編譯配置,使mp3編碼器生效
./configure --enable-libmp3lame
make
make install

 

[root@localhost ffmpeg-3.2]# ffmpeg
ffmpeg version 3.2 Copyright (c) 2000-2016 the FFmpeg developers
  built with gcc 4.4.7 (GCC) 20120313 (Red Hat 4.4.7-17)
  configuration: --enable-libmp3lame
  libavutil      55. 34.100 / 55. 34.100
  libavcodec     57. 64.100 / 57. 64.100
  libavformat    57. 56.100 / 57. 56.100
  libavdevice    57.  1.100 / 57.  1.100
  libavfilter     6. 65.100 /  6. 65.100
  libswscale      4.  2.100 /  4.  2.100
  libswresample   2.  3.100 /  2.  3.100
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...

Use -h to get full help or, even better, run 'man ffmpeg'

 

[root@localhost ffmpeg-3.2]# ffmpeg -codecs |grep mp3
ffmpeg version 3.2 Copyright (c) 2000-2016 the FFmpeg developers
  built with gcc 4.4.7 (GCC) 20120313 (Red Hat 4.4.7-17)
  configuration: --enable-libmp3lame
  libavutil      55. 34.100 / 55. 34.100
  libavcodec     57. 64.100 / 57. 64.100
  libavformat    57. 56.100 / 57. 56.100
  libavdevice    57.  1.100 / 57.  1.100
  libavfilter     6. 65.100 /  6. 65.100
  libswscale      4.  2.100 /  4.  2.100
  libswresample   2.  3.100 /  2.  3.100
 DEA.L. mp3                  MP3 (MPEG audio layer 3) (decoders: mp3 mp3float ) (encoders: libmp3lame )
 D.A.L. mp3adu               ADU (Application Data Unit) MP3 (MPEG audio layer 3) (decoders: mp3adu mp3adufloat )
 D.A.L. mp3on4               MP3onMP4 (decoders: mp3on4 mp3on4float )

執行以上命令則代表安裝完畢

參考https://blog.csdn.net/clj198606061111/article/details/53054270

 

# FFMPEG 使用說明

## 截圖命令

### 截取一張352x240尺寸大小,格式爲jpg的圖片
```
ffmpeg -i input_file -y -f image2 -t 0.001 -s 352x240 output.jpg
```

### 把視頻的前30幀轉換成一個Animated Gif
```
ffmpeg -i input_file -vframes 30 -y -f gif output.gif
```

### 在視頻的第8.01秒出截取230x240的縮略圖
```
ffmpeg -i input_file -y -f mjpeg -ss 8 -t 0.001 -s 320x240 output.jpg
```

### 每隔一秒截一張圖

```
ffmpeg -i out.mp4 -f image2 -vf fps=fps=1 out%d.png
```

### 每隔20秒截一張圖

```
ffmpeg -i out.mp4 -f image2 -vf fps=fps=1/20 out%d.png
```

### 多張截圖合併到一個文件裏(2x3)每隔一千幀(秒數=1000/fps25)即40s截一張圖

```
ffmpeg -i out.mp4 -frames 3 -vf "select=not(mod(n\,1000)),scale=320:240,tile=2x3" out.png
```

### 從視頻中生成GIF圖片
```
ffmpeg -i out.mp4 -t 10 -pix_fmt rgb24 out.gif
```
### 轉換視頻爲圖片(每幀一張圖)
```
ffmpeg -i out.mp4 out%4d.png
```

### 圖片轉換爲視頻
```
ffmpeg -f image2 -i out%4d.png -r 25 video.mp4
```

## 切分視頻並生成M3U8文件

```
ffmpeg -i input.mp4 -c:v libx264 -c:a aac -strict -2 -f hls -hls_time 20 -hls_list_size 0 -hls_wrap 0 output.m3u8
```

相關參數說明:

```
-i 輸入視頻文件
-c:v 輸出視頻格式
-c:a 輸出音頻格式
-strict
-f hls 輸出視頻爲HTTP Live Stream(M3U8)
-hls_time 設置每片的長度,默認爲2,單位爲秒
-hls_list_size 設置播放列表保存的最多條目,設置爲0會保存所有信息,默認爲5
-hls_wrap 設置多少片之後開始覆蓋,如果設置爲0則不會覆蓋,默認值爲0。這個選項能夠避免在磁盤上存儲過多的片,而且能夠限制寫入磁盤的最多片的數量。
```
注意,播放列表的sequence number對每個segment來說都必須是唯一的,而且它不能和片的文件名(當使用wrap選項時,文件名可能會重複使用)混淆。

## 分離視頻音頻流

```
ffmpeg -i input_file -vcodec copy -an output_file_video    //分離視頻流
ffmpeg -i input_file -acodec copy -vn output_file_audio    //分離音頻流
```

## 視頻解複用

```
ffmpeg -i test.mp4 -vcoder copy -an -f m4v test.264
ffmpeg -i test.avi -vcoder copy -an -f m4v test.264
```

## 視頻轉碼

```
ffmpeg -i test.mp4 -vcoder h264 -s 352*278 -an -f m4v test.264    //轉碼爲碼流原始文件
ffmpeg -i test.mp4 -vcoder h264 -bf 0 -g 25 -s 352-278 -an -f m4v test.264    //轉碼爲碼流原始文件
ffmpeg -i test.avi -vcoder mpeg4 -vtag xvid -qsame test_xvid.avi    //轉碼爲封裝文件 -bf B幀數目控制, -g 關鍵幀間隔控制, -s 分辨率控制
```
## 視頻封裝

```
ffmpeg -i video_file -i audio_file -vcoder copy -acodec copy output_file
```

## 視頻剪切

```
ffmpeg -i test.avi -r 1 -f image2 image.jpeg //視頻截圖
ffmpeg -i input.avi -ss 0:1:30 -t 0:0:20 -vcoder copy -acoder copy output.avi //剪切視頻 -r 提取圖像頻率, -ss 開始時間, -t 持續時間
```

## 視頻錄製

```
ffmpeg -i rtsp://hostname/test -vcoder copy out.avi
```

## YUV序列播放

```
ffplay -f rawvideo -video_size 1920x1080 input.yuv
```

## YUV序列轉AVI

```
ffmpeg -s w*h -pix_fmt yuv420p -i input.yuv -vcoder mpeg4 output.avi
```

### 常用參數說明

#### 主要參數
```
-i 設定輸入流
-f 設定輸出格式
-ss 開始時間
```
#### 視頻參數
```
-b 設定視頻流量,默認是200Kbit/s
-s 設定畫面的寬和高
-aspect 設定畫面的比例
-vn 不處理視頻
-vcoder 設定視頻的編碼器,未設定時則使用與輸入流相同的編解碼器
```
### 音頻參數
```
-ar 設定採樣率
-ac 設定聲音的Channel數
-acodec 設定瀋陽的Channel數
-an 不處理音頻
```

## 使用ffmpeg合併MP4文件

```
ffmpeg -i "Apache Sqoop Tutorial Part 1.mp4" -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate1.ts
ffmpeg -i "Apache Sqoop Tutorial Part 2.mp4" -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate2.ts
ffmpeg -i "Apache Sqoop Tutorial Part 3.mp4" -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate3.ts
ffmpeg -i "Apache Sqoop Tutorial Part 4.mp4" -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate4.ts
ffmpeg -i "concat:intermediate1.ts|intermediate2.ts|intermediate3.ts|intermediate4.ts" -c copy -bsf:a aac_adtstoasc "Apache Sqoop Tutorial.mp4"
```

## 使用ffmpeg轉換flv到mp4

```
ffmpeg -i out.flv -vcodec copy -acodec copy out.mp4
```

## 視頻添加水印

### 水印局中

```
ffmpeg -i out.mp4 -i [email protected] -filter_complex overlay="(main_w/2)-(overlay_w/2):(main_h/2)-(overlay_h)/2" output.mp4
```

參數解釋

* -i out.mp4(視頻源)
* -i [email protected](水印圖片)
* overlay 水印的位置
* output.mp4 輸出文件

## 視頻翻轉和旋轉

### 翻轉

#### 水平翻轉語法: -vf hflip

```
ffplay -i out.mp4 -vf hflip
```

#### 垂直翻轉語法:-vf vflip

```
ffplay -i out.mp4 -vf vflip
```

### 旋轉

語法:transpose={0,1,2,3}

* 0:逆時針旋轉90°然後垂直翻轉

* 1:順時針旋轉90°

* 2:逆時針旋轉90°

* 3:順時針旋轉90°然後水平翻轉

### 將視頻順時針旋轉90度

```
ffplay -i out.mp4 -vf transpose=1
```

### 將視頻水平翻轉(左右翻轉)

```
ffplay -i out.mp4 -vf hflip
```

### 順時針旋轉90度並水平翻轉

```
ffplay -i out.mp4 -vf transpose=1,hflip
```

## 添加字幕

有的時候你需要給視頻加一個字幕(subtitle),使用ffmpeg也可以做。一般我們見到的字幕以srt字幕爲主,在ffmpeg裏需要首先將srt字幕轉化爲ass字幕,然後就可以集成到視頻中了(不是單獨的字幕流,而是直接改寫視頻流)。

```
ffmpeg -i my_subtitle.srt my_subtitle.ass
ffmpeg -i inputfile.mp4 -vf ass=my_subtitle.ass outputfile.mp4
```

但是值得注意的是:

> my_subtitle.srt需要使用UTF8編碼,老外不會注意到這一點,但是中文這是必須要考慮的;

將字幕直接寫入視頻流需要將每個字符渲染到畫面上,因此有一個字體的問題,在ass文件中會指定一個缺省字體,例如Arial,但是我們首先需要讓ffmpeg能找到字體文件,不然文字的渲染就無從談起了。ffmpeg使用了fontconfig來設置字體配置。你需要首先設置一下FONTCONFIG_PATH或者FONTCONFIG_FILE環境變量,不然fontconfig是無法找到配置文件的,這一點請參看這篇文章,如果你設置的是FONTCONFIG_PATH,那把配置文件保存爲%FONTCONFIG_PATH%/font.conf即可,然後你可以在font.conf文件中配置字體文件的路徑之類的。

Windows下爲fontconfig設置如下的環境變量

```
FC_CONFIG_DIR=C:\ffmpeg
FONTCONFIG_FILE=font.conf
FONTCONFIG_PATH=C:\ffmpeg
PATH=C:\ffmpeg\bin;%PATH%
```

下面是一個簡單的Windows版`font.conf`文件。

```xml
<?xml version="1.0"?>
<fontconfig>

<dir>C:\WINDOWS\Fonts</dir>

<match target="pattern">
   <test qual="any" name="family"><string>mono</string></test>
   <edit name="family" mode="assign"><string>monospace</string></edit>
</match>

<match target="pattern">
   <test qual="all" name="family" mode="not_eq"><string>sans-serif</string></test>
   <test qual="all" name="family" mode="not_eq"><string>serif</string></test>
   <test qual="all" name="family" mode="not_eq"><string>monospace</string></test>
   <edit name="family" mode="append_last"><string>sans-serif</string></edit>
</match>

<alias>
   <family>Times</family>
   <prefer><family>Times New Roman</family></prefer>
   <default><family>serif</family></default>
</alias>
<alias>
   <family>Helvetica</family>
   <prefer><family>Arial</family></prefer>
   <default><family>sans</family></default>
</alias>
<alias>
   <family>Courier</family>
   <prefer><family>Courier New</family></prefer>
   <default><family>monospace</family></default>
</alias>
<alias>
   <family>serif</family>
   <prefer><family>Times New Roman</family></prefer>
</alias>
<alias>
   <family>sans</family>
   <prefer><family>Arial</family></prefer>
</alias>
<alias>
   <family>monospace</family>
   <prefer><family>Andale Mono</family></prefer>
</alias>
<match target="pattern">
   <test name="family" mode="eq">
      <string>Courier New</string>
   </test>
   <edit name="family" mode="prepend">
      <string>monospace</string>
   </edit>
</match>
<match target="pattern">
   <test name="family" mode="eq">
      <string>Courier</string>
   </test>
   <edit name="family" mode="prepend">
      <string>monospace</string>
   </edit>
</match>

</fontconfig>
```

下面這個是Linux系統下改版過來的

```xml
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<!-- /etc/fonts/fonts.conf file to configure system font access -->
<fontconfig>
<!-- 
    Find fonts in these directories
-->
<dir>C:/Windows/Fonts</dir>
<!--
<dir>/usr/X11R6/lib/X11/fonts</dir>
-->
<!--
    Accept deprecated 'mono' alias, replacing it with 'monospace'
-->
<match target="pattern">
    <test qual="any" name="family"><string>mono</string></test>
    <edit name="family" mode="assign"><string>monospace</string></edit>
</match>

<!--
    Load per-user customization file, but don't complain
    if it doesn't exist
-->
<include ignore_missing="yes" prefix="xdg">fontconfig/fonts.conf</include>

<!--
    Load local customization files, but don't complain
    if there aren't any
-->
<include ignore_missing="yes">conf.d</include>
<include ignore_missing="yes">local.conf</include>

<!--
    Alias well known font names to available TrueType fonts.
    These substitute TrueType faces for similar Type1
    faces to improve screen appearance.
-->
<alias>
    <family>Times</family>
    <prefer><family>Times New Roman</family></prefer>
    <default><family>serif</family></default>
</alias>
<alias>
    <family>Helvetica</family>
    <prefer><family>Arial</family></prefer>
    <default><family>sans</family></default>
</alias>
<alias>
    <family>Courier</family>
    <prefer><family>Courier New</family></prefer>
    <default><family>monospace</family></default>
</alias>

<!--
    Provide required aliases for standard names
    Do these after the users configuration file so that
    any aliases there are used preferentially
-->
<alias>
    <family>serif</family>
    <prefer><family>Times New Roman</family></prefer>
</alias>
<alias>
    <family>sans</family>
    <prefer><family>Arial</family></prefer>
</alias>
<alias>
    <family>monospace</family>
    <prefer><family>Andale Mono</family></prefer>
</alias>
</fontconfig>
```

參考:

> http://blog.raphaelzhang.com/2013/04/video-streaming-and-ffmpeg-transcoding/

## 嵌入字幕

在一個MP4文件裏面添加字幕,不是把 .srt 字幕文件集成到 MP4 文件裏,而是在播放器裏選擇字幕,這種集成字幕比較簡單,速度也相當快

```
ffmpeg -i input.mp4 -i subtitles.srt -c:s mov_text -c:v copy -c:a copy output.mp4
```

希望字幕直接顯示出來,其實也不難

```
ffmpeg -i subtitle.srt subtitle.ass
ffmpeg -i input.mp4 -vf ass=subtitle.ass output.mp4
```
參考:

> http://blog.neten.de/posts/2013/10/06/use-ffmpeg-to-burn-subtitles-into-the-video/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章