图片和视频压缩例子
/*** 視頻壓縮 需要jave jar 包。到網上下載即可 * * @param source 需要壓縮的視頻* @param targetPath 壓縮的目標路徑* @return*/public static boolean compressVideo(File source, String targetPath) {System.out.println("source:" + source);System.out.println("targetPath:" + targetPath);try {// 編碼器Encoder encoder = new Encoder();// 源視頻的信息MultimediaInfo mInfo = encoder.getInfo(source);AudioInfo audioInfo = mInfo.getAudio();VideoInfo videoInfo = mInfo.getVideo();// 音頻參數設置(一些參數都是使用源視頻)AudioAttributes audio = new AudioAttributes();audio.setBitRate(44100);audio.setChannels(audioInfo.getChannels());audio.setSamplingRate(audioInfo.getSamplingRate());audio.setCodec("libmp3lame");audio.setVolume(256);// 256表示原聲,小于表示減少音量 大于表示增加音量// 視頻參數設置(一些參數都是使用源視頻)VideoAttributes video = new VideoAttributes();video.setCodec("mpeg4");video.setBitRate(videoInfo.getBitRate());
// VideoSize size = new VideoSize(720, 960);
// video.setSize(size);video.setFrameRate(30);// 視頻轉碼編碼設置EncodingAttributes attrs = new EncodingAttributes();attrs.setFormat("mp4");attrs.setAudioAttributes(audio);attrs.setVideoAttributes(video);System.out.println("audioInfo:" + audioInfo);System.out.println("videoInfo:" + videoInfo);System.out.println("mInfo" + mInfo);File target = new File(targetPath);String decoderstr = videoInfo.getDecoder();System.out.println("decoderstr:" + decoderstr);String[] decoder = encoder.getSupportedDecodingFormats();List<String> list = new ArrayList<String>();for (int i = 0; i < decoder.length; i++) {list.add(decoder[i]);}// 如果視頻解碼方式存在就編碼。不然就不用// 。。查看解碼格式類型:http://www.sauronsoftware.it/projects/jave/manual.php#3.1if (list.contains(decoderstr)) {encoder.encode(source, target, attrs);logger.info("壓縮完成...");} else {logger.info("不支持該編碼的視頻,編碼為:{};提示:壓縮失敗", decoderstr);}} catch (EncoderException e) {logger.info("視頻壓縮出現異常");e.printStackTrace();}return false;}/*** 圖片壓縮* * @param source* @param target*/public static void compressPicture(File source, String targetPath) {try {System.out.println("文件:" + source + "臨時文件路徑:" + targetPath);System.out.println("begin compress picture。。。。");FileOutputStream fos = new FileOutputStream(targetPath);BufferedImage bi = ImageIO.read(source);int srcWidth = bi.getWidth();int srcHeight = bi.getHeight();Image image = bi.getScaledInstance(srcWidth, srcHeight, Image.SCALE_SMOOTH);BufferedImage tag = new BufferedImage(srcWidth, srcHeight, BufferedImage.TYPE_INT_RGB);Graphics g = tag.getGraphics();g.setColor(Color.RED);g.drawImage(image, 0, 0, null);g.dispose();ImageIO.write(tag, "jpg", fos);System.out.println("end compress picture。。。。");} catch (IOException e) {e.printStackTrace();}}
}
?
轉載于:https://www.cnblogs.com/gaolt/p/10556543.html
總結
- 上一篇: 我实在不懂Python的Asyncio
- 下一篇: VS2010中预处理器定义