# video-ffmpeg **Repository Path**: nuoee/video-ffmpeg ## Basic Information - **Project Name**: video-ffmpeg - **Description**: 视频转换以及加速转码支持视频和音频 - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2025-03-25 - **Last Updated**: 2025-04-17 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # JAVA 使用FFMPEG硬件加速转码 ## 打包 ```shell mvn clean package -Dmaven.test.skip=true ``` ## 引入 ```xml com.moses.video video-ffmpeg 1.0.0 ``` ## 支持视频和音频格式 ## ffmpeg支持的硬件以及硬件加速方式 ##### 通过命令 ffmpeg -hwaccels 可以查询系统支持的硬件加速器。 ```shell ffmpeg -hwaccels ---------------------------------------------- cuda dxva2 qsv d3d11va opencl vulkan ``` > PS:以上输出代表系统所支持的硬件加速方式,后续会用到 ## 硬编码后缀解释 qsv:intel显卡的快速视频同步技术(quick sync video) nvenc:nvidia显卡的硬件视频编码器(nvidia hardware video encoder) cuvid:nvdec的旧称,只有解码端。 cuda: 同上 amf:amd显卡的amf硬件编码器(amd hardware encoder) >基本上所有的Intel 电脑都支持qsv方式。 ## 编解码器支持 ```shell ffmpeg -codecs ``` 查询编码器 ```shell ffmpeg -codecs ``` 后面可以跟 sls命令查询所需的编码,例如: ffmpeg -codecs | sls nvenc ```shell ffmpeg -codecs | sls nvenc --------------------------------------------------------------- DEV.LS h264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (decoders: h264 h264_qsv h264_cuvid ) (encoders: libx264 libx264 libx264rgb h264_amf h264_nvenc h264_qsv ) DEV.L. hevc H.265 / HEVC (High Efficiency Video Coding) (decoders: hevc hevc_qsv hevc_cuvid ) (encoder s: libx265 hevc_amf hevc_nvenc hevc_qsv ) ``` 查询系统支持的编解码格式,需要用到 ffmpeg -decodersffmpeg -encoders 两个命令。 查询编解码器选项 查询编码器hevc_nvenc的选项 ```shell ffmpeg -h encoder=hevc_nvenc ``` 查询解码器 h264_qsv 的详细信息 ```shell ffmpeg -h decoder=h264_qsv ``` ## ffmpeg -decoders 下面以h264作为测试 ```shell ffmpeg -decoders | sls h264 ------------------------------------------------------ VFS..D h264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 V....D h264_qsv H264 video (Intel Quick Sync Video acceleration) (codec h264) V..... h264_cuvid Nvidia CUVID H264 decoder (codec h264) ``` > PS:h264_qsv ,h264_cuvid,h264 :就是支持的解码方式 > sls :作用相当于linux中的grep > h264_qsv:英特尔硬解码 > h264_cuvid:英伟达硬解码 ## ffmpeg -encoders 同解码格式查询 ```shell ffmpeg -encoders | sls h264 -------------------------------------------------------- V..... libx264 libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (codec h264) V..... libx264 libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (codec h264) V..... libx264rgb libx264 H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 RGB (codec h264) V....D h264_amf AMD AMF H.264 Encoder (codec h264) V....D h264_nvenc NVIDIA NVENC H.264 encoder (codec h264) V..... h264_qsv H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (Intel Quick Sync Video acceleration) (codec h26) ``` > sls :作用相当于linux中的grep > 如以上有类似输出则证明是支持硬解码或者硬编码的。 > 硬件加速命令 ```shell ffmpeg -hwaccel cuvid -c:v h264_cuvid -i input.mp4 -c:v h264_nvenc -b:v 2048k -s 1920x1080 output.mp4 ``` ## 常用命令参数解释 > -hwaccel cuvid:使用cuvid进行硬件加速 > > -c:v h264_cuvid:使用h264_cuvid进行解码 > > -c:v h264_nvenc:使用h264_nvenc进行编码(nvidia硬件加速x265) > > -b:v 2000k:比特率,值越大约清晰。 > > -s 1920x1080:分辨率 > > -b:v 3500K :视频平均比特率为 3500K > > -bufsize 6000K:缓存区大小 6000K,建议设定为 当前码率帧率5,也许这里的2pass是针对缓冲区的,缓存未来5s > > -maxrate 5000K:最大码率为 5000K,每帧的最高码率不超过这个数值 > > -preset slow:预设方案是 slow,slow已经是最好的选项了 > > -pix_fmt p010le:输出的像素格式是 p010le 也就10bit,只有新的硬件支持,例如:970m不支持,1070可以支持 ## 服务器安装相关插件 ```shell yum install -y ffmpeg x264 lame # centos 系列 apt-get install -y ffmpeg x264 lame # ubuntu 系列 ``` # 代码说明 ## 支持的容器格式 ##### 内置的ffmpeg可执行文件支持以下多媒体容器格式: #### 解码 ```shell 描述 格式 Formato Descrizione 4xm 4X Technologies format MTV MTV format RoQ Id RoQ format aac ADTS AAC ac3 raw ac3 aiff Audio IFF alaw pcm A law format amr 3gpp amr file format apc CRYO APC format ape Monkey's Audio asf asf format au SUN AU Format avi avi format avs AVISynth bethsoftvid Bethesda Softworks 'Daggerfall' VID format c93 Interplay C93 daud D-Cinema audio format dsicin Delphine Software International CIN format dts raw dts dv DV video format dxa dxa ea Electronic Arts Multimedia Format ea_cdata Electronic Arts cdata ffm ffm format film_cpk Sega FILM/CPK format flac raw flac flic FLI/FLC/FLX animation format flv flv format gif GIF Animation gxf GXF format h261 raw h261 h263 raw h263 h264 raw H264 video format idcin Id CIN format image2 image2 sequence image2pipe piped image2 sequence ingenient Ingenient MJPEG ipmovie Interplay MVE format libnut nut format m4v raw MPEG4 video format matroska Matroska File Format mjpeg MJPEG video mm American Laser Games MM format mmf mmf format mov,mp4,m4a,3gp,3g2,mj2 QuickTime/MPEG4/Motion JPEG 2000 format mp3 MPEG audio layer 3 mpc musepack mpc8 musepack8 mpeg MPEG1 System format mpegts MPEG2 transport stream format mpegtsraw MPEG2 raw transport stream format mpegvideo MPEG video mulaw pcm mu law format mxf MXF format nsv NullSoft Video format nut nut format nuv NuppelVideo format ogg Ogg format psxstr Sony Playstation STR format rawvideo raw video format redir Redirector format rm rm format rtsp RTSP input format s16be pcm signed 16 bit big endian format s16le pcm signed 16 bit little endian format s8 pcm signed 8 bit format sdp SDP shn raw shorten siff Beam Software SIFF smk Smacker Video sol Sierra SOL Format swf Flash format thp THP tiertexseq Tiertex Limited SEQ format tta true-audio txd txd format u16be pcm unsigned 16 bit big endian format u16le pcm unsigned 16 bit little endian format u8 pcm unsigned 8 bit format vc1 raw vc1 vmd Sierra VMD format voc Creative Voice File format wav wav format wc3movie Wing Commander III movie format wsaud Westwood Studios audio format wsvqa Westwood Studios VQA format wv WavPack yuv4mpegpipe YUV4MPEG pipe format ``` #### 编码 ```shell 描述 格式 3g2 3gp2 format 3gp 3gp format RoQ Id RoQ format ac3 raw ac3 adts ADTS AAC aiff Audio IFF alaw pcm A law format amr 3gpp amr file format asf asf format asf_stream asf format au SUN AU Format avi avi format crc crc testing format dv DV video format dvd MPEG2 PS format (DVD VOB) ffm ffm format flac raw flac flv flv format framecrc framecrc testing format gif GIF Animation gxf GXF format h261 raw h261 h263 raw h263 h264 raw H264 video format image2 image2 sequence image2pipe piped image2 sequence libnut nut format m4v raw MPEG4 video format matroska Matroska File Format mjpeg MJPEG video mmf mmf format mov mov format mp2 MPEG audio layer 2 mp3 MPEG audio layer 3 mp4 mp4 format mpeg MPEG1 System format mpeg1video MPEG video mpeg2video MPEG2 video mpegts MPEG2 transport stream format mpjpeg Mime multipart JPEG format mulaw pcm mu law format null null video format nut nut format ogg Ogg format psp psp mp4 format rawvideo raw video format rm rm format rtp RTP output format s16be pcm signed 16 bit big endian format s16le pcm signed 16 bit little endian format s8 pcm signed 8 bit format svcd MPEG2 PS format (VOB) swf Flash format u16be pcm unsigned 16 bit big endian format u16le pcm unsigned 16 bit little endian format u8 pcm unsigned 8 bit format vcd MPEG1 System format (VCD) vob MPEG2 PS format (VOB) voc Creative Voice File format wav wav format yuv4mpegpipe YUV4MPEG pipe format ``` ## 内置解码器和编码器 ##### 内置的ffmpeg可执行文件包含以下解码器和编码器: #### 音频解码器 ```shell adpcm_4xm adpcm_adx adpcm_ct adpcm_ea adpcm_ea_r1 adpcm_ea_r2 adpcm_ea_r3 adpcm_ea_xas adpcm_ima_amv adpcm_ima_dk3 adpcm_ima_dk4 adpcm_ima_ea_eacs adpcm_ima_ea_sead adpcm_ima_qt adpcm_ima_smjpeg adpcm_ima_wav adpcm_ima_ws adpcm_ms adpcm_sbpro_2 adpcm_sbpro_3 adpcm_sbpro_4 adpcm_swf adpcm_thp adpcm_xa adpcm_yamaha alac ape atrac 3 cook dca dsicinaudio flac g726 imc interplay_dpcm liba52 libamr_nb libamr_wb libfaad libgsm libgsm_ms mace3 mace6 mp2 mp3 mp3adu mp3on4 mpc sv7 mpc sv8 mpeg4aac nellymoser pcm_alaw pcm_mulaw pcm_s16be pcm_s16le pcm_s16le_planar pcm_s24be pcm_s24daud pcm_s24le pcm_s32be pcm_s32le pcm_s8 pcm_u16be pcm_u16le pcm_u24be pcm_u24le pcm_u32be pcm_u32le pcm_u8 pcm_zork qdm2 real_144 real_288 roq_dpcm shorten smackaud sol_dpcm sonic truespeech tta vmdaudio vorbis wavpack wmav1 wmav2 ws_snd1 xan_dpcm ``` #### 音频编码器 ```shell ac3 adpcm_adx adpcm_ima_wav adpcm_ms adpcm_swf adpcm_yamaha flac g726 libamr_nb libamr_wb libfaac libgsm libgsm_ms libmp3lame libvorbis mp2 pcm_alaw pcm_mulaw pcm_s16be pcm_s16le pcm_s24be pcm_s24daud pcm_s24le pcm_s32be pcm_s32le pcm_s8 pcm_u16be pcm_u16le pcm_u24be pcm_u24le pcm_u32be pcm_u32le pcm_u8 pcm_zork roq_dpcm sonic sonicls vorbis wmav1 wmav2 Video decoders 4xm 8bps VMware video aasc amv asv1 asv2 avs bethsoftvid bmp c93 camstudio camtasia cavs cinepak cljr cyuv dnxhd dsicinvideo dvvideo dxa ffv1 ffvhuff flashsv flic flv fraps gif h261 h263 h263i h264 huffyuv idcinvideo indeo2 indeo3 interplayvideo jpegls kmvc loco mdec mjpeg mjpegb mmvideo mpeg1video mpeg2video mpeg4 mpegvideo msmpeg4 msmpeg4v1 msmpeg4v2 msrle msvideo1 mszh nuv pam pbm pgm pgmyuv png ppm ptx qdraw qpeg qtrle rawvideo roqvideo rpza rv10 rv20 sgi smackvid smc snow sp5x svq1 svq3 targa theora thp tiertexseqvideo tiff truemotion1 truemotion2 txd ultimotion vb vc1 vcr1 vmdvideo vp3 vp5 vp6 vp6a vp6f vqavideo wmv1 wmv2 wmv3 wnv1 xan_wc3 xl zlib zmbv Video encoders asv1 asv2 bmp dnxhd dvvideo ffv1 ffvhuff flashsv flv gif h261 h263 h263p huffyuv jpegls libtheora libx264 libxvid ljpeg mjpeg mpeg1video mpeg2video mpeg4 msmpeg4 msmpeg4v1 msmpeg4v2 pam pbm pgm pgmyuv png ppm qtrle rawvideo roqvideo rv10 rv20 sgi snow svq1 targa tiff wmv1 wmv2 zlib zmbv ``` ## 案例 #### 从通用的AVI到类似iqyi(爱奇艺)的FLV电影,带有嵌入式MP3音频流: ```shell File source = new File("source.avi"); File target = new File("target.flv"); AudioAttributes audio = new AudioAttributes(); audio.setCodec("libmp3lame"); audio.setBitRate(new Integer(64000)); audio.setChannels(new Integer(1)); audio.setSamplingRate(new Integer(22050)); VideoAttributes video = new VideoAttributes(); video.setCodec("flv"); video.setBitRate(new Integer(160000)); video.setFrameRate(new Integer(15)); video.setSize(new VideoSize(400, 300)); EncodingAttributes attrs = new EncodingAttributes(); attrs.setFormat("flv"); attrs.setAudioAttributes(audio); attrs.setVideoAttributes(video); Encoder encoder = new Encoder(); encoder.encode(source, target, attrs); ``` #### 接下来的几行从AVI中提取音频信息并将其存储在普通WAV文件中: ```shell File source = new File("source.avi"); File target = new File("target.wav"); AudioAttributes audio = new AudioAttributes(); audio.setCodec("pcm_s16le"); EncodingAttributes attrs = new EncodingAttributes(); attrs.setFormat("wav"); attrs.setAudioAttributes(audio); Encoder encoder = new Encoder(); encoder.encode(source, target, attrs); ``` #### 下一个示例采用音频WAV文件并生成128 kbit/s、立体声、44100 Hz MP3文件: ```shell File source = new File("source.wav"); File target = new File("target.mp3"); AudioAttributes audio = new AudioAttributes(); audio.setCodec("libmp3lame"); audio.setBitRate(new Integer(128000)); audio.setChannels(new Integer(2)); audio.setSamplingRate(new Integer(44100)); EncodingAttributes attrs = new EncodingAttributes(); attrs.setFormat("mp3"); attrs.setAudioAttributes(audio); Encoder encoder = new Encoder(); encoder.encode(source, target, attrs); Next one decodes a generic AVI file and creates another one with the same video stream of the source and a re-encoded low quality MP3 audio stream: File source = new File("source.avi"); File target = new File("target.avi"); AudioAttributes audio = new AudioAttributes(); audio.setCodec("libmp3lame"); audio.setBitRate(new Integer(56000)); audio.setChannels(new Integer(1)); audio.setSamplingRate(new Integer(22050)); VideoAttributes video = new VideoAttributes(); video.setCodec(VideoAttributes.DIRECT_STREAM_COPY); EncodingAttributes attrs = new EncodingAttributes(); attrs.setFormat("avi"); attrs.setAudioAttributes(audio); attrs.setVideoAttributes(video); Encoder encoder = new Encoder(); encoder.encode(source, target, attrs); ``` #### 接下来,生成一个包含MPEG 4/DivX视频和OGG Vorbis音频的AVI: ```shell File source = new File("source.avi"); File target = new File("target.avi"); AudioAttributes audio = new AudioAttributes(); audio.setCodec("libvorbis"); VideoAttributes video = new VideoAttributes(); video.setCodec("mpeg4"); video.setTag("DIVX"); video.setBitRate(new Integer(160000)); video.setFrameRate(new Integer(30)); EncodingAttributes attrs = new EncodingAttributes(); attrs.setFormat("mpegvideo"); attrs.setAudioAttributes(audio); attrs.setVideoAttributes(video); Encoder encoder = new Encoder(); encoder.encode(source, target, attrs); ``` #### 适合智能手机的视频: ```shell File source = new File("source.avi"); File target = new File("target.3gp"); AudioAttributes audio = new AudioAttributes(); audio.setCodec("libfaac"); audio.setBitRate(new Integer(128000)); audio.setSamplingRate(new Integer(44100)); audio.setChannels(new Integer(2)); VideoAttributes video = new VideoAttributes(); video.setCodec("mpeg4"); video.setBitRate(new Integer(160000)); video.setFrameRate(new Integer(15)); video.setSize(new VideoSize(176, 144)); EncodingAttributes attrs = new EncodingAttributes(); attrs.setFormat("3gp"); attrs.setAudioAttributes(audio); attrs.setVideoAttributes(video); Encoder encoder = new Encoder(); encoder.encode(source, target, attrs); ``` #### ASX、 WMA、 ASF、 MPG、 WMV、 GP 、 MP4、 MOV、 AVI、 FLV 转成MP4(支持压缩并高保真): ```shell VideoConvertMp4 videoConvertMp4 = new VideoConvertMp4(); videoConvertMp4.process("linux.mov", "/data/mp4"); ``` #### MP4转成m3u8: ```shell VideoConvertM3u8 videoConvertM3u8 = new VideoConvertM3u8(); videoConvertM3u8.process("linux.mp4", "/data/m3u8"); ``` ## 总结 > 以上就是本人使用ffmpeg硬件加速转码的整体过程,如有疑问或者意见欢迎大家讨论。