Autopsy  4.4.1
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.opencv.core.Mat;
34 import org.opencv.highgui.VideoCapture;
35 import org.openide.util.NbBundle;
38 import static org.sleuthkit.autopsy.coreutils.ImageUtils.isMediaThumbnailSupported;
40 import org.sleuthkit.datamodel.AbstractFile;
41 
45 public class VideoUtils {
46 
47  private static final List<String> SUPPORTED_VIDEO_EXTENSIONS =
48  Arrays.asList("mov", "m4v", "flv", "mp4", "3gp", "avi", "mpg", //NON-NLS
49  "mpeg", "asf", "divx", "rm", "moov", "wmv", "vob", "dat", //NON-NLS
50  "m1v", "m2v", "m4v", "mkv", "mpe", "yop", "vqa", "xmv", //NON-NLS
51  "mve", "wtv", "webm", "vivo", "vc1", "seq", "thp", "san", //NON-NLS
52  "mjpg", "smk", "vmd", "sol", "cpk", "sdp", "sbg", "rtsp", //NON-NLS
53  "rpl", "rl2", "r3d", "mlp", "mjpeg", "hevc", "h265", "265", //NON-NLS
54  "h264", "h263", "h261", "drc", "avs", "pva", "pmp", "ogg", //NON-NLS
55  "nut", "nuv", "nsv", "mxf", "mtv", "mvi", "mxg", "lxf", //NON-NLS
56  "lvf", "ivf", "mve", "cin", "hnm", "gxf", "fli", "flc", //NON-NLS
57  "flx", "ffm", "wve", "uv2", "dxa", "dv", "cdxl", "cdg", //NON-NLS
58  "bfi", "jv", "bik", "vid", "vb", "son", "avs", "paf", "mm", //NON-NLS
59  "flm", "tmv", "4xm"); //NON-NLS
60 
61  private static final SortedSet<String> SUPPORTED_VIDEO_MIME_TYPES = new TreeSet<>(
62  Arrays.asList("application/x-shockwave-flash",
63  "video/x-m4v",
64  "video/x-flv",
65  "video/quicktime",
66  "video/avi",
67  "video/msvideo",
68  "video/x-msvideo", //NON-NLS
69  "video/mp4",
70  "video/x-ms-wmv",
71  "video/mpeg",
72  "video/asf")); //NON-NLS
73 
74  public static List<String> getSupportedVideoExtensions() {
76  }
77 
78  public static SortedSet<String> getSupportedVideoMimeTypes() {
79  return Collections.unmodifiableSortedSet(SUPPORTED_VIDEO_MIME_TYPES);
80  }
81 
82  private static final int THUMB_COLUMNS = 3;
83  private static final int THUMB_ROWS = 3;
84  private static final int CV_CAP_PROP_POS_MSEC = 0;
85  private static final int CV_CAP_PROP_FRAME_COUNT = 7;
86  private static final int CV_CAP_PROP_FPS = 5;
87 
88  static final Logger LOGGER = Logger.getLogger(VideoUtils.class.getName());
89 
90  private VideoUtils() {
91  }
92 
93  public static File getTempVideoFile(AbstractFile file) {
94  return Paths.get(Case.getCurrentCase().getTempDirectory(), "videos", file.getId() + "." + file.getNameExtension()).toFile(); //NON-NLS
95  }
96 
97  public static boolean isVideoThumbnailSupported(AbstractFile file) {
98  return isMediaThumbnailSupported(file, "video/", SUPPORTED_VIDEO_MIME_TYPES, SUPPORTED_VIDEO_EXTENSIONS);
99  }
100 
101  @NbBundle.Messages({"# {0} - file name",
102  "VideoUtils.genVideoThumb.progress.text=extracting temporary file {0}"})
103  static BufferedImage generateVideoThumbnail(AbstractFile file, int iconSize) {
104  java.io.File tempFile = getTempVideoFile(file);
105  if (tempFile.exists() == false || tempFile.length() < file.getSize()) {
106  ProgressHandle progress = ProgressHandle.createHandle(Bundle.VideoUtils_genVideoThumb_progress_text(file.getName()));
107  progress.start(100);
108  try {
109  Files.createParentDirs(tempFile);
110  if (Thread.interrupted()) {
111  return null;
112  }
113  ContentUtils.writeToFile(file, tempFile, progress, null, true);
114  } catch (IOException ex) {
115  LOGGER.log(Level.WARNING, "Error extracting temporary file for " + ImageUtils.getContentPathSafe(file), ex); //NON-NLS
116  } finally {
117  progress.finish();
118  }
119  }
120  VideoCapture videoFile = new VideoCapture(); // will contain the video
121  BufferedImage bufferedImage = null;
122 
123  try {
124 
125  if (!videoFile.open(tempFile.toString())) {
126  LOGGER.log(Level.WARNING, "Error opening {0} for preview generation.", ImageUtils.getContentPathSafe(file)); //NON-NLS
127  return null;
128  }
129  double fps = videoFile.get(CV_CAP_PROP_FPS); // gets frame per second
130  double totalFrames = videoFile.get(CV_CAP_PROP_FRAME_COUNT); // gets total frames
131  if (fps <= 0 || totalFrames <= 0) {
132  LOGGER.log(Level.WARNING, "Error getting fps or total frames for {0}", ImageUtils.getContentPathSafe(file)); //NON-NLS
133  return null;
134  }
135  double milliseconds = 1000 * (totalFrames / fps); //total milliseconds
136 
137  double timestamp = Math.min(milliseconds, 500); //default time to check for is 500ms, unless the files is extremely small
138 
139  int framkeskip = Double.valueOf(Math.floor((milliseconds - timestamp) / (THUMB_COLUMNS * THUMB_ROWS))).intValue();
140 
141  Mat imageMatrix = new Mat();
142 
143  for (int x = 0; x < THUMB_COLUMNS; x++) {
144  for (int y = 0; y < THUMB_ROWS; y++) {
145  if (Thread.interrupted()) {
146  return null;
147  }
148  if (!videoFile.set(CV_CAP_PROP_POS_MSEC, timestamp + x * framkeskip + y * framkeskip * THUMB_COLUMNS)) {
149  LOGGER.log(Level.WARNING, "Error seeking to " + timestamp + "ms in {0}", ImageUtils.getContentPathSafe(file)); //NON-NLS
150  break; // if we can't set the time, return black for that frame
151  }
152  //read the frame into the image/matrix
153  if (!videoFile.read(imageMatrix)) {
154  LOGGER.log(Level.WARNING, "Error reading frames at " + timestamp + "ms from {0}", ImageUtils.getContentPathSafe(file)); //NON-NLS
155  break; //if the image for some reason is bad, return black for that frame
156  }
157 
158  if (bufferedImage == null) {
159  bufferedImage = new BufferedImage(imageMatrix.cols() * THUMB_COLUMNS, imageMatrix.rows() * THUMB_ROWS, BufferedImage.TYPE_3BYTE_BGR);
160  }
161 
162  byte[] data = new byte[imageMatrix.rows() * imageMatrix.cols() * (int) (imageMatrix.elemSize())];
163  imageMatrix.get(0, 0, data); //copy the image to data
164 
165  //todo: this looks like we are swapping the first and third channels. so we can use BufferedImage.TYPE_3BYTE_BGR
166  if (imageMatrix.channels() == 3) {
167  for (int k = 0; k < data.length; k += 3) {
168  byte temp = data[k];
169  data[k] = data[k + 2];
170  data[k + 2] = temp;
171  }
172  }
173 
174  bufferedImage.getRaster().setDataElements(imageMatrix.cols() * x, imageMatrix.rows() * y, imageMatrix.cols(), imageMatrix.rows(), data);
175  }
176  }
177  } finally {
178  videoFile.release(); // close the file}
179  }
180  if (Thread.interrupted()) {
181  return null;
182  }
183  return bufferedImage == null ? null : ScalrWrapper.resizeFast(bufferedImage, iconSize);
184  }
185 }
static List< String > getSupportedVideoExtensions()
Definition: VideoUtils.java:74
static SortedSet< String > getSupportedVideoMimeTypes()
Definition: VideoUtils.java:78
static final List< String > SUPPORTED_VIDEO_EXTENSIONS
Definition: VideoUtils.java:47
static final SortedSet< String > SUPPORTED_VIDEO_MIME_TYPES
Definition: VideoUtils.java:61
synchronized static Logger getLogger(String name)
Definition: Logger.java:161
static File getTempVideoFile(AbstractFile file)
Definition: VideoUtils.java:93
static boolean isVideoThumbnailSupported(AbstractFile file)
Definition: VideoUtils.java:97

Copyright © 2012-2016 Basis Technology. Generated on: Fri Sep 29 2017
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.