Search in sources :

Example 1 with DownloadThread

use of com.bixly.pastevid.download.DownloadThread in project screenbird by adamhub.

the class VideoScrubManager method scrubVideo.

/**
     * Updates the recorded video with a newly recorded portion.
     * @param rawVideoPath Location of the raw video recording which is to be 
     * used to sample from.
     */
public void scrubVideo(String rawVideoPath, FFMpegProgressBarListener progressBarListener) {
    // Get access to scrub info
    VideoScrub scrub = null;
    File videoClip = null;
    File rawVideoMPG = null;
    File rawVideoMP4 = new File(rawVideoPath);
    File finalClipMPG = null;
    File finalClipMP4 = null;
    File handbrake = null;
    File rawVideoMP4Backup = new File(rawVideoMP4.getParent() + Settings.FILE_SEP + "raw.mp4");
    // Not cut, no editing needed
    if (this.scrubs.isEmpty()) {
        return;
    }
    this.isScrubbing = true;
    // Grab reference to handbrake download
    DownloadThread downloadHandbrake = DownloadManager.getInstance().getDownload(Settings.getHandbrakeExecutable());
    log(String.format("RawMP4[%s], Exists?[%s]", rawVideoPath, rawVideoMP4.exists()));
    // Return if raw video mp4 file does not exist
    if (!rawVideoMP4.exists() || rawVideoMP4.length() == 0) {
        return;
    }
    // Convert video to MPG
    rawVideoMPG = FileUtil.convertMp4ToMpg(rawVideoMP4, progressBarListener);
    // Return if the resulting MPG file is not found
    if (!rawVideoMPG.exists() || rawVideoMPG.length() == 0) {
        return;
    }
    finalClipMPG = new File(Settings.SCREEN_CAPTURE_DIR + "merged.mpg");
    int rawVideoMpgLength = FileUtil.getVideoDuration(rawVideoMPG.getAbsolutePath());
    int duration;
    int offset = 0;
    log("Number of cuts: " + this.scrubs.size());
    // Search for segments which are to be extracted from raw video
    for (int i = 0; i <= this.scrubs.size(); i++) {
        // Re-use the last cut info for clip
        if (i < this.scrubs.size()) {
            scrub = this.scrubs.get(i);
        }
        videoClip = new File(Settings.SCREEN_CAPTURE_DIR + "clip" + i + ".mpg");
        log(String.format("Scrubbing out %d to %d from %s", scrub.start, scrub.end, rawVideoMPG.getName()));
        if (i == this.scrubs.size()) {
            // Nth scrub
            System.out.println("Last Clip");
            // Keep video clip N->End
            duration = rawVideoMpgLength - scrub.end;
            offset = scrub.end;
            log(String.format("clip[%d] offset[%d] duration[%d]", i, scrub.start, duration));
            // Keep video clip 0->N
            FileUtil.extractMpgLastClip(rawVideoMPG, videoClip, offset, duration, progressBarListener);
        } else {
            // 0, 1, 2, ... N-1 scrubs
            duration = scrub.start - offset;
            log(String.format("clip[%d] offset[%d] duration[%d]", i, scrub.start, duration));
            // Keep video clip 0->N
            FileUtil.extractMpgClip(rawVideoMPG, videoClip, offset, duration, progressBarListener);
            videoClip.deleteOnExit();
        }
        // Save video clip to memory to merge later
        FileUtil.appendBinary(videoClip, finalClipMPG);
        // N->next start
        offset = scrub.end;
    }
    // Wait till we are finish downloading our libraries
    while (!downloadHandbrake.checkStatus(DownloadStatus.FINISHED)) {
        log("Waiting for the download of handbrake executable");
        TimeUtil.skipToMyLou(1);
    }
    handbrake = downloadHandbrake.getFile();
    LibraryUtil.chmod("777", handbrake);
    progressBarListener.setId(FFMpegProgressBarListener.HANDBRAKE);
    finalClipMP4 = FileUtil.convertMpgToMp4(finalClipMPG, progressBarListener);
    progressBarListener.setId(FFMpegProgressBarListener.FFMPEG);
    FileUtil.copyTo(rawVideoMP4, rawVideoMP4Backup);
    if (!rawVideoMP4.delete()) {
        log("Could not delete" + rawVideoMP4.getAbsolutePath() + " does not exists");
    }
    if (!finalClipMP4.renameTo(rawVideoMP4)) {
        log("Could not rename" + finalClipMP4.getAbsolutePath() + " to " + rawVideoMP4.getAbsolutePath() + "does not exists");
    }
    this.isScrubbing = false;
    log("Done");
}
Also used : DownloadThread(com.bixly.pastevid.download.DownloadThread) File(java.io.File)

Example 2 with DownloadThread

use of com.bixly.pastevid.download.DownloadThread in project screenbird by adamhub.

the class RecorderPanel method prepareVideo.

/**
     * Processes all images captured to create a Quicktime movie.
     */
public void prepareVideo() {
    this.showUploadForm();
    this.setEnableUploadForm(false);
    this.btnUpload.setEnabled(true);
    this.showUploadMessage("Processing Video...", MESSAGE_INFO);
    Boolean movieCreated = false;
    JFrame jf = (JFrame) RecorderPanel.this.getTopLevelAncestor();
    jf.setTitle("Screen Recorder - Processing Video...");
    // Place in encoding state
    this.isEncoding = true;
    this.pbEncoding.setIndeterminate(true);
    this.pbEncoding.setStringPainted(false);
    this.showUploadMessage("Rendering Video...", MESSAGE_INFO);
    // Converts screen shots to video file
    try {
        movieCreated = recorder.compileVideo();
    } catch (Exception e) {
        log(e);
    }
    // Converts audio clips to single audio file
    if (recorder.hasAudioToCompile()) {
        this.showUploadMessage("Rendering Audio...", MESSAGE_INFO);
        recorder.compileAudio();
        while (recorder.isWaitingForAudio()) {
            // we need to wait for the audio file.
            TimeUtil.skipToMyLou(0.1);
        }
    }
    // Check for ffmpeg executable on the computer
    // Download if not yet available
    DownloadThread ffmpegDownload = DownloadManager.getInstance().getDownload(Settings.getFFMpegExecutable());
    if (!ffmpegDownload.checkStatus(DownloadStatus.FINISHED)) {
        this.pbEncoding.setIndeterminate(true);
        this.pbEncoding.setStringPainted(false);
        this.showUploadMessage("Preparing Files", MESSAGE_INFO);
    }
    // Wait for ffmpeg download to finish
    while (!ffmpegDownload.checkStatus(DownloadStatus.FINISHED)) {
        TimeUtil.skipToMyLouMS(500L);
        log("Current Status :" + ffmpegDownload.getStatus());
    }
    this.pbEncoding.setIndeterminate(false);
    this.pbEncoding.setStringPainted(true);
    jf.setTitle("Screen Recorder - Done.");
    // Encode video/audio
    if (movieCreated) {
        try {
            int numOfTasks = 1;
            // Assign number of tasks via the operating system of the client
            if (recorder.hasPreviousCompiledVideo()) {
                numOfTasks = (MediaUtil.osIsMac() || MediaUtil.osIsUnix()) ? 5 : 4;
            }
            if (this.scrubManager.isVideoEdited()) {
                numOfTasks += (4 + this.scrubManager.getScrubs().size());
            }
            // Set up encoding progress bar
            this.pbEncodingListener = new FFMpegProgressBarListener(pbEncoding, numOfTasks, FFMpegProgressBarListener.FFMPEG);
            this.showUploadMessage("Encoding Video...", MESSAGE_INFO);
            // Encode the video
            this.recordingOutput = FileUtil.encodeVideoMp4(recorder.getFile(), recorder.getOffset(), recorder.getBitRateCompresion(), this.pbEncodingListener);
            // Append to previously rendered video if continuing
            if (recorder.hasPreviousCompiledVideo()) {
                this.recordingOutput = FileUtil.mergeVideoMp4(recorder.getPrevVideoFileName(), this.recordingOutput, this.pbEncodingListener);
            }
            this.recorder.setResultVideoPath(this.recordingOutput);
            log("Compiled Video Location: " + this.recordingOutput);
        } catch (FileNotFoundException ex) {
            log(ex);
        } catch (IOException ex) {
            log(ex);
        } catch (UnsupportedBitRateCompression e) {
            log(e);
        }
        // Disable scrub feature for resuming previous recordings
        if (this.scrubManager != null && this.scrubManager.isVideoEdited() && !this.recorder.hasPreviousCompiledVideo()) {
            // Give it a few seconds for the video to finish encoding
            TimeUtil.skipToMyLou(3);
            this.scrubManager.scrubVideo(this.recordingOutput, this.pbEncodingListener);
        }
        this.setEnableUploadForm(true);
        // Mark applet as a non-encoding state
        this.isEncoding = false;
        // Start uploading copy if auto-upload is checked
        if (this.chkAutoUpload.isSelected()) {
            // Disable checkbox since is useless after encoding is done
            this.chkAutoUpload.setEnabled(false);
            this.btnUpload.setEnabled(false);
            FileUtil.removeMarker(UPLOAD_ON_ENCODE);
            // Wait a few seconds for client's system to catch up
            TimeUtil.skipToMyLou(5);
            uploadCopy();
        }
        // Set progress bar to 100%
        this.pbEncoding.setValue(pbEncoding.getMaximum());
        this.showUploadMessage("Ready for upload", MESSAGE_OK);
    } else {
        this.showRecorderMessage("Error processing video.", MESSAGE_ERROR);
        log("Movie not compiled properly.");
    }
}
Also used : DownloadThread(com.bixly.pastevid.download.DownloadThread) JFrame(javax.swing.JFrame) FileNotFoundException(java.io.FileNotFoundException) UnsupportedBitRateCompression(com.bixly.pastevid.recorders.UnsupportedBitRateCompression) IOException(java.io.IOException) MissingResourceException(java.util.MissingResourceException) FileNotFoundException(java.io.FileNotFoundException) LineUnavailableException(javax.sound.sampled.LineUnavailableException) IOException(java.io.IOException) FFMpegProgressBarListener(com.bixly.pastevid.screencap.components.progressbar.FFMpegProgressBarListener)

Example 3 with DownloadThread

use of com.bixly.pastevid.download.DownloadThread in project screenbird by adamhub.

the class FFMPEGExecutor method execute.

/**
     * Executes the ffmpeg process with the previous given arguments.
     * OutputLog boolean is used for tracking progress of each ffmpeg
     * execution. True if you want to catch and parse the data here
     * in execte(), of false if you are going to manually catch the 
     * output in another function
     * 
     * @throws IOException
     *             If the process call fails.
     */
public void execute(boolean outputLog) throws IOException {
    DownloadThread downloadFFMpeg = DownloadManager.getInstance().getDownload(Settings.getFFMpegExecutable());
    //Last check for FFMpeg to see if it is finished downloading
    while (!downloadFFMpeg.checkStatus(DownloadStatus.FINISHED)) {
        log("Waiting for ffmpeg to finish downloading");
        TimeUtil.skipToMyLouMS(500L);
    }
    int argsSize = args.size();
    String[] cmd = new String[argsSize + 1];
    cmd[0] = this.ffmpegExecutablePath;
    StringBuilder str = new StringBuilder();
    for (int i = 0; i < argsSize; i++) {
        cmd[i + 1] = (String) args.get(i);
        str.append(args.get(i));
        str.append(" ");
    }
    log(str.toString());
    Runtime runtime = Runtime.getRuntime();
    ffmpeg = runtime.exec(cmd);
    ffmpegKiller = new ProcessKiller(ffmpeg, "FFmpeg Process Killer");
    runtime.addShutdownHook(ffmpegKiller);
    inputStream = ffmpeg.getInputStream();
    outputStream = ffmpeg.getOutputStream();
    errorStream = ffmpeg.getErrorStream();
    if (this.ffmpegListener != null && outputLog) {
        String line = "";
        InputStreamReader isr = new InputStreamReader(errorStream);
        BufferedReader br = new BufferedReader(isr);
        while ((line = br.readLine()) != null) {
            //log(line);
            parseTimeInfo(line);
            if (Settings.PRINT_EXEC_TO_CONSOLE) {
                System.out.println(line);
            }
        }
        //Complete task
        this.ffmpegListener.taskComplete();
    }
}
Also used : DownloadThread(com.bixly.pastevid.download.DownloadThread) InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader)

Aggregations

DownloadThread (com.bixly.pastevid.download.DownloadThread)3 UnsupportedBitRateCompression (com.bixly.pastevid.recorders.UnsupportedBitRateCompression)1 FFMpegProgressBarListener (com.bixly.pastevid.screencap.components.progressbar.FFMpegProgressBarListener)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 MissingResourceException (java.util.MissingResourceException)1 LineUnavailableException (javax.sound.sampled.LineUnavailableException)1 JFrame (javax.swing.JFrame)1