日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

图片和视频压缩例子

發布時間:2023/12/1 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 图片和视频压缩例子 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
/*** 視頻壓縮 需要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

總結

以上是生活随笔為你收集整理的图片和视频压缩例子的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。