Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to obtain video duration and capture screenshots of frames by Java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you Java how to obtain video length and capture screenshots of frames, I believe most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's learn about it!

Example

Add dependency

Org.bytedeco javacv-platform 1.4.4

The above maven dependency contains complete javacv functions (very many, dependencies on Jar account for probably 500MB). Since only the feature of ffmpeg is used here, extraneous parts can also be excluded as follows.

Org.bytedeco javacv 1.4.4 org.bytedeco javacpp org.bytedeco.javacpp-presets flycapture org.bytedeco.javacpp-presets libdc1394 org.bytedeco.javacpp-presets libfreenect org.bytedeco. Javacpp-presets libfreenect2 org.bytedeco.javacpp-presets librealsense org.bytedeco.javacpp-presets videoinput org.bytedeco.javacpp-presets opencv org.bytedeco.javacpp-presets tesseract org.bytedeco. Javacpp-presets leptonica org.bytedeco.javacpp-presets flandmark org.bytedeco.javacpp-presets artoolkitplus org.bytedeco javacv-platform 1.4.4 org.bytedeco javacv org.bytedeco.javacpp -presets flycapture-platform org.bytedeco.javacpp-presets libdc1394-platform org.bytedeco.javacpp-presets libfreenect-platform org.bytedeco.javacpp-presets libfreenect2-platform org.bytedeco.javacpp-presets librealsense-platform Org.bytedeco.javacpp-presets videoinput-platform org.bytedeco.javacpp-presets opencv-platform org.bytedeco.javacpp-presets tesseract-platform org.bytedeco.javacpp-presets leptonica-platform org.bytedeco.javacpp-presets Flandmark-platform org.bytedeco.javacpp-presets artoolkitplus-platform

Core code

Get the video duration

/ * acquire video duration in seconds * * @ param video source video file * @ return duration (s) * / public static long getVideoDuration (File video) {long duration = 0L; FFmpegFrameGrabber ff = new FFmpegFrameGrabber (video); try {ff.start (); duration = ff.getLengthInTime () / (1000 * 1000); ff.stop () } catch (FrameGrabber.Exception e) {e.printStackTrace ();} return duration;}

Capture the video and specify the frame as the picture

/ * capture the video to get the picture of the specified frame * * @ param video source video file * @ param picPath screenshot storage path * / public static void getVideoPic (File video, String picPath) {FFmpegFrameGrabber ff = new FFmpegFrameGrabber (video); try {ff.start (); / / capture the intermediate frame image (depending on the actual situation) int I = 0 Int length = ff.getLengthInFrames (); int middleFrame = length / 2; Frame frame = null; while (I

< length) { frame = ff.grabFrame(); if ((i >

MiddleFrame) & & (frame.image! = null) {break;} iImages;} / / captured frame images Java2DFrameConverter converter = new Java2DFrameConverter (); BufferedImage srcImage = converter.getBufferedImage (frame); int srcImageWidth = srcImage.getWidth (); int srcImageHeight = srcImage.getHeight (); / / A pair of screenshots are scaled proportionally (thumbnails) int width = 480 Int height = (int) (double) width / srcImageWidth) * srcImageHeight); BufferedImage thumbnailImage = new BufferedImage (width, height, BufferedImage.TYPE_3BYTE_BGR); thumbnailImage.getGraphics (). DrawImage (srcImage.getScaledInstance (width, height, Image.SCALE_SMOOTH), 0,0, null); File picFile = new File (picPath); ImageIO.write (thumbnailImage, "jpg", picFile); ff.stop () } catch (IOException e) {e.printStackTrace ();}}

Test case

Public static void main (String [] args) {String videoPath = ResourceUtils.CLASSPATH_URL_PREFIX + "video.mp4"; File video = null; try {video = ResourceUtils.getFile (videoPath);} catch (FileNotFoundException e) {e.printStackTrace ();} String picPath = "video.jpg"; getVideoPic (video, picPath); long duration = getVideoDuration (video); System.out.println ("videoDuration =" + duration) } these are all the contents of the article "how to get video duration and capture frame screenshots by Java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report