Due to the project requirements involved in video audio extraction, as well as the function of subtitle compression, has been studying ffmpeg, only two functions, but deeply ffmpeg torture.
Today talk about FFmpeg in Java simple use, first download FFmpeg package, the official address: http://ffmpeg.org/download.html, here is recommended to download the Linux Static builds version, Light small and can be used directly after decompression, I use the version is FFMPEG-GIT-20170922-64BIT-STATIC.TAR.XZ.
After unpacking, the folder has an executable file ffmpeg, which can be run directly on Linux./ffmpeg-version, you can view the version information for ffmpeg and configuration information.
You can now use the FFmpeg commands to do something:
1. Audio extract in Video:ffmpeg-i [videofile] -vn-acodec copy [audiofile]
2. Subtitles compressed into the video:ffmpeg-i [videofile] -vf subtitles=[subtitle.srt] [Targetvideofile]
3. Other related commands can be consulted:http://ffmpeg.org/ffmpeg.html
Description
- Videofile is a video source file that needs to extract audio, either a local file or a network file URL.
- SUBTITLE.SRT is a subtitle file (Chinese subtitles that change English to Chinese, other formats are consistent), this side uses the simplest SRT standard format, as follows:
- If you want to compress Chinese subtitles, you need to have Chinese fonts in the system, using Fc-list query system supported fonts, FC-LIST:LANG=ZH query supported Chinese fonts
- The relevant Java code is as follows:
Public classFfmpegutil {Private Static FinalLogger Logger = Logger.getlogger (ffmpegutil.class); //ffmpeg command Location path Private Static FinalString Ffmpeg_path = "/ffmpeg/ffmpeg"; //ffmpeg temporary files after processing Private Static FinalString Tmp_path = "/tmp"; //Home Path Private Static FinalString Home_path; Static{Home_path= System.getproperty ("User.home"); Logger.info ("Static Home Path:" +Home_path); } /*** Video Turn audio *@paramVideourl*/ Public Staticstring Videotoaudio (String videourl) {string Aacfile= ""; Try{aacfile= Tmp_path + "/" +NewSimpleDateFormat ("Yyyymmddhhmmss"). Format (NewDate ())+ Uuid.randomuuid (). toString (). ReplaceAll ("-", "") + ". AAC"; String Command= Home_path + Ffmpeg_path + "-i" + Videourl + "-vn-acodec copy" +Aacfile; Logger.info ("Video to Audio command:" +command); Process Process=runtime.getruntime (). exec (command); Process.waitfor (); } Catch(Exception e) {logger.error ("Video to audio failed, video address:" +Videourl, E); } return""; } /*** Burn subtitles to video *@paramVideourl*/ Public Staticstring Burnsubtitlesintovideo (String Videourl, String subtitleurl) {string Burnedfile= ""; Try{burnedfile= Tmp_path + "/" +NewSimpleDateFormat ("Yyyymmddhhmmss"). Format (NewDate ())+ Uuid.randomuuid (). toString (). ReplaceAll ("-", "") + ". mp4"; String Command= Home_path + Ffmpeg_path + "-i" + Videourl + "-VF subtitles=" + Subtitleurl + "" +Burnedfile; Logger.info ("Burn subtitle into video command:" +command); Process Process=runtime.getruntime (). exec (command); Process.waitfor (); } Catch(Exception e) {logger.error ("Video compression caption failed, video address:" +videourl+ ", subtitle address:" +Subtitleurl, E); } return""; }}
FFmpeg use in Java-Audio Extraction & subtitle compression