Search in sources :

Example 1 with ProcessResult

use of org.apache.openmeetings.util.process.ProcessResult in project openmeetings by apache.

the class DocumentConverter method doJodConvert.

/**
 * Generates PDF using JOD Library (external library)
 *
 * @param in - file to convert
 * @param out - file to write result
 * @return - result of the conversion as {@link ProcessResult}
 */
public ProcessResult doJodConvert(File in, File out) {
    try {
        createOfficeManager(cfgDao.getString(CONFIG_PATH_OFFICE, null), man -> {
            OfficeDocumentConverter converter = new OfficeDocumentConverter(man);
            converter.convert(in, out);
        });
    } catch (Exception ex) {
        log.error("doJodConvert", ex);
        return new ProcessResult("doJodConvert", ex.getMessage(), ex);
    }
    return new ProcessResult("doJodConvert", "Document converted successfully", null).setExitCode(0);
}
Also used : ProcessResult(org.apache.openmeetings.util.process.ProcessResult) OfficeDocumentConverter(org.artofsolving.jodconverter.OfficeDocumentConverter)

Example 2 with ProcessResult

use of org.apache.openmeetings.util.process.ProcessResult in project openmeetings by apache.

the class ImageConverter method convertDocument.

/**
 * Converts PDF document to the series of images
 *
 * @param f - {@link FileItem} object to write number of pages and size
 * @param pdf - input PDF document
 * @param logs - logs of the conversion
 * @return - result of conversion
 * @throws IOException in case IO exception occurred
 */
public ProcessResultList convertDocument(FileItem f, File pdf, ProcessResultList logs) throws IOException {
    log.debug("convertDocument");
    String[] argv = new String[] { getPathToConvert(), "-density", getDpi(), pdf.getCanonicalPath(), "-quality", getQuality(), new File(pdf.getParentFile(), PAGE_TMPLT).getCanonicalPath() };
    ProcessResult res = ProcessHelper.executeScript("convert PDF to images", argv);
    logs.add(res);
    if (res.isOk()) {
        File[] pages = pdf.getParentFile().listFiles(fi -> fi.isFile() && fi.getName().startsWith(DOC_PAGE_PREFIX) && fi.getName().endsWith(EXTENSION_PNG));
        if (pages == null || pages.length == 0) {
            f.setCount(0);
        } else {
            f.setCount(pages.length);
            logs.add(initSize(f, pages[0], PNG_MIME_TYPE));
        }
    }
    return logs;
}
Also used : ProcessResult(org.apache.openmeetings.util.process.ProcessResult) StoredFile(org.apache.openmeetings.util.StoredFile) File(java.io.File) FileUtils.copyFile(org.apache.commons.io.FileUtils.copyFile)

Example 3 with ProcessResult

use of org.apache.openmeetings.util.process.ProcessResult in project openmeetings by apache.

the class ImageConverter method initSize.

private static ProcessResult initSize(BaseFileItem f, File img, String mime) {
    ProcessResult res = new ProcessResult();
    res.setProcess("get image dimensions :: " + f.getId());
    final Parser parser = new ImageParser();
    try (InputStream is = new FileInputStream(img)) {
        Metadata metadata = new Metadata();
        metadata.set(CONTENT_TYPE, mime);
        parser.parse(is, new DefaultHandler(), metadata, new ParseContext());
        f.setWidth(Integer.valueOf(metadata.get(TIFF.IMAGE_WIDTH)));
        f.setHeight(Integer.valueOf(metadata.get(TIFF.IMAGE_LENGTH)));
        res.setExitCode(ZERO);
    } catch (Exception e) {
        log.error("Error while getting dimensions", e);
        res.setError("Error while getting dimensions");
        res.setException(e.getMessage());
        res.setExitCode(-1);
    }
    return res;
}
Also used : ImageParser(org.apache.tika.parser.image.ImageParser) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ProcessResult(org.apache.openmeetings.util.process.ProcessResult) Metadata(org.apache.tika.metadata.Metadata) ParseContext(org.apache.tika.parser.ParseContext) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) Parser(org.apache.tika.parser.Parser) ImageParser(org.apache.tika.parser.image.ImageParser) DefaultHandler(org.xml.sax.helpers.DefaultHandler)

Example 4 with ProcessResult

use of org.apache.openmeetings.util.process.ProcessResult in project openmeetings by apache.

the class InterviewConverter method startConversion.

public void startConversion(Long id, boolean reconversion, ReConverterParams rcv) {
    Recording r = null;
    try {
        r = recordingDao.get(id);
        log.debug("recording {}", r.getId());
        if (Strings.isEmpty(r.getHash())) {
            r.setHash(UUID.randomUUID().toString());
        }
        r.setStatus(Recording.Status.CONVERTING);
        r = recordingDao.update(r);
        ProcessResultList logs = new ProcessResultList();
        List<File> waveFiles = new ArrayList<>();
        File streamFolder = getStreamFolder(r);
        List<RecordingMetaData> metaDataList = metaDataDao.getAudioMetaDataByRecording(r.getId());
        stripAudioFirstPass(r, logs, waveFiles, streamFolder, metaDataList);
        // Merge Wave to Full Length
        File streamFolderGeneral = getStreamsHibernateDir();
        File wav = new File(streamFolder, String.format("INTERVIEW_%s_FINAL_WAVE.wav", r.getId()));
        deleteFileIfExists(wav);
        if (waveFiles.isEmpty()) {
            // create default Audio to merge it.
            // strip to content length
            File outputWav = new File(streamFolderGeneral, "one_second.wav");
            // Calculate delta at beginning
            double deltaPadding = diffSeconds(r.getRecordEnd(), r.getRecordStart());
            String[] cmdSox = new String[] { getPathToSoX(), outputWav.getCanonicalPath(), wav.getCanonicalPath(), "pad", "0", String.valueOf(deltaPadding) };
            logs.add(ProcessHelper.executeScript("generateSampleAudio", cmdSox));
        } else if (waveFiles.size() == 1) {
            wav = waveFiles.get(0);
        } else {
            String[] soxArgs;
            if (reconversion) {
                soxArgs = mergeAudioToWaves(waveFiles, wav, metaDataList, rcv);
            } else {
                soxArgs = mergeAudioToWaves(waveFiles, wav);
            }
            logs.add(ProcessHelper.executeScript("mergeAudioToWaves", soxArgs));
        }
        // Default Image for empty interview video pods
        final File defaultInterviewImageFile = new File(streamFolderGeneral, "default_interview_image.png");
        if (!defaultInterviewImageFile.exists()) {
            throw new ConversionException("defaultInterviewImageFile does not exist!");
        }
        final int flvWidth = 320;
        final int flvHeight = 260;
        // Merge Audio with Video / Calculate resulting FLV
        String[] pods = new String[2];
        boolean found = false;
        for (RecordingMetaData meta : metaDataList) {
            File flv = getRecordingMetaData(r.getRoomId(), meta.getStreamName());
            Integer pod = meta.getInteriewPodId();
            if (flv.exists() && pod != null && pod > 0 && pod < 3) {
                String path = flv.getCanonicalPath();
                /*
					 * CHECK FILE:
					 * ffmpeg -i rec_316_stream_567_2013_08_28_11_51_45.flv -v error -f null file.null
					 */
                String[] args = new String[] { getPathToFFMPEG(), "-y", "-i", path, // only input files with video will be treated as video sources
                "-an", "-v", "error", "-f", "null", "file.null" };
                ProcessResult res = ProcessHelper.executeScript("checkFlvPod_" + pod, args, true);
                logs.add(res);
                if (res.isOk()) {
                    long diff = diff(meta.getRecordStart(), meta.getRecording().getRecordStart());
                    if (diff != 0L) {
                        // stub to add
                        // ffmpeg -y -loop 1 -i /home/solomax/work/openmeetings/branches/3.0.x/dist/red5/webapps/openmeetings/streams/hibernate/default_interview_image.jpg -filter_complex '[0:0]scale=320:260' -c:v libx264 -t 00:00:29.059 -pix_fmt yuv420p out.flv
                        File podFB = new File(streamFolder, String.format("%s_pod_%s_blank.flv", meta.getStreamName(), pod));
                        String podPB = podFB.getCanonicalPath();
                        String[] argsPodB = new String[] { // 
                        getPathToFFMPEG(), // 
                        "-y", // 
                        "-loop", // 
                        "1", // 
                        "-i", // 
                        defaultInterviewImageFile.getCanonicalPath(), // 
                        "-filter_complex", // 
                        String.format("[0:0]scale=%1$d:%2$d", flvWidth, flvHeight), // 
                        "-c:v", // 
                        "libx264", // 
                        "-t", // 
                        formatMillis(diff), // 
                        "-pix_fmt", // 
                        "yuv420p", podPB };
                        logs.add(ProcessHelper.executeScript("blankFlvPod_" + pod, argsPodB));
                        // ffmpeg -y -i out.flv -i rec_15_stream_4_2014_07_15_20_41_03.flv -filter_complex '[0:0]setsar=1/1[sarfix];[1:0]scale=320:260,setsar=1/1[scale];[sarfix] [scale] concat=n=2:v=1:a=0 [v]' -map '[v]'  output1.flv
                        File podF = new File(streamFolder, OmFileHelper.getName(meta.getStreamName() + "_pod_" + pod, EXTENSION_FLV));
                        String podP = podF.getCanonicalPath();
                        String[] argsPod = new String[] { // 
                        getPathToFFMPEG(), // 
                        "-y", // 
                        "-i", // 
                        podPB, // 
                        "-i", // 
                        path, // 
                        "-filter_complex", // 
                        String.format("[0:0]setsar=1/1[sarfix];[1:0]scale=%1$d:%2$d,setsar=1/1[scale];[sarfix] [scale] concat=n=2:v=1:a=0 [v]", flvWidth, flvHeight), // 
                        "-map", // 
                        "[v]", podP };
                        logs.add(ProcessHelper.executeScript("shiftedFlvPod_" + pod, argsPod));
                        pods[pod - 1] = podP;
                    } else {
                        pods[pod - 1] = path;
                    }
                }
                found = true;
            }
        }
        if (!found) {
            ProcessResult res = new ProcessResult();
            res.setProcess("CheckFlvFilesExists");
            res.setError("No valid pods found");
            res.setExitCode(-1);
            logs.add(res);
            return;
        }
        boolean shortest = false;
        List<String> args = new ArrayList<>();
        for (int i = 0; i < 2; ++i) {
            /*
				 * INSERT BLANK INSTEAD OF BAD PAD:
				 * ffmpeg -loop 1 -i default_interview_image.jpg -i rec_316_stream_569_2013_08_28_11_51_45.flv -filter_complex '[0:v]scale=320:260,pad=2*320:260[left];[1:v]scale=320:260[right];[left][right]overlay=main_w/2:0' -shortest -y out4.flv
				 *
				 * JUST MERGE:
				 * ffmpeg -i rec_316_stream_569_2013_08_28_11_51_45.flv -i rec_316_stream_569_2013_08_28_11_51_45.flv -filter_complex '[0:v]scale=320:260,pad=2*320:260[left];[1:v]scale=320:260[right];[left][right]overlay=main_w/2:0' -y out4.flv
				 */
            if (pods[i] == null) {
                shortest = true;
                args.add("-loop");
                args.add("1");
                args.add("-i");
                args.add(defaultInterviewImageFile.getCanonicalPath());
            } else {
                args.add("-i");
                args.add(pods[i]);
            }
        }
        args.add("-i");
        args.add(wav.getCanonicalPath());
        args.add("-filter_complex");
        args.add(String.format("[0:v]scale=%1$d:%2$d,pad=2*%1$d:%2$d[left];[1:v]scale=%1$d:%2$d[right];[left][right]overlay=main_w/2:0%3$s", flvWidth, flvHeight, shortest ? ":shortest=1" : ""));
        if (shortest) {
            args.add("-shortest");
        }
        args.add("-map");
        args.add("0:0");
        args.add("-map");
        args.add("1:0");
        args.add("-map");
        args.add("2:0");
        args.add("-qmax");
        args.add("1");
        args.add("-qmin");
        args.add("1");
        r.setWidth(2 * flvWidth);
        r.setHeight(flvHeight);
        String mp4path = convertToMp4(r, args, logs);
        postProcess(r, mp4path, logs, waveFiles);
    } catch (Exception err) {
        log.error("[startConversion]", err);
        r.setStatus(Recording.Status.ERROR);
    }
    recordingDao.update(r);
}
Also used : ArrayList(java.util.ArrayList) ProcessResult(org.apache.openmeetings.util.process.ProcessResult) IOException(java.io.IOException) ProcessResultList(org.apache.openmeetings.util.process.ProcessResultList) OmFileHelper.getRecordingMetaData(org.apache.openmeetings.util.OmFileHelper.getRecordingMetaData) RecordingMetaData(org.apache.openmeetings.db.entity.record.RecordingMetaData) Recording(org.apache.openmeetings.db.entity.record.Recording) File(java.io.File)

Example 5 with ProcessResult

use of org.apache.openmeetings.util.process.ProcessResult in project openmeetings by apache.

the class VideoConverter method convertVideo.

public void convertVideo(FileItem f, String ext, ProcessResultList logs) {
    try {
        File mp4 = f.getFile(EXTENSION_MP4);
        f.setType(Type.Video);
        String input = f.getFile(ext).getCanonicalPath();
        boolean sameExt = EXTENSION_MP4.equals(ext);
        Path tmp = null;
        if (sameExt) {
            // we should do in-place conversion
            tmp = Files.createTempFile("video", ".mp4");
            input = Files.move(mp4.toPath(), tmp, REPLACE_EXISTING).toFile().getCanonicalPath();
        }
        String[] args = new String[] { getPathToFFMPEG(), "-y", // 
        "-i", // 
        input, // 
        "-c:v", // 
        "h264", // 
        "-c:a", // 
        "libfaac", // 
        "-c:a", // 
        "libfdk_aac", // 
        "-pix_fmt", // 
        "yuv420p", mp4.getCanonicalPath() };
        ProcessResult res = ProcessHelper.executeScript("convert to MP4 :: " + f.getHash(), args);
        logs.add(res);
        if (sameExt && tmp != null) {
            if (res.isOk()) {
                Files.delete(tmp);
            } else {
                // conversion fails, need to move temp file back
                Files.move(tmp, mp4.toPath(), REPLACE_EXISTING);
            }
        }
        // Parse the width height from the FFMPEG output
        Dimension dim = getDimension(res.getError());
        f.setWidth(dim.getWidth());
        f.setHeight(dim.getHeight());
        convertToPng(f, mp4.getCanonicalPath(), logs);
    } catch (Exception err) {
        log.error("[convertToFLV]", err);
        logs.add(new ProcessResult("convertToMP4", err.getMessage(), err));
    }
}
Also used : Path(java.nio.file.Path) ProcessResult(org.apache.openmeetings.util.process.ProcessResult) File(java.io.File)

Aggregations

ProcessResult (org.apache.openmeetings.util.process.ProcessResult)8 File (java.io.File)5 IOException (java.io.IOException)2 FileUtils.copyFile (org.apache.commons.io.FileUtils.copyFile)2 StoredFile (org.apache.openmeetings.util.StoredFile)2 ProcessResultList (org.apache.openmeetings.util.process.ProcessResultList)2 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 Path (java.nio.file.Path)1 ArrayList (java.util.ArrayList)1 FileUtils.copyInputStreamToFile (org.apache.commons.io.FileUtils.copyInputStreamToFile)1 BaseFileItem (org.apache.openmeetings.db.entity.file.BaseFileItem)1 FileItem (org.apache.openmeetings.db.entity.file.FileItem)1 Recording (org.apache.openmeetings.db.entity.record.Recording)1 RecordingMetaData (org.apache.openmeetings.db.entity.record.RecordingMetaData)1 OmFileHelper.getRecordingMetaData (org.apache.openmeetings.util.OmFileHelper.getRecordingMetaData)1 Metadata (org.apache.tika.metadata.Metadata)1 ParseContext (org.apache.tika.parser.ParseContext)1 Parser (org.apache.tika.parser.Parser)1 ImageParser (org.apache.tika.parser.image.ImageParser)1