In addition to Weibo, there is also WeChat
Please pay attention

WeChat public account
Shulou
2025-11-02 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.

The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope





About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.