一 ffmpeg編譯安裝
原文網址:https://www.cnblogs.com/miaosha5s/p/7670447.html
# wget http://www.ffmpeg.org/releases/ffmpeg-3.1.tar.gz
# tar -zxvf ffmpeg-3.1.tar.gz
# cd ffmpeg-3.1
# ./configure --prefix=/usr/local/ffmpeg
# make && make install
等待安裝完成...
# vi /etc/profile
在最後PATH添加環境變量:
PATH=$PATH:/usr/local/ffmpeg/bin
export PATH
保存退出
# source /ect/profile 設置生效
# ffmpeg -version 查看版本
注意:
若安裝過程中出現以下錯誤:
yasm/nasm not found or too old. Use –disable-yasm for a crippled build.
If you think configure made a mistake, make sure you are using the latest
version from Git. If the latest version fails, report the problem to the
[email protected] mailing list or IRC #ffmpeg on irc.freenode.net.
Include the log file “config.log” produced by configure as this will help
solve the problem.
如錯誤提示,yasm未安裝或者版本太老了,需要安裝新版的yasm
#
wget http://www.tortall.net/projects/yasm/releases/yasm-1.3.0.tar.gz
#
tar -zxvf yasm-1.3.0.tar.gz
#
cd yasm-1.3.0
#
./configure
#
make && make install
問題:在其他機器安裝時,還遇到了下面的幾個問題,可供參考:
1、使用./configure 時,出現:
If gcc is a cross-compiler, use the --enable-cross-compile option.
Only do this if you know what cross compiling means.
C compiler test failed.
If you think configure made a mistake, make sure you are using the latest
version from Git. If the latest version fails, report the problem to the
[email protected] mailing list or IRC #ffmpeg on irc.freenode.net.
Include the log file "config.log" produced by configure as this will help
solve the problem.
提示是gcc的問題,並提示了可以查看config.log。cat config.log 查看錯誤信息如下【這裏只粘貼最後的錯誤信息】:
END /tmp/ffconf.A6YIvVF3.c
gcc -c -o /tmp/ffconf.kvhyNUCz.o /tmp/ffconf.A6YIvVF3.c
./configure: line 875: gcc: command not found
C compiler test failed.
顯然,是沒有找到gcc命令。。 通過yum 或 apt-get命令安裝即可,之後再進行./configure ,便可以繼續安裝2、運行./configure 會報錯,錯誤提示爲:
configure: error: C++ preprocessor “/lib/cpp” sanity check
check See `config.log’ for more details
解決辦法:出現該情況是由於c++編譯器的相關package沒有安裝,在終端上執行:
$ sudo yum install glibc-headers gcc-c++
windows下的編譯步驟,參考:http://blog.csdn.net/zhouyongku/article/details/44961447
二 、視頻轉碼命令
由於需求的來源是要把h264裸流轉換成mp4,好供前端頁面進行播放(js的視頻播放器支持的格式比較有限。。由於開發時間緊張,所以也沒有足夠的經歷自己開發或找到現成的播放器,所以暫時只能採用編碼的方案)。
2.1 ffmpeg命令:264轉mp4
ffmpeg -i slamtv60.264 -vcodec copy -f mp4 test.mp4
執行中打印的信息如下:
ffmpeg version 3.1 Copyright (c) 2000-2016 the FFmpeg developers
built with gcc 4.8.5 (GCC) 20150623 (Red Hat 4.8.5-11)
configuration: --prefix=/usr/local/ffmpeg
libavutil 55. 27.100 / 55. 27.100
libavcodec 57. 48.101 / 57. 48.101
libavformat 57. 40.101 / 57. 40.101
libavdevice 57. 0.101 / 57. 0.101
libavfilter 6. 46.102 / 6. 46.102
libswscale 4. 1.100 / 4. 1.100
libswresample 2. 1.100 / 2. 1.100
Input #0, h264, from 'slamtv60.264':
Duration: N/A, bitrate: N/A
Stream #0:0: Video: h264 (Main), yuv420p, 384x288, 25.50 fps, 25.50 tbr, 1200k tbn, 51 tbc
File 'test.mp4' already exists. Overwrite ? [y/N] y
[mp4 @ 0x3913260] Using AVStream.codec to pass codec parameters to muxers is deprecated, use AVStream.codecpar instead.
Output #0, mp4, to 'test.mp4':
Metadata:
encoder : Lavf57.40.101
Stream #0:0: Video: h264 ([33][0][0][0] / 0x0021), yuv420p, 384x288, q=2-31, 25.50 fps, 25.50 tbr, 1200k tbn, 1200k tbc
Stream mapping:
Stream #0:0 -> #0:0 (copy)
Press [q] to stop, [?] for help
[mp4 @ 0x3913260] Timestamps are unset in a packet for stream 0. This is deprecated and will stop working in the future. Fix your code to set the timestamps properly
frame= 1479 fps=0.0 q=-1.0 Lsize= 4890kB time=00:00:58.31 bitrate= 687.0kbits/s speed=2.41e+03x
video:4884kB audio:0kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.139057%
轉換速度還是很快的,5M的一個.264文件,轉換成mp4耗時在毫秒級。【不過是在配置較高的機器,x86_64,8核內存20G的服務器】2.2 提取視頻縮略圖
ffmpeg -i 001709270738_1514337636497_3393223176.H264 -y -f image2 -t 0.001 -s 352x240 a.jpg
上述指令,是從H264文件中,提取一個尺寸爲352x240大小的jpg圖片文件,作爲縮略圖。
三 Java代碼調用命令
使用的還是簡單的java中執行shell命令的方法,考慮到都在java中實現的話可以在一個任務中完成操作,不必在使用shell銜接,增加複雜度。
代碼如下:
package schedule;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class FfmpegTranscode {
public static void runCmd(String command) {
try {
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(command);
InputStream stderr = proc.getErrorStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
String line = null;
System.out.println("<LogInfo>");
while ((line = br.readLine()) != null){
System.out.println(line);
}
System.out.println("</LogInfo>");
int exitVal = proc.waitFor();
System.out.println("Process exitValue: " + exitVal);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
if(args.length<2) {
System.out.println("請輸入源文件和輸出文件位置,源文件爲264,輸出mp4! ");
return;
}
String h264Path = args[0];
String mp4Path = args[1];
System.out.println("執行轉碼:"+h264Path +" "+mp4Path);
String videoCommand = "ffmpeg -i "
+h264Path
+" -vcodec copy -f mp4 "
+mp4Path;
System.out.println(videoCommand);
runCmd(videoCommand);
}
}
執行時,需要在有ffmpeg的環境下運行。可以打成jar包後,上傳到上面已安裝ffmpeg的機器,通過命令:
java -jar ffmpegtools.jar ./slamtv60.264 ./slamtv60.mp4 的方式執行(slamtv60.264替換成自己的264文件)
四 、流數據轉換【待續-研究中】
雖然【二】中已經實現了文件的轉換,但更希望可以直接針對流數據進行處理,可以減少存儲文件和讀取的時間;直接把h264的流處理成mp4流後再寫入指定位置。
http://blog.csdn.net/firehood_/article/details/8813587