Autopsy  4.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
VideoUtils.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2015-16 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.coreutils;
20 
21 import com.google.common.io.Files;
22 import java.awt.image.BufferedImage;
23 import java.io.File;
24 import java.io.IOException;
25 import java.nio.file.Paths;
26 import java.util.Arrays;
27 import java.util.Collections;
28 import java.util.List;
29 import java.util.SortedSet;
30 import java.util.TreeSet;
31 import java.util.logging.Level;
32 import org.netbeans.api.progress.ProgressHandle;
33 import org.netbeans.api.progress.ProgressHandleFactory;
34 import org.opencv.core.Mat;
35 import org.opencv.highgui.VideoCapture;
36 import org.openide.util.NbBundle;
39 import static org.sleuthkit.autopsy.coreutils.ImageUtils.isMediaThumbnailSupported;
41 import org.sleuthkit.datamodel.AbstractFile;
42 
46 public class VideoUtils {
47 
48  private static final List<String> SUPPORTED_VIDEO_EXTENSIONS =
49  Arrays.asList("mov", "m4v", "flv", "mp4", "3gp", "avi", "mpg", //NON-NLS
50  "mpeg", "asf", "divx", "rm", "moov", "wmv", "vob", "dat", //NON-NLS
51  "m1v", "m2v", "m4v", "mkv", "mpe", "yop", "vqa", "xmv", //NON-NLS
52  "mve", "wtv", "webm", "vivo", "vc1", "seq", "thp", "san", //NON-NLS
53  "mjpg", "smk", "vmd", "sol", "cpk", "sdp", "sbg", "rtsp", //NON-NLS
54  "rpl", "rl2", "r3d", "mlp", "mjpeg", "hevc", "h265", "265", //NON-NLS
55  "h264", "h263", "h261", "drc", "avs", "pva", "pmp", "ogg", //NON-NLS
56  "nut", "nuv", "nsv", "mxf", "mtv", "mvi", "mxg", "lxf", //NON-NLS
57  "lvf", "ivf", "mve", "cin", "hnm", "gxf", "fli", "flc", //NON-NLS
58  "flx", "ffm", "wve", "uv2", "dxa", "dv", "cdxl", "cdg", //NON-NLS
59  "bfi", "jv", "bik", "vid", "vb", "son", "avs", "paf", "mm", //NON-NLS
60  "flm", "tmv", "4xm"); //NON-NLS
61 
62  private static final SortedSet<String> SUPPORTED_VIDEO_MIME_TYPES = new TreeSet<>(
63  Arrays.asList("application/x-shockwave-flash",
64  "video/x-m4v",
65  "video/x-flv",
66  "video/quicktime",
67  "video/avi",
68  "video/msvideo",
69  "video/x-msvideo", //NON-NLS
70  "video/mp4",
71  "video/x-ms-wmv",
72  "video/mpeg",
73  "video/asf")); //NON-NLS
74 
75  public static List<String> getSupportedVideoExtensions() {
77  }
78 
79  public static SortedSet<String> getSupportedVideoMimeTypes() {
80  return Collections.unmodifiableSortedSet(SUPPORTED_VIDEO_MIME_TYPES);
81  }
82 
83  private static final int THUMB_COLUMNS = 3;
84  private static final int THUMB_ROWS = 3;
85  private static final int CV_CAP_PROP_POS_MSEC = 0;
86  private static final int CV_CAP_PROP_FRAME_COUNT = 7;
87  private static final int CV_CAP_PROP_FPS = 5;
88 
89  static final Logger LOGGER = Logger.getLogger(VideoUtils.class.getName());
90 
91  private VideoUtils() {
92  }
93 
94  public static File getTempVideoFile(AbstractFile file) {
95  return Paths.get(Case.getCurrentCase().getTempDirectory(), "videos", file.getId() + "." + file.getNameExtension()).toFile(); //NON-NLS
96  }
97 
98  public static boolean isVideoThumbnailSupported(AbstractFile file) {
99  return isMediaThumbnailSupported(file, "video/", SUPPORTED_VIDEO_MIME_TYPES, SUPPORTED_VIDEO_EXTENSIONS);
100  }
101 
102  @NbBundle.Messages({"# {0} - file name",
103  "VideoUtils.genVideoThumb.progress.text=extracting temporary file {0}"})
104  static BufferedImage generateVideoThumbnail(AbstractFile file, int iconSize) {
105  java.io.File tempFile = getTempVideoFile(file);
106  if (tempFile.exists() == false || tempFile.length() < file.getSize()) {
107  ProgressHandle progress = ProgressHandleFactory.createHandle(Bundle.VideoUtils_genVideoThumb_progress_text(file.getName()));
108  progress.start(100);
109  try {
110  Files.createParentDirs(tempFile);
111  ContentUtils.writeToFile(file, tempFile, progress, null, true);
112  } catch (IOException ex) {
113  LOGGER.log(Level.WARNING, "Error extracting temporary file for " + ImageUtils.getContentPathSafe(file), ex); //NON-NLS
114  } finally {
115  progress.finish();
116  }
117  }
118 
119  VideoCapture videoFile = new VideoCapture(); // will contain the video
120 
121  if (!videoFile.open(tempFile.toString())) {
122  LOGGER.log(Level.WARNING, "Error opening {0} for preview generation.", ImageUtils.getContentPathSafe(file)); //NON-NLS
123  return null;
124  }
125  double fps = videoFile.get(CV_CAP_PROP_FPS); // gets frame per second
126  double totalFrames = videoFile.get(CV_CAP_PROP_FRAME_COUNT); // gets total frames
127  if (fps <= 0 || totalFrames <= 0) {
128  LOGGER.log(Level.WARNING, "Error getting fps or total frames for {0}", ImageUtils.getContentPathSafe(file)); //NON-NLS
129  return null;
130  }
131  double milliseconds = 1000 * (totalFrames / fps); //total milliseconds
132 
133  double timestamp = Math.min(milliseconds, 500); //default time to check for is 500ms, unless the files is extremely small
134 
135  int framkeskip = Double.valueOf(Math.floor((milliseconds - timestamp) / (THUMB_COLUMNS * THUMB_ROWS))).intValue();
136 
137  Mat imageMatrix = new Mat();
138  BufferedImage bufferedImage = null;
139 
140  for (int x = 0; x < THUMB_COLUMNS; x++) {
141  for (int y = 0; y < THUMB_ROWS; y++) {
142  if (!videoFile.set(CV_CAP_PROP_POS_MSEC, timestamp + x * framkeskip + y * framkeskip * THUMB_COLUMNS)) {
143  LOGGER.log(Level.WARNING, "Error seeking to " + timestamp + "ms in {0}", ImageUtils.getContentPathSafe(file)); //NON-NLS
144  break; // if we can't set the time, return black for that frame
145  }
146  //read the frame into the image/matrix
147  if (!videoFile.read(imageMatrix)) {
148  LOGGER.log(Level.WARNING, "Error reading frames at " + timestamp + "ms from {0}", ImageUtils.getContentPathSafe(file)); //NON-NLS
149  break; //if the image for some reason is bad, return black for that frame
150  }
151 
152  if (bufferedImage == null) {
153  bufferedImage = new BufferedImage(imageMatrix.cols() * THUMB_COLUMNS, imageMatrix.rows() * THUMB_ROWS, BufferedImage.TYPE_3BYTE_BGR);
154  }
155 
156  byte[] data = new byte[imageMatrix.rows() * imageMatrix.cols() * (int) (imageMatrix.elemSize())];
157  imageMatrix.get(0, 0, data); //copy the image to data
158 
159  //todo: this looks like we are swapping the first and third channels. so we can use BufferedImage.TYPE_3BYTE_BGR
160  if (imageMatrix.channels() == 3) {
161  for (int k = 0; k < data.length; k += 3) {
162  byte temp = data[k];
163  data[k] = data[k + 2];
164  data[k + 2] = temp;
165  }
166  }
167 
168  bufferedImage.getRaster().setDataElements(imageMatrix.cols() * x, imageMatrix.rows() * y, imageMatrix.cols(), imageMatrix.rows(), data);
169  }
170  }
171 
172  videoFile.release(); // close the file
173 
174  return bufferedImage == null ? null : ScalrWrapper.resizeFast(bufferedImage, iconSize);
175  }
176 }
static List< String > getSupportedVideoExtensions()
Definition: VideoUtils.java:75
static SortedSet< String > getSupportedVideoMimeTypes()
Definition: VideoUtils.java:79
static final List< String > SUPPORTED_VIDEO_EXTENSIONS
Definition: VideoUtils.java:48
static final SortedSet< String > SUPPORTED_VIDEO_MIME_TYPES
Definition: VideoUtils.java:62
synchronized static Logger getLogger(String name)
Definition: Logger.java:166
static File getTempVideoFile(AbstractFile file)
Definition: VideoUtils.java:94
static boolean isVideoThumbnailSupported(AbstractFile file)
Definition: VideoUtils.java:98

Copyright © 2012-2015 Basis Technology. Generated on: Wed Apr 6 2016
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.