Search in sources :

Example 1 with AVIReader

use of com.seleniumtests.util.video.avi.AVIReader in project seleniumRobot by bhecquet.

the class VideoUtils method extractReferenceForSteps.

/**
 * Extract the picture associated to the beginning of a step to <output_dir>/video
 * @param videoFile
 * @param testSteps
 * @param outputDirectory
 */
public static void extractReferenceForSteps(File videoFile, List<TestStep> testSteps, Path outputDirectory) {
    // create output
    Path videoOutputDirectory = outputDirectory.resolve(VIDEO_DIR);
    videoOutputDirectory.toFile().mkdirs();
    AVIReader in = null;
    try {
        // Create the reader
        in = new AVIReader(videoFile);
        // Look for the first video track
        int trackId = 0;
        while (trackId < in.getTrackCount() && in.getFormat(trackId).get(MediaTypeKey) != MediaType.VIDEO) {
            trackId++;
        }
        Map<Long, TestStep> samples = new HashMap<>();
        for (TestStep testStep : testSteps) {
            if (!testStep.isTestEndStep()) {
                // timestamp outside of video, do not try to extract as we would get the last picture
                if (testStep.getVideoTimeStamp() / 1000 * in.getTimeScale(trackId) > in.getChunkCount(0)) {
                    continue;
                }
                samples.put(min(in.timeToSample(trackId, new Rational(testStep.getVideoTimeStamp(), 1000)), in.getChunkCount(trackId) - 1), testStep);
            }
        }
        // Read images from the track
        BufferedImage img = null;
        // read video and extract requested images
        long i = 0;
        int j = 0;
        do {
            img = in.read(trackId, img);
            if (samples.containsKey(i)) {
                Path extractedPicture = videoOutputDirectory.resolve(String.format("video-%d.jpg", j));
                FileUtility.writeImage(extractedPicture.toString(), img);
                Snapshot snapshot = new Snapshot(new ScreenShot(outputDirectory.relativize(extractedPicture).toString()), "Step beginning state", SnapshotCheckType.REFERENCE_ONLY);
                // by default, reference snapshot won't be displayed in report. This flag will be set to "true" only if step fails and we have a reference picture from server
                snapshot.setDisplayInReport(false);
                samples.get(i).addSnapshot(snapshot, j, null);
                j++;
            }
            i++;
        } while (img != null);
    } catch (IOException e) {
        logger.error("Cannot extract step reference " + e.getMessage());
    } finally {
        // Close the reader
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                logger.error("Cannot close video reader " + e.getMessage());
            }
        }
    }
}
Also used : Path(java.nio.file.Path) AVIReader(com.seleniumtests.util.video.avi.AVIReader) TestStep(com.seleniumtests.reporter.logger.TestStep) Rational(org.monte.media.math.Rational) HashMap(java.util.HashMap) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) Snapshot(com.seleniumtests.reporter.logger.Snapshot) ScreenShot(com.seleniumtests.driver.screenshots.ScreenShot)

Aggregations

ScreenShot (com.seleniumtests.driver.screenshots.ScreenShot)1 Snapshot (com.seleniumtests.reporter.logger.Snapshot)1 TestStep (com.seleniumtests.reporter.logger.TestStep)1 AVIReader (com.seleniumtests.util.video.avi.AVIReader)1 BufferedImage (java.awt.image.BufferedImage)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 HashMap (java.util.HashMap)1 Rational (org.monte.media.math.Rational)1