preface
ws.schild is a java toolkit based on ffmpeg. It is the mainstream java toolkit for transcoding, clipping and extracting video and audio.
1, Introduction to main tools
1. VideoAttributes
VideoAttributes is WS The important tool class for setting video attributes in Schild toolkit is WS Schild is the most important class for video operation, and it is also the most widely used class.
Usage experience: (below, video is the instantiated object of this class)
1. The Quality attribute of video has a great impact on the size of the video after transcoding, and also has a certain impact on the transcoding time. It mainly affects the video Quality. The parameter type is shaping. The smaller the value, the higher the Quality.
2. The size attribute of video has a great impact on the size of the video after transcoding, and also has a certain impact on the transcoding time. The parameter type is VideoSize.
3. The BitRate attribute of video has a great impact on the size and time of transcoded video, mainly affecting the fluency of video. Generally, it needs to be set larger, such as 100k (for tested video, universality cannot be guaranteed).
4. The Codec attribute of video is generally set to mpeg4 or h264.
5. The FrameRate attribute of video should not be set too low. It is generally set to 15 or above. If it is set too small, the video will not be smooth.
(the above attributes may have different effects on different videos, so you need to test more ~ ~)
2. AudioAttributes
AudioAttributes is WS An important tool class for setting audio attributes in Schild toolkit.
2, Use steps
1. Realize video transcoding
Transcoding video to H.264 coding (including audio transcoding):
/** * Video transcoding * @param videoSource * @param videoTarget * @return true or false */ public static boolean videoToVideo(String videoSource, String videoTarget) { // Date time = new Date(); // SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss"); // System.out.println(simpleDateFormat.format(time)); long start = System.currentTimeMillis(); File source = new File(videoSource); File target = new File(videoTarget); AudioAttributes audio = new AudioAttributes(); audio.setCodec("aac"); audio.setBitRate(236000 / 2); audio.setChannels(2); audio.setSamplingRate(8000); VideoAttributes video = new VideoAttributes(); video.setCodec("h264"); video.setBitRate(1000000); video.setFrameRate(25); video.setQuality(4); // video.setSize(new VideoSize(720, 480)); EncodingAttributes attrs = new EncodingAttributes(); attrs.setOutputFormat("mp4"); attrs.setAudioAttributes(audio); attrs.setVideoAttributes(video); Encoder encoder = new Encoder(); try { encoder.encode(new MultimediaObject(source), target, attrs); return true; } catch (Exception e) { e.printStackTrace(); System.out.println(encoder.getUnhandledMessages()); return false; }finally { // time = new Date(); // System.out.println(simpleDateFormat.format(time)); long end = System.currentTimeMillis(); System.out.println("Total time:"+ (end-start) +"ms"); } }
2. Realize audio transcoding
Transcoding audio to mp3 encoding:
/** * m4r The audio format is converted to mp3, and audioPath can be changed to the audio format to be converted * @param audioPath * @param mp3Path */ public static void m4rToMp3(String audioPath,String mp3Path){ File source = new File(audioPath); File target = new File(mp3Path); AudioAttributes audio = new AudioAttributes(); audio.setCodec("libmp3lame"); audio.setBitRate(128000); audio.setChannels(2); audio.setSamplingRate(44100); EncodingAttributes attrs = new EncodingAttributes(); attrs.setOutputFormat("mp3"); attrs.setAudioAttributes(audio); Encoder encoder = new Encoder(); try { encoder.encode(new MultimediaObject(source), target, attrs); } catch (EncoderException e) { e.printStackTrace(); } }
3. Convert video files to audio files
Convert video to Audio:
/** * Video file to audio file * @param videoPath * @param audioPath * return true or false */ public static boolean videoToAudio(String videoPath, String audioPath){ File fileMp4 = new File(videoPath); File fileMp3 = new File(audioPath); AudioAttributes audio = new AudioAttributes(); audio.setCodec("libmp3lame"); audio.setBitRate(128000); audio.setChannels(2); audio.setSamplingRate(44100); EncodingAttributes attrs = new EncodingAttributes(); attrs.setOutputFormat("mp3"); attrs.setAudioAttributes(audio); Encoder encoder = new Encoder(); MultimediaObject mediaObject = new MultimediaObject(fileMp4); try{ encoder.encode(mediaObject,fileMp3,attrs); Log.info("File MP4 convertito MP3"); return true; }catch (Exception e){ Log.error("File non convertito"); Log.error(e.getMessage()); return false; } }
summary
ws.schild toolkit can easily help us realize the transcoding and clipping of video and audio, and can meet different needs by setting different parameters. However, in terms of the current test results, different coding formats may have different sensitivity to parameters, and the specific scenario should be analyzed in detail.