如果安装好了 Homebrew 那就好说了(Homebrew 介绍和使用),直接:
brew install ffmpeg
可以简单玩一下,将一个为 MPEG-4 的视频转化为 H264:
➜ Downloads ffmpeg -i 测试无法显示画面.mp4 -vcodec h264 trans.mp4
ffmpeg version 4.1.4 Copyright (c) 2000-2019 the FFmpeg developers
built with Apple LLVM version 10.0.1 (clang-1001.0.46.4)
configuration: --prefix=/usr/local/Cellar/ffmpeg/4.1.4 --enable-shared --enable-pthreads --enable-version3 --enable-avresample --cc=clang --host-cflags='-I/Library/Java/JavaVirtualMachines/adoptopenjdk-12.0.1.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/adoptopenjdk-12.0.1.jdk/Contents/Home/include/darwin' --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libbluray --enable-libmp3lame --enable-libopus --enable-librubberband --enable-libsnappy --enable-libtesseract --enable-libtheora --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librtmp --enable-libspeex --enable-videotoolbox --disable-libjack --disable-indev=jack --enable-libaom --enable-libsoxr
libavutil 56. 22.100 / 56. 22.100
libavcodec 58. 35.100 / 58. 35.100
libavformat 58. 20.100 / 58. 20.100
libavdevice 58. 5.100 / 58. 5.100
libavfilter 7. 40.101 / 7. 40.101
libavresample 4. 0. 0 / 4. 0. 0
libswscale 5. 3.100 / 5. 3.100
libswresample 3. 3.100 / 3. 3.100
libpostproc 55. 3.100 / 55. 3.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '测试无法显示画面.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2mp41
encoder : Lavf58.12.100
Duration: 00:04:42.73, start: 0.000000, bitrate: 422 kb/s
Stream #0:0(und): Video: mpeg4 (Simple Profile) (mp4v / 0x7634706D), yuv420p, 640x360 [SAR 1:1 DAR 16:9], 303 kb/s, 10 fps, 10 tbr, 10240 tbn, 10 tbc (default)
Metadata:
handler_name : VideoHandler
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 115 kb/s (default)
Metadata:
handler_name : SoundHandler
Stream mapping:
Stream #0:0 -> #0:0 (mpeg4 (native) -> h264 (libx264))
Stream #0:1 -> #0:1 (aac (native) -> aac (native))
Press [q] to stop, [?] for help
[libx264 @ 0x7f9e8480c800] using SAR=1/1
[libx264 @ 0x7f9e8480c800] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
[libx264 @ 0x7f9e8480c800] profile High, level 2.2
[libx264 @ 0x7f9e8480c800] 264 - core 155 r2917 0a84d98 - H.264/MPEG-4 AVC codec - Copyleft 2003-2018 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=11 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=3 b_pyramid=2 b_adapt=1 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=250 keyint_min=10 scenecut=40 intra_refresh=0 rc_lookahead=40 rc=crf mbtree=1 crf=23.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
Output #0, mp4, to 'trans.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2mp41
encoder : Lavf58.20.100
Stream #0:0(und): Video: h264 (libx264) (avc1 / 0x31637661), yuv420p, 640x360 [SAR 1:1 DAR 16:9], q=-1--1, 10 fps, 10240 tbn, 10 tbc (default)
Metadata:
handler_name : VideoHandler
encoder : Lavc58.35.100 libx264
Side data:
cpb: bitrate max/min/avg: 0/0/0 buffer size: 0 vbv_delay: -1
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
Metadata:
handler_name : SoundHandler
encoder : Lavc58.35.100 aac
frame= 2827 fps=338 q=-1.0 Lsize= 4723kB time=00:04:42.70 bitrate= 136.8kbits/s speed=33.8x
video:575kB audio:4000kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 3.215851%
[libx264 @ 0x7f9e8480c800] frame I:15 Avg QP: 8.85 size: 25246
[libx264 @ 0x7f9e8480c800] frame P:712 Avg QP:20.38 size: 128
[libx264 @ 0x7f9e8480c800] frame B:2100 Avg QP:26.87 size: 57
[libx264 @ 0x7f9e8480c800] consecutive B-frames: 0.8% 0.3% 0.7% 98.2%
[libx264 @ 0x7f9e8480c800] mb I I16..4: 54.3% 19.3% 26.4%
[libx264 @ 0x7f9e8480c800] mb P I16..4: 0.1% 0.1% 0.0% P16..4: 0.9% 0.1% 0.1% 0.0% 0.0% skip:98.8%
[libx264 @ 0x7f9e8480c800] mb B I16..4: 0.1% 0.4% 0.0% B16..8: 1.4% 0.1% 0.0% direct: 0.0% skip:98.0% L0:59.9% L1:39.9% BI: 0.2%
[libx264 @ 0x7f9e8480c800] 8x8 transform intra:44.1% inter:40.1%
[libx264 @ 0x7f9e8480c800] coded y,uvDC,uvAC intra: 13.9% 14.0% 11.3% inter: 0.1% 0.1% 0.0%
[libx264 @ 0x7f9e8480c800] i16 v,h,dc,p: 69% 27% 4% 0%
[libx264 @ 0x7f9e8480c800] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 20% 10% 69% 0% 0% 0% 0% 0% 0%
[libx264 @ 0x7f9e8480c800] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 27% 32% 18% 3% 4% 3% 5% 3% 6%
[libx264 @ 0x7f9e8480c800] i8c dc,h,v,p: 69% 20% 10% 1%
[libx264 @ 0x7f9e8480c800] Weighted P-Frames: Y:0.0% UV:0.0%
[libx264 @ 0x7f9e8480c800] ref P L0: 77.1% 1.8% 12.4% 8.7%
[libx264 @ 0x7f9e8480c800] ref B L0: 57.9% 40.4% 1.8%
[libx264 @ 0x7f9e8480c800] ref B L1: 95.1% 4.9%
[libx264 @ 0x7f9e8480c800] kb/s:16.66
[aac @ 0x7f9e8480e000] Qavg: 55327.031
转化成功:
Java 代码实现:
private void transfer(String infile,String outfile) {
String videoCommend = "ffmpeg -i " + infile + " -vcodec libx264 -r 29.97 -b 768k -ar 24000 -ab 64k -s 1280x720 "
+ outfile;
try {
Runtime rt = Runtime.getRuntime();
Process proc = rt.exec(videoCommend);
InputStream stderr = proc.getErrorStream();
InputStreamReader isr = new InputStreamReader(stderr);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ( (line = br.readLine()) != null)
System.out.println(line);
int exitVal = proc.waitFor();
System.out.println("Process exitValue: " + exitVal);
} catch (Throwable t) {
t.printStackTrace();
}
}
References:
https://www.cnblogs.com/shuaifing/p/9593808.html