/** * start_transcode * * This is a rather complex function that starts the transcoding or * resampling of a song and returns the opened file handle. */ public static function start_transcode($song, $type = null, $bitrate = 0) { debug_event('stream.class.php', 'Starting transcode for {' . $song->file . '}. Type {' . $type . '}. Bitrate {' . $bitrate . '}...', 5); $transcode_settings = $song->get_transcode_settings($type); // Bail out early if we're unutterably broken if ($transcode_settings == false) { debug_event('stream', 'Transcode requested, but get_transcode_settings failed', 2); return false; } if ($bitrate == 0) { $sample_rate = self::get_allowed_bitrate($song); debug_event('stream', 'Configured bitrate is ' . $sample_rate, 5); // Validate the bitrate $sample_rate = self::validate_bitrate($sample_rate); } else { $sample_rate = $bitrate; } // Never upsample a song if ($song->type == $transcode_settings['format'] && $sample_rate * 1000 > $song->bitrate) { debug_event('stream', 'Clamping bitrate to avoid upsampling to ' . $sample_rate, 5); $sample_rate = self::validate_bitrate($song->bitrate / 1000); } debug_event('stream', 'Final transcode bitrate is ' . $sample_rate, 5); $song_file = scrub_arg($song->file); // Finalise the command line $command = $transcode_settings['command']; $string_map = array('%FILE%' => $song_file, '%SAMPLE%' => $sample_rate); foreach ($string_map as $search => $replace) { $command = str_replace($search, $replace, $command, $ret); if (!$ret) { debug_event('downsample', "{$search} not in downsample command", 5); } } debug_event('downsample', "Downsample command: {$command}", 3); $descriptors = array(1 => array('pipe', 'w')); if (strtoupper(substr(PHP_OS, 0, 3)) !== 'WIN') { // Windows doesn't like to provide stderr as a pipe $descriptors[2] = array('pipe', 'w'); } $process = proc_open($command, $descriptors, $pipes); return array('process' => $process, 'handle' => $pipes[1], 'stderr' => $pipes[2], 'format' => $transcode_settings['format']); }
public static function get_image_preview($media) { $image = null; $sec = $media->time >= 30 ? 30 : intval($media->time / 2); $frame = gmdate("H:i:s", $sec); if (AmpConfig::get('transcode_cmd') && AmpConfig::get('transcode_input') && AmpConfig::get('encode_get_image')) { $command = AmpConfig::get('transcode_cmd') . ' ' . AmpConfig::get('transcode_input') . ' ' . AmpConfig::get('encode_get_image'); $string_map = array('%FILE%' => scrub_arg($media->file), '%TIME%' => $frame); foreach ($string_map as $search => $replace) { $command = str_replace($search, $replace, $command, $ret); if (!$ret) { debug_event('stream', "{$search} not in transcode command", 5); } } $proc = self::start_process($command); if (is_resource($proc['handle'])) { $image = ''; do { $image .= fread($proc['handle'], 1024); } while (!feof($proc['handle'])); fclose($proc['handle']); } } else { debug_event('stream', 'Missing transcode_cmd / encode_get_image parameters to generate media preview.', 3); } return $image; }