Package net.pms.io

Examples of net.pms.io.ProcessWrapperImpl


    params.maxBufferSize = 1;
    params.stdin = media.getPush();
    params.noexitcheck = true; // not serious if anything happens during the thumbnailer

    // true: consume stderr on behalf of the caller i.e. parse()
    final ProcessWrapperImpl pw = new ProcessWrapperImpl(args, params, false, true);

    // FAILSAFE
    parsing = true;
    Runnable r = new Runnable() {
      @Override
      public void run() {
        try {
          Thread.sleep(10000);
          ffmpeg_failure = true;
        } catch (InterruptedException e) { }

        pw.stopProcess();
        parsing = false;
      }
    };

    Thread failsafe = new Thread(r, "FFmpeg Thumbnail Failsafe");
    failsafe.start();
    pw.runInSameThread();
    parsing = false;;
    return pw;
  }
View Full Code Here


    params.workDir = configuration.getTempFolder();
    params.maxBufferSize = 1;
    params.stdin = media.getPush();
    params.log = true;
    params.noexitcheck = true; // not serious if anything happens during the thumbnailer
    final ProcessWrapperImpl pw = new ProcessWrapperImpl(args, params);

    // FAILSAFE
    parsing = true;
    Runnable r = new Runnable() {
      @Override
      public void run() {
        try {
          Thread.sleep(3000);
        } catch (InterruptedException e) { }

        pw.stopProcess();
        parsing = false;
      }
    };

    Thread failsafe = new Thread(r, "MPlayer Thumbnail Failsafe");
    failsafe.start();
    pw.runInSameThread();
    parsing = false;
    return pw;
  }
View Full Code Here

        size = file.length();
      } else {
        size = inputFile.getSize();
      }

      ProcessWrapperImpl pw = null;
      boolean ffmpeg_parsing = true;

      if (type == Format.AUDIO || ext instanceof AudioAsVideo) {
        ffmpeg_parsing = false;
        DLNAMediaAudio audio = new DLNAMediaAudio();

        if (file != null) {
          try {
            AudioFile af = AudioFileIO.read(file);
            AudioHeader ah = af.getAudioHeader();

            if (ah != null && !thumbOnly) {
              int length = ah.getTrackLength();
              int rate = ah.getSampleRateAsNumber();

              if (ah.getEncodingType().toLowerCase().contains("flac 24")) {
                audio.setBitsperSample(24);
              }

              audio.setSampleFrequency("" + rate);
              durationSec = (double) length;
              bitrate = (int) ah.getBitRateAsNumber();
              audio.getAudioProperties().setNumberOfChannels(2);

              if (ah.getChannels() != null && ah.getChannels().toLowerCase().contains("mono")) {
                audio.getAudioProperties().setNumberOfChannels(1);
              } else if (ah.getChannels() != null && ah.getChannels().toLowerCase().contains("stereo")) {
                audio.getAudioProperties().setNumberOfChannels(2);
              } else if (ah.getChannels() != null) {
                audio.getAudioProperties().setNumberOfChannels(Integer.parseInt(ah.getChannels()));
              }

              audio.setCodecA(ah.getEncodingType().toLowerCase());

              if (audio.getCodecA().contains("(windows media")) {
                audio.setCodecA(audio.getCodecA().substring(0, audio.getCodecA().indexOf("(windows media")).trim());
              }
            }

            Tag t = af.getTag();

            if (t != null) {
              if (t.getArtworkList().size() > 0) {
                thumb = t.getArtworkList().get(0).getBinaryData();
              } else {
                if (configuration.getAudioThumbnailMethod() > 0) {
                  thumb =
                    CoverUtil.get().getThumbnailFromArtistAlbum(
                      configuration.getAudioThumbnailMethod() == 1 ?
                        CoverUtil.AUDIO_AMAZON :
                        CoverUtil.AUDIO_DISCOGS,
                      audio.getArtist(), audio.getAlbum()
                    );
                }
              }

              if (!thumbOnly) {
                audio.setAlbum(t.getFirst(FieldKey.ALBUM));
                audio.setArtist(t.getFirst(FieldKey.ARTIST));
                audio.setSongname(t.getFirst(FieldKey.TITLE));
                String y = t.getFirst(FieldKey.YEAR);

                try {
                  if (y.length() > 4) {
                    y = y.substring(0, 4);
                  }
                  audio.setYear(Integer.parseInt(((y != null && y.length() > 0) ? y : "0")));
                  y = t.getFirst(FieldKey.TRACK);
                  audio.setTrack(Integer.parseInt(((y != null && y.length() > 0) ? y : "1")));
                  audio.setGenre(t.getFirst(FieldKey.GENRE));
                } catch (NumberFormatException | KeyNotFoundException e) {
                  LOGGER.debug("Error parsing unimportant metadata: " + e.getMessage());
                }
              }
            }
          } catch (CannotReadException | IOException | TagException | ReadOnlyFileException | InvalidAudioFrameException | NumberFormatException | KeyNotFoundException e) {
            LOGGER.debug("Error parsing audio file: {} - {}", e.getMessage(), e.getCause() != null ? e.getCause().getMessage() : "");
            ffmpeg_parsing = false;
          }

          if (audio.getSongname() == null || audio.getSongname().length() == 0) {
            audio.setSongname(file.getName());
          }

          if (!ffmpeg_parsing) {
            audioTracks.add(audio);
          }
        }
      }

      if (type == Format.IMAGE && file != null) {
        try {
          ffmpeg_parsing = false;
          ImageInfo info = Sanselan.getImageInfo(file);
          width = info.getWidth();
          height = info.getHeight();
          bitsPerPixel = info.getBitsPerPixel();
          String formatName = info.getFormatName();

          if (formatName.startsWith("JPEG")) {
            codecV = "jpg";
            IImageMetadata meta = Sanselan.getMetadata(file);

            if (meta != null && meta instanceof JpegImageMetadata) {
              JpegImageMetadata jpegmeta = (JpegImageMetadata) meta;
              TiffField tf = jpegmeta.findEXIFValue(TiffConstants.EXIF_TAG_MODEL);

              if (tf != null) {
                model = tf.getStringValue().trim();
              }

              tf = jpegmeta.findEXIFValue(TiffConstants.EXIF_TAG_EXPOSURE_TIME);
              if (tf != null) {
                exposure = (int) (1000 * tf.getDoubleValue());
              }

              tf = jpegmeta.findEXIFValue(TiffConstants.EXIF_TAG_ORIENTATION);
              if (tf != null) {
                orientation = tf.getIntValue();
              }

              tf = jpegmeta.findEXIFValue(TiffConstants.EXIF_TAG_ISO);
              if (tf != null) {
                // Galaxy Nexus jpg pictures may contain multiple values, take the first
                int[] isoValues = tf.getIntArrayValue();
                iso = isoValues[0];
              }
            }
          } else if (formatName.startsWith("PNG")) {
            codecV = "png";
          } else if (formatName.startsWith("GIF")) {
            codecV = "gif";
          } else if (formatName.startsWith("TIF")) {
            codecV = "tiff";
          }

          container = codecV;
        } catch (ImageReadException | IOException e) {
          LOGGER.info("Error parsing image ({}) with Sanselan, switching to FFmpeg.", file.getAbsolutePath());
        }
        if (configuration.getImageThumbnailsEnabled() && gen_thumb) {
          LOGGER.trace("Creating (temporary) thumbnail: {}", file.getName());

          // Create the thumbnail image using the Thumbnailator library
          try {
            ByteArrayOutputStream out = new ByteArrayOutputStream()
            Thumbnails.of(file)
                .size(320, 180)
                .outputFormat("JPEG")
                .outputQuality(1.0f)
                .toOutputStream(out);

            thumb = out.toByteArray();
          } catch (IOException | IllegalArgumentException | IllegalStateException e) {
            LOGGER.debug("Error generating thumbnail for: " + file.getName());
            LOGGER.debug("The full error was: " + e);
          }
        }
      }

      if (ffmpeg_parsing) {
        if (!thumbOnly || !configuration.isUseMplayerForVideoThumbs()) {
          pw = getFFMpegThumbnail(inputFile, resume);
        }

        boolean dvrms = false;
        String input = "-";

        if (file != null) {
          input = ProcessUtil.getShortFileNameIfWideChars(file.getAbsolutePath());
          dvrms = file.getAbsolutePath().toLowerCase().endsWith("dvr-ms");
        }

        if (pw != null && !ffmpeg_failure && !thumbOnly) {
          parseFFmpegInfo(pw.getResults(), input);
        }

        if (
          !thumbOnly &&
          container != null &&
          file != null &&
          container.equals("mpegts") &&
          isH264() &&
          getDurationInSeconds() == 0
        ) {
          // Parse the duration
          try {
            int length = MpegUtil.getDurationFromMpeg(file);
            if (length > 0) {
              durationSec = (double) length;
            }
          } catch (IOException e) {
            LOGGER.trace("Error retrieving length: " + e.getMessage());
          }
        }

        if (configuration.isUseMplayerForVideoThumbs() && type == Format.VIDEO && !dvrms) {
          try {
            getMplayerThumbnail(inputFile, resume);
            String frameName = "" + inputFile.hashCode();
            frameName = configuration.getTempFolder() + "/mplayer_thumbs/" + frameName + "00000001/00000001.jpg";
            frameName = frameName.replace(',', '_');
            File jpg = new File(frameName);

            if (jpg.exists()) {
              try (InputStream is = new FileInputStream(jpg)) {
                int sz = is.available();

                if (sz > 0) {
                  thumb = new byte[sz];
                  is.read(thumb);
                }
              }

              if (!jpg.delete()) {
                jpg.deleteOnExit();
              }

              // Try and retry
              if (!jpg.getParentFile().delete() && !jpg.getParentFile().delete()) {
                LOGGER.debug("Failed to delete \"" + jpg.getParentFile().getAbsolutePath() + "\"");
              }
            }
          } catch (IOException e) {
            LOGGER.debug("Caught exception", e);
          }
        }

        if (type == Format.VIDEO && pw != null && thumb == null) {
          InputStream is;
          try {
            int sz = 0;
            is = pw.getInputStream(0);
            if (is != null) {
              sz = is.available();
              if (sz > 0) {
                thumb = new byte[sz];
                is.read(thumb);
View Full Code Here

    byte[][] returnData = new byte[2][];
    OutputParams params = new OutputParams(configuration);
    params.maxBufferSize = 1;
    params.stdin = f.getPush();

    final ProcessWrapperImpl pw = new ProcessWrapperImpl(cmdArray, params);

    Runnable r = new Runnable() {
      @Override
      public void run() {
        try {
          Thread.sleep(3000);
          ffmpeg_annexb_failure = true;
        } catch (InterruptedException e) { }
        pw.stopProcess();
      }
    };

    Thread failsafe = new Thread(r, "FFMpeg AnnexB Frame Header Failsafe");
    failsafe.start();
    pw.runInSameThread();

    if (ffmpeg_annexb_failure) {
      return null;
    }

    InputStream is;
    ByteArrayOutputStream baot = new ByteArrayOutputStream();

    try {
      is = pw.getInputStream(0);
      byte b[] = new byte[4096];
      int n;

      while ((n = is.read(b)) > 0) {
        baot.write(b, 0, n);
View Full Code Here

    cmdList.add(tempSubsFile.getAbsolutePath());

    String[] cmdArray = new String[cmdList.size()];
    cmdList.toArray(cmdArray);

    ProcessWrapperImpl pw = new ProcessWrapperImpl(cmdArray, params);
    pw.runInNewThread();

    try {
      pw.join(); // Wait until the conversion is finished
      pw.stopProcess(); // Avoid creating a pipe for this process and messing up with buffer progress bar
    } catch (InterruptedException e) {
      LOGGER.debug("Subtitles conversion finished wih error: " + e);
      return null;
    }
View Full Code Here

      media,
      params,
      cmdArray
    );

    ProcessWrapperImpl pw = new ProcessWrapperImpl(cmdArray, params);
    pw.runInNewThread();

    return pw;
  }
View Full Code Here

  ) throws IOException {
    final String filename = dlna.getSystemName();
    setAudioAndSubs(filename, media, params);

    PipeIPCProcess ffVideoPipe;
    ProcessWrapperImpl ffVideo;

    PipeIPCProcess ffAudioPipe[] = null;
    ProcessWrapperImpl ffAudio[] = null;

    String fps = media.getValidFps(false);

    int width  = media.getWidth();
    int height = media.getHeight();
    if (width < 320 || height < 240) {
      width  = -1;
      height = -1;
    }

    String videoType = "V_MPEG4/ISO/AVC";
    if (media.getCodecV() != null && media.getCodecV().startsWith("mpeg2")) {
      videoType = "V_MPEG-2";
    }

    if (this instanceof TsMuxeRAudio && media.getFirstAudioTrack() != null) {
      ffVideoPipe = new PipeIPCProcess(System.currentTimeMillis() + "fakevideo", System.currentTimeMillis() + "videoout", false, true);

      String timeEndValue1 = "-t";
      String timeEndValue2 = "" + params.timeend;
      if (params.timeend < 1) {
        timeEndValue1 = "-y";
        timeEndValue2 = "-y";
      }

      String[] ffmpegLPCMextract = new String[] {
        configuration.getFfmpegPath(),
        timeEndValue1, timeEndValue2,
        "-loop", "1",
        "-i", "DummyInput.jpg",
        "-f", "h264",
        "-c:v", "libx264",
        "-level", "31",
        "-pix_fmt", "yuv420p",
        "-an",
        "-y",
        ffVideoPipe.getInputPipe()
      };

      videoType = "V_MPEG4/ISO/AVC";

      OutputParams ffparams = new OutputParams(configuration);
      ffparams.maxBufferSize = 1;
      ffVideo = new ProcessWrapperImpl(ffmpegLPCMextract, ffparams);

      if (
        filename.toLowerCase().endsWith(".flac") &&
        media.getFirstAudioTrack().getBitsperSample() >= 24 &&
        media.getFirstAudioTrack().getSampleRate() % 48000 == 0
      ) {
        ffAudioPipe = new PipeIPCProcess[1];
        ffAudioPipe[0] = new PipeIPCProcess(System.currentTimeMillis() + "flacaudio", System.currentTimeMillis() + "audioout", false, true);

        String[] flacCmd = new String[] {
          configuration.getFlacPath(),
          "--output-name=" + ffAudioPipe[0].getInputPipe(),
          "-d",
          "-f",
          "-F",
          filename
        };

        ffparams = new OutputParams(configuration);
        ffparams.maxBufferSize = 1;
        ffAudio = new ProcessWrapperImpl[1];
        ffAudio[0] = new ProcessWrapperImpl(flacCmd, ffparams);
      } else {
        ffAudioPipe = new PipeIPCProcess[1];
        ffAudioPipe[0] = new PipeIPCProcess(System.currentTimeMillis() + "mlpaudio", System.currentTimeMillis() + "audioout", false, true);
        String depth = "pcm_s16le";
        String rate = "48000";

        if (media.getFirstAudioTrack().getBitsperSample() >= 24) {
          depth = "pcm_s24le";
        }

        if (media.getFirstAudioTrack().getSampleRate() > 48000) {
          rate = "" + media.getFirstAudioTrack().getSampleRate();
        }

        String[] flacCmd = new String[] {
          configuration.getFfmpegPath(),
          "-i", filename,
          "-ar", rate,
          "-f", "wav",
          "-acodec", depth,
          "-y",
          ffAudioPipe[0].getInputPipe()
        };

        ffparams = new OutputParams(configuration);
        ffparams.maxBufferSize = 1;
        ffAudio = new ProcessWrapperImpl[1];
        ffAudio[0] = new ProcessWrapperImpl(flacCmd, ffparams);
      }
    } else {
      params.waitbeforestart = 5000;
      params.manageFastStart();

      String mencoderPath = configuration.getMencoderPath();

      ffVideoPipe = new PipeIPCProcess(System.currentTimeMillis() + "ffmpegvideo", System.currentTimeMillis() + "videoout", false, true);

      // Special handling for evo files
      String evoValue1 = "-quiet";
      String evoValue2 = "-quiet";
      if (filename.toLowerCase().endsWith(".evo")) {
        evoValue1 = "-psprobe";
        evoValue2 = "1000000";
      }

      String[] ffmpegLPCMextract = new String[] {
        mencoderPath,
        "-ss", params.timeseek > 0 ? "" + params.timeseek : "0",
        params.stdin != null ? "-" : filename,
        evoValue1, evoValue2,
        "-really-quiet",
        "-msglevel", "statusline=2",
        "-ovc", "copy",
        "-nosound",
        "-mc", "0",
        "-noskip",
        "-of", "rawvideo",
        "-o", ffVideoPipe.getInputPipe()
      };

      InputFile newInput = new InputFile();
      newInput.setFilename(filename);
      newInput.setPush(params.stdin);

      /**
       * Note: This logic is weird; on one hand we check if the renderer requires videos to be Level 4.1 or below, but then
       * the other function allows the video to exceed those limits.
       * In reality this won't cause problems since renderers typically don't support above 4.1 anyway - nor are many
       * videos encoded higher than that either - but it's worth acknowledging the logic discrepancy.
       */
      if (!media.isVideoWithinH264LevelLimits(newInput, params.mediaRenderer) && params.mediaRenderer.isH264Level41Limited()) {
        LOGGER.info("The video will not play or will show a black screen");
      }

      if (media.getH264AnnexB() != null && media.getH264AnnexB().length > 0) {
        StreamModifier sm = new StreamModifier();
        sm.setHeader(media.getH264AnnexB());
        sm.setH264AnnexB(true);
        ffVideoPipe.setModifier(sm);
      }

      OutputParams ffparams = new OutputParams(configuration);
      ffparams.maxBufferSize = 1;
      ffparams.stdin = params.stdin;
      ffVideo = new ProcessWrapperImpl(ffmpegLPCMextract, ffparams);

      int numAudioTracks = 1;

      if (media.getAudioTracksList() != null && media.getAudioTracksList().size() > 1 && configuration.isMuxAllAudioTracks()) {
        numAudioTracks = media.getAudioTracksList().size();
      }

      boolean singleMediaAudio = media.getAudioTracksList().size() <= 1;

      if (params.aid != null) {
        boolean ac3Remux;
        boolean dtsRemux;
        boolean encodedAudioPassthrough;
        boolean pcm;

        if (numAudioTracks <= 1) {
          ffAudioPipe = new PipeIPCProcess[numAudioTracks];
          ffAudioPipe[0] = new PipeIPCProcess(System.currentTimeMillis() + "ffmpegaudio01", System.currentTimeMillis() + "audioout", false, true);

          /**
           * Disable AC3 remux for stereo tracks with 384 kbits bitrate and PS3 renderer (PS3 FW bug?)
           *
           * Commented out until we can find a way to detect when a video has an audio track that switches from 2 to 6 channels
           * because MEncoder can't handle those files, which are very common these days.
          boolean ps3_and_stereo_and_384_kbits = params.aid != null &&
            (params.mediaRenderer.isPS3() && params.aid.getAudioProperties().getNumberOfChannels() == 2) &&
            (params.aid.getBitRate() > 370000 && params.aid.getBitRate() < 400000);
           */

          encodedAudioPassthrough = configuration.isEncodedAudioPassthrough() && params.aid.isNonPCMEncodedAudio() && params.mediaRenderer.isWrapEncodedAudioIntoPCM();
          ac3Remux = params.aid.isAC3() && configuration.isAudioRemuxAC3() && !encodedAudioPassthrough;
          dtsRemux = configuration.isAudioEmbedDtsInPcm() && params.aid.isDTS() && params.mediaRenderer.isDTSPlayable() && !encodedAudioPassthrough;

          pcm = configuration.isAudioUsePCM() &&
            media.isValidForLPCMTranscoding() &&
            (
              params.aid.isLossless() ||
              (params.aid.isDTS() && params.aid.getAudioProperties().getNumberOfChannels() <= 6) ||
              params.aid.isTrueHD() ||
              (
                !configuration.isMencoderUsePcmForHQAudioOnly() &&
                (
                  params.aid.isAC3() ||
                  params.aid.isMP3() ||
                  params.aid.isAAC() ||
                  params.aid.isVorbis() ||
                  // params.aid.isWMA() ||
                  params.aid.isMpegAudio()
                )
              )
            ) && params.mediaRenderer.isLPCMPlayable();

          int channels;
          if (ac3Remux) {
            channels = params.aid.getAudioProperties().getNumberOfChannels(); // AC-3 remux
          } else if (dtsRemux || encodedAudioPassthrough) {
            channels = 2;
          } else if (pcm) {
            channels = params.aid.getAudioProperties().getNumberOfChannels();
          } else {
            channels = configuration.getAudioChannelCount(); // 5.1 max for AC-3 encoding
          }

          if (!ac3Remux && (dtsRemux || pcm || encodedAudioPassthrough)) {
            // DTS remux or LPCM
            StreamModifier sm = new StreamModifier();
            sm.setPcm(pcm);
            sm.setDtsEmbed(dtsRemux);
            sm.setEncodedAudioPassthrough(encodedAudioPassthrough);
            sm.setNbChannels(channels);
            sm.setSampleFrequency(params.aid.getSampleRate() < 48000 ? 48000 : params.aid.getSampleRate());
            sm.setBitsPerSample(16);

            String mixer = null;

            if (pcm && !dtsRemux && !encodedAudioPassthrough) {
              mixer = getLPCMChannelMappingForMencoder(params.aid);
            }

            ffmpegLPCMextract = new String[] {
              mencoderPath,
              "-ss", params.timeseek > 0 ? "" + params.timeseek : "0",
              params.stdin != null ? "-" : filename,
              evoValue1, evoValue2,
              "-really-quiet",
              "-msglevel", "statusline=2",
              "-channels", "" + sm.getNbChannels(),
              "-ovc", "copy",
              "-of", "rawaudio",
              "-mc", sm.isDtsEmbed() || sm.isEncodedAudioPassthrough() ? "0.1" : "0",
              "-noskip",
              "-oac", sm.isDtsEmbed() || sm.isEncodedAudioPassthrough() ? "copy" : "pcm",
              isNotBlank(mixer) ? "-af" : "-quiet", isNotBlank(mixer) ? mixer : "-quiet",
              singleMediaAudio ? "-quiet" : "-aid", singleMediaAudio ? "-quiet" : ("" + params.aid.getId()),
              "-srate", "48000",
              "-o", ffAudioPipe[0].getInputPipe()
            };

            // Use PCM trick when media renderer does not support DTS in MPEG
            if (!params.mediaRenderer.isMuxDTSToMpeg()) {
              ffAudioPipe[0].setModifier(sm);
            }
          } else {
            // AC-3 remux or encoding
            ffmpegLPCMextract = new String[] {
              mencoderPath,
              "-ss", params.timeseek > 0 ? "" + params.timeseek : "0",
              params.stdin != null ? "-" : filename,
              evoValue1, evoValue2,
              "-really-quiet",
              "-msglevel", "statusline=2",
              "-channels", "" + channels,
              "-ovc", "copy",
              "-of", "rawaudio",
              "-mc", "0",
              "-noskip",
              "-oac", (ac3Remux) ? "copy" : "lavc",
              params.aid.isAC3() ? "-fafmttag" : "-quiet", params.aid.isAC3() ? "0x2000" : "-quiet",
              "-lavcopts", "acodec=" + (configuration.isMencoderAc3Fixed() ? "ac3_fixed" : "ac3") + ":abitrate=" + CodecUtil.getAC3Bitrate(configuration, params.aid),
              "-af", "lavcresample=48000",
              "-srate", "48000",
              singleMediaAudio ? "-quiet" : "-aid", singleMediaAudio ? "-quiet" : ("" + params.aid.getId()),
              "-o", ffAudioPipe[0].getInputPipe()
            };
          }

          ffparams = new OutputParams(configuration);
          ffparams.maxBufferSize = 1;
          ffparams.stdin = params.stdin;
          ffAudio = new ProcessWrapperImpl[numAudioTracks];
          ffAudio[0] = new ProcessWrapperImpl(ffmpegLPCMextract, ffparams);
        } else {
          ffAudioPipe = new PipeIPCProcess[numAudioTracks];
          ffAudio = new ProcessWrapperImpl[numAudioTracks];
          for (int i = 0; i < media.getAudioTracksList().size(); i++) {
            DLNAMediaAudio audio = media.getAudioTracksList().get(i);
            ffAudioPipe[i] = new PipeIPCProcess(System.currentTimeMillis() + "ffmpeg" + i, System.currentTimeMillis() + "audioout" + i, false, true);

            /**
             * Disable AC3 remux for stereo tracks with 384 kbits bitrate and PS3 renderer (PS3 FW bug?)
             *
             * Commented out until we can find a way to detect when a video has an audio track that switches from 2 to 6 channels
             * because MEncoder can't handle those files, which are very common these days.
            boolean ps3_and_stereo_and_384_kbits = params.aid != null &&
              (params.mediaRenderer.isPS3() && params.aid.getAudioProperties().getNumberOfChannels() == 2) &&
              (params.aid.getBitRate() > 370000 && params.aid.getBitRate() < 400000);
             */

            encodedAudioPassthrough = configuration.isEncodedAudioPassthrough() && params.aid.isNonPCMEncodedAudio() && params.mediaRenderer.isWrapEncodedAudioIntoPCM();
            ac3Remux = audio.isAC3() && configuration.isAudioRemuxAC3() && !encodedAudioPassthrough;
            dtsRemux = configuration.isAudioEmbedDtsInPcm() && audio.isDTS() && params.mediaRenderer.isDTSPlayable() && !encodedAudioPassthrough;

            pcm = configuration.isAudioUsePCM() &&
              media.isValidForLPCMTranscoding() &&
              (
                audio.isLossless() ||
                (audio.isDTS() && audio.getAudioProperties().getNumberOfChannels() <= 6) ||
                audio.isTrueHD() ||
                (
                  !configuration.isMencoderUsePcmForHQAudioOnly() &&
                  (
                    audio.isAC3() ||
                    audio.isMP3() ||
                    audio.isAAC() ||
                    audio.isVorbis() ||
                    // audio.isWMA() ||
                    audio.isMpegAudio()
                  )
                )
              ) && params.mediaRenderer.isLPCMPlayable();

            int channels;
            if (ac3Remux) {
              channels = audio.getAudioProperties().getNumberOfChannels(); // AC-3 remux
            } else if (dtsRemux || encodedAudioPassthrough) {
              channels = 2;
            } else if (pcm) {
              channels = audio.getAudioProperties().getNumberOfChannels();
            } else {
              channels = configuration.getAudioChannelCount(); // 5.1 max for AC-3 encoding
            }

            if (!ac3Remux && (dtsRemux || pcm || encodedAudioPassthrough)) {
              // DTS remux or LPCM
              StreamModifier sm = new StreamModifier();
              sm.setPcm(pcm);
              sm.setDtsEmbed(dtsRemux);
              sm.setEncodedAudioPassthrough(encodedAudioPassthrough);
              sm.setNbChannels(channels);
              sm.setSampleFrequency(audio.getSampleRate() < 48000 ? 48000 : audio.getSampleRate());
              sm.setBitsPerSample(16);
              if (!params.mediaRenderer.isMuxDTSToMpeg()) {
                ffAudioPipe[i].setModifier(sm);
              }

              String mixer = null;
              if (pcm && !dtsRemux && !encodedAudioPassthrough) {
                mixer = getLPCMChannelMappingForMencoder(audio);
              }

              ffmpegLPCMextract = new String[]{
                mencoderPath,
                "-ss", params.timeseek > 0 ? "" + params.timeseek : "0",
                params.stdin != null ? "-" : filename,
                evoValue1, evoValue2,
                "-really-quiet",
                "-msglevel", "statusline=2",
                "-channels", "" + sm.getNbChannels(),
                "-ovc", "copy",
                "-of", "rawaudio",
                "-mc", sm.isDtsEmbed() || sm.isEncodedAudioPassthrough() ? "0.1" : "0",
                "-noskip",
                "-oac", sm.isDtsEmbed() || sm.isEncodedAudioPassthrough() ? "copy" : "pcm",
                isNotBlank(mixer) ? "-af" : "-quiet", isNotBlank(mixer) ? mixer : "-quiet",
                singleMediaAudio ? "-quiet" : "-aid", singleMediaAudio ? "-quiet" : ("" + audio.getId()),
                "-srate", "48000",
                "-o", ffAudioPipe[i].getInputPipe()
              };
            } else {
              // AC-3 remux or encoding
              ffmpegLPCMextract = new String[]{
                mencoderPath,
                "-ss", params.timeseek > 0 ? "" + params.timeseek : "0",
                params.stdin != null ? "-" : filename,
                evoValue1, evoValue2,
                "-really-quiet",
                "-msglevel", "statusline=2",
                "-channels", "" + channels,
                "-ovc", "copy",
                "-of", "rawaudio",
                "-mc", "0",
                "-noskip",
                "-oac", (ac3Remux) ? "copy" : "lavc",
                audio.isAC3() ? "-fafmttag" : "-quiet", audio.isAC3() ? "0x2000" : "-quiet",
                "-lavcopts", "acodec=" + (configuration.isMencoderAc3Fixed() ? "ac3_fixed" : "ac3") + ":abitrate=" + CodecUtil.getAC3Bitrate(configuration, audio),
                "-af", "lavcresample=48000",
                "-srate", "48000",
                singleMediaAudio ? "-quiet" : "-aid", singleMediaAudio ? "-quiet" : ("" + audio.getId()),
                "-o", ffAudioPipe[i].getInputPipe()
              };
            }

            ffparams = new OutputParams(configuration);
            ffparams.maxBufferSize = 1;
            ffparams.stdin = params.stdin;
            ffAudio[i] = new ProcessWrapperImpl(ffmpegLPCMextract, ffparams);
          }
        }
      }
    }

    File f = new File(configuration.getTempFolder(), "pms-tsmuxer.meta");
    params.log = false;
    try (PrintWriter pw = new PrintWriter(f)) {
      pw.print("MUXOPT --no-pcr-on-video-pid");
      pw.print(" --new-audio-pes");
      pw.print(" --no-asyncio");
      pw.print(" --vbr");
      pw.println(" --vbv-len=500");

      String videoparams = "level=4.1, insertSEI, contSPS, track=1";
      if (this instanceof TsMuxeRAudio) {
        videoparams = "track=224";
      }
      if (configuration.isFix25FPSAvMismatch()) {
        fps = "25";
      }
      pw.println(videoType + ", \"" + ffVideoPipe.getOutputPipe() + "\", " + (fps != null ? ("fps=" + fps + ", ") : "") + (width != -1 ? ("video-width=" + width + ", ") : "") + (height != -1 ? ("video-height=" + height + ", ") : "") + videoparams);

      if (ffAudioPipe != null && ffAudioPipe.length == 1) {
        String timeshift = "";
        boolean ac3Remux;
        boolean dtsRemux;
        boolean encodedAudioPassthrough;
        boolean pcm;

        /**
         * Disable AC3 remux for stereo tracks with 384 kbits bitrate and PS3 renderer (PS3 FW bug?)
         *
         * Commented out until we can find a way to detect when a video has an audio track that switches from 2 to 6 channels
         * because MEncoder can't handle those files, which are very common these days.
        boolean ps3_and_stereo_and_384_kbits = params.aid != null &&
          (params.mediaRenderer.isPS3() && params.aid.getAudioProperties().getNumberOfChannels() == 2) &&
          (params.aid.getBitRate() > 370000 && params.aid.getBitRate() < 400000);
         */

        encodedAudioPassthrough = configuration.isEncodedAudioPassthrough() && params.aid.isNonPCMEncodedAudio() && params.mediaRenderer.isWrapEncodedAudioIntoPCM();
        ac3Remux = params.aid.isAC3() && configuration.isAudioRemuxAC3() && !encodedAudioPassthrough;
        dtsRemux = configuration.isAudioEmbedDtsInPcm() && params.aid.isDTS() && params.mediaRenderer.isDTSPlayable() && !encodedAudioPassthrough;

        pcm = configuration.isAudioUsePCM() &&
          media.isValidForLPCMTranscoding() &&
          (
            params.aid.isLossless() ||
            (params.aid.isDTS() && params.aid.getAudioProperties().getNumberOfChannels() <= 6) ||
            params.aid.isTrueHD() ||
            (
              !configuration.isMencoderUsePcmForHQAudioOnly() &&
              (
                params.aid.isAC3() ||
                params.aid.isMP3() ||
                params.aid.isAAC() ||
                params.aid.isVorbis() ||
                // params.aid.isWMA() ||
                params.aid.isMpegAudio()
              )
            )
          ) && params.mediaRenderer.isLPCMPlayable();
        String type = "A_AC3";
        if (ac3Remux) {
          // AC-3 remux takes priority
          type = "A_AC3";
        } else {
          if (pcm || this instanceof TsMuxeRAudio) {
            type = "A_LPCM";
          }
          if (encodedAudioPassthrough || this instanceof TsMuxeRAudio) {
            type = "A_LPCM";
          }
          if (dtsRemux || this instanceof TsMuxeRAudio) {
            type = "A_LPCM";
            if (params.mediaRenderer.isMuxDTSToMpeg()) {
              type = "A_DTS";
            }
          }
        }
        if (params.aid != null && params.aid.getAudioProperties().getAudioDelay() != 0 && params.timeseek == 0) {
          timeshift = "timeshift=" + params.aid.getAudioProperties().getAudioDelay() + "ms, ";
        }
        pw.println(type + ", \"" + ffAudioPipe[0].getOutputPipe() + "\", " + timeshift + "track=2");
      } else if (ffAudioPipe != null) {
        for (int i = 0; i < media.getAudioTracksList().size(); i++) {
          DLNAMediaAudio lang = media.getAudioTracksList().get(i);
          String timeshift = "";
          boolean ac3Remux;
          boolean dtsRemux;
          boolean encodedAudioPassthrough;
          boolean pcm;

          /**
           * Disable AC3 remux for stereo tracks with 384 kbits bitrate and PS3 renderer (PS3 FW bug?)
           *
           * Commented out until we can find a way to detect when a video has an audio track that switches from 2 to 6 channels
           * because MEncoder can't handle those files, which are very common these days.
          boolean ps3_and_stereo_and_384_kbits = params.aid != null &&
            (params.mediaRenderer.isPS3() && params.aid.getAudioProperties().getNumberOfChannels() == 2) &&
            (params.aid.getBitRate() > 370000 && params.aid.getBitRate() < 400000);
           */

          encodedAudioPassthrough = configuration.isEncodedAudioPassthrough() && params.aid.isNonPCMEncodedAudio() && params.mediaRenderer.isWrapEncodedAudioIntoPCM();
          ac3Remux = lang.isAC3() && configuration.isAudioRemuxAC3() && !encodedAudioPassthrough;
          dtsRemux = configuration.isAudioEmbedDtsInPcm() && lang.isDTS() && params.mediaRenderer.isDTSPlayable() && !encodedAudioPassthrough;

          pcm = configuration.isAudioUsePCM() &&
            media.isValidForLPCMTranscoding() &&
            (
              lang.isLossless() ||
              (lang.isDTS() && lang.getAudioProperties().getNumberOfChannels() <= 6) ||
              lang.isTrueHD() ||
              (
                !configuration.isMencoderUsePcmForHQAudioOnly() &&
                (
                  params.aid.isAC3() ||
                  params.aid.isMP3() ||
                  params.aid.isAAC() ||
                  params.aid.isVorbis() ||
                  // params.aid.isWMA() ||
                  params.aid.isMpegAudio()
                )
              )
            ) && params.mediaRenderer.isLPCMPlayable();
          String type = "A_AC3";
          if (ac3Remux) {
            // AC-3 remux takes priority
            type = "A_AC3";
          } else {
            if (pcm) {
              type = "A_LPCM";
            }
            if (encodedAudioPassthrough) {
              type = "A_LPCM";
            }
            if (dtsRemux) {
              type = "A_LPCM";
              if (params.mediaRenderer.isMuxDTSToMpeg()) {
                type = "A_DTS";
              }
            }
          }
          if (lang.getAudioProperties().getAudioDelay() != 0 && params.timeseek == 0) {
            timeshift = "timeshift=" + lang.getAudioProperties().getAudioDelay() + "ms, ";
          }
          pw.println(type + ", \"" + ffAudioPipe[i].getOutputPipe() + "\", " + timeshift + "track=" + (2 + i));
        }
      }
    }

    PipeProcess tsPipe = new PipeProcess(System.currentTimeMillis() + "tsmuxerout.ts");

    /**
     * Use the newer version of tsMuxeR on PS3 since other renderers
     * like Panasonic TVs don't always recognize the new output
     */
    String executable = executable();
    if (params.mediaRenderer.isPS3()) {
      executable = configuration.getTsmuxerNewPath();
    }

    String[] cmdArray = new String[]{
      executable,
      f.getAbsolutePath(),
      tsPipe.getInputPipe()
    };

    cmdArray = finalizeTranscoderArgs(
      filename,
      dlna,
      media,
      params,
      cmdArray
    );

    ProcessWrapperImpl p = new ProcessWrapperImpl(cmdArray, params);
    params.maxBufferSize = 100;
    params.input_pipes[0] = tsPipe;
    params.stdin = null;
    ProcessWrapper pipe_process = tsPipe.getPipeProcess();
    p.attachProcess(pipe_process);
    pipe_process.runInNewThread();

    try {
      Thread.sleep(50);
    } catch (InterruptedException e) {
    }
    tsPipe.deleteLater();

    ProcessWrapper ff_pipe_process = ffVideoPipe.getPipeProcess();
    p.attachProcess(ff_pipe_process);
    ff_pipe_process.runInNewThread();
    try {
      Thread.sleep(50);
    } catch (InterruptedException e) {
    }
    ffVideoPipe.deleteLater();

    p.attachProcess(ffVideo);
    ffVideo.runInNewThread();
    try {
      Thread.sleep(50);
    } catch (InterruptedException e) {
    }

    if (ffAudioPipe != null && params.aid != null) {
      for (int i = 0; i < ffAudioPipe.length; i++) {
        ff_pipe_process = ffAudioPipe[i].getPipeProcess();
        p.attachProcess(ff_pipe_process);
        ff_pipe_process.runInNewThread();
        try {
          Thread.sleep(50);
        } catch (InterruptedException e) {
        }
        ffAudioPipe[i].deleteLater();
        p.attachProcess(ffAudio[i]);
        ffAudio[i].runInNewThread();
      }
    }

    try {
      Thread.sleep(100);
    } catch (InterruptedException e) {
    }

    p.runInNewThread();
    return p;
  }
View Full Code Here

      frameName = "mplayer_thumbs:subdirs=\"" + frameName + "\"";
      frameName = frameName.replace(',', '_');
      cmd[10] = "jpeg:outdir=" + frameName;
    }
    params.log = true;
    final ProcessWrapperImpl pw = new ProcessWrapperImpl(cmd, params, true, false);
    Runnable r = new Runnable() {
      @Override
      public void run() {
        try {
          Thread.sleep(10000);
        } catch (InterruptedException e) {
        }
        pw.stopProcess();
      }
    };
    Thread failsafe = new Thread(r, "DVD ISO Title Failsafe");
    failsafe.start();
    pw.runInSameThread();
    List<String> lines = pw.getOtherResults();

    String duration = null;
    int nbsectors = 0;
    String fps = null;
    String aspect = null;
View Full Code Here

      "dvd://1"
    };
    OutputParams params = new OutputParams(configuration);
    params.maxBufferSize = 1;
    params.log = true;
    final ProcessWrapperImpl pw = new ProcessWrapperImpl(cmd, params, true, false);
    Runnable r = new Runnable() {
      @Override
      public void run() {
        try {
          Thread.sleep(10000);
        } catch (InterruptedException e) {
        }
        pw.stopProcess();
      }
    };
    Thread failsafe = new Thread(r, "DVDISO Failsafe");
    failsafe.start();
    pw.runInSameThread();
    List<String> lines = pw.getOtherResults();
    if (lines != null) {
      for (String line : lines) {
        if (line.startsWith("ID_DVD_TITLE_") && line.contains("_LENGTH")) {
          int rank = Integer.parseInt(line.substring(13, line.indexOf("_LENGT")));
          double duration = Double.parseDouble(line.substring(line.lastIndexOf("LENGTH=") + 7));
View Full Code Here

      if (file.getFile() != null) {
        cmdArray[3] = file.getFile().getAbsolutePath();
      }

      params.log = true;
      ProcessWrapperImpl pw = new ProcessWrapperImpl(cmdArray, params, true, false);
      pw.runInSameThread();

      List<String> list = pw.getOtherResults();
      for (String s : list) {
        if (s.startsWith("Thumb size:  ")) {
          String sz = s.substring(13);
          media.setWidth(Integer.parseInt(sz.substring(0, sz.indexOf('x')).trim()));
          media.setHeight(Integer.parseInt(sz.substring(sz.indexOf('x') + 1).trim()));
View Full Code Here

TOP

Related Classes of net.pms.io.ProcessWrapperImpl

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.