use of org.opencastproject.inspection.ffmpeg.api.VideoStreamMetadata in project opencast by opencast.
the class FFmpegAnalyzer method analyze.
@Override
public MediaContainerMetadata analyze(File media) throws MediaAnalyzerException {
if (binary == null)
throw new IllegalStateException("Binary is not set");
List<String> command = new ArrayList<>();
command.add("-show_format");
command.add("-show_streams");
if (accurateFrameCount)
command.add("-count_frames");
command.add("-of");
command.add("json");
command.add(media.getAbsolutePath().replaceAll(" ", "\\ "));
String commandline = StringUtils.join(command, " ");
/* Execute ffprobe and obtain the result */
logger.debug("Running {} {}", binary, commandline);
MediaContainerMetadata metadata = new MediaContainerMetadata();
final StringBuilder sb = new StringBuilder();
try {
ProcessInfo info = ProcessRunner.mk(binary, command.toArray(new String[command.size()]));
int exitCode = ProcessRunner.run(info, new Pred<String>() {
@Override
public Boolean apply(String s) {
logger.debug(s);
sb.append(s);
sb.append(System.getProperty("line.separator"));
return true;
}
}, fnLogError);
// Windows binary will return -1 when queried for options
if (exitCode != -1 && exitCode != 0 && exitCode != 255)
throw new MediaAnalyzerException("Frame analyzer " + binary + " exited with code " + exitCode);
} catch (IOException e) {
logger.error("Error executing ffprobe: {}", ExceptionUtils.getStackTrace(e));
throw new MediaAnalyzerException("Error while running ffprobe " + binary, e);
}
JSONParser parser = new JSONParser();
try {
JSONObject jsonObject = (JSONObject) parser.parse(sb.toString());
Object obj;
Double duration;
/* Get format specific stuff */
JSONObject jsonFormat = (JSONObject) jsonObject.get("format");
/* File Name */
obj = jsonFormat.get("filename");
if (obj != null) {
metadata.setFileName((String) obj);
}
/* Format */
obj = jsonFormat.get("format_long_name");
if (obj != null) {
metadata.setFormat((String) obj);
}
/*
* Mediainfo does not return a duration if there is no stream but FFprobe will return 0. For compatibility
* reasons, check if there are any streams before reading the duration:
*/
obj = jsonFormat.get("nb_streams");
if (obj != null && (Long) obj > 0) {
obj = jsonFormat.get("duration");
if (obj != null) {
duration = new Double((String) obj) * 1000;
metadata.setDuration(duration.longValue());
}
}
/* File Size */
obj = jsonFormat.get("size");
if (obj != null) {
metadata.setSize(new Long((String) obj));
}
/* Bitrate */
obj = jsonFormat.get("bit_rate");
if (obj != null) {
metadata.setBitRate(new Float((String) obj));
}
/* Loop through streams */
/*
* FFprobe will return an empty stream array if there are no streams. Thus we do not need to check.
*/
JSONArray streams = (JSONArray) jsonObject.get("streams");
Iterator<JSONObject> iterator = streams.iterator();
while (iterator.hasNext()) {
JSONObject stream = iterator.next();
/* Check type of string */
String codecType = (String) stream.get("codec_type");
if ("audio".equals(codecType)) {
/* Extract audio stream metadata */
AudioStreamMetadata aMetadata = new AudioStreamMetadata();
/* Codec */
obj = stream.get("codec_long_name");
if (obj != null) {
aMetadata.setFormat((String) obj);
}
/* Duration */
obj = stream.get("duration");
if (obj != null) {
duration = new Double((String) obj) * 1000;
aMetadata.setDuration(duration.longValue());
} else {
/*
* If no duration for this stream is specified assume the duration of the file for this as well.
*/
aMetadata.setDuration(metadata.getDuration());
}
/* Bitrate */
obj = stream.get("bit_rate");
if (obj != null) {
aMetadata.setBitRate(new Float((String) obj));
}
/* Channels */
obj = stream.get("channels");
if (obj != null) {
aMetadata.setChannels(((Long) obj).intValue());
}
/* Sample Rate */
obj = stream.get("sample_rate");
if (obj != null) {
aMetadata.setSamplingRate(Integer.parseInt((String) obj));
}
/* Frame Count */
obj = stream.get("nb_read_frames");
if (obj != null) {
aMetadata.setFrames(Long.parseLong((String) obj));
} else {
/* alternate JSON element if accurate frame count is not requested from ffmpeg */
obj = stream.get("nb_frames");
if (obj != null) {
aMetadata.setFrames(Long.parseLong((String) obj));
}
}
/* Add video stream metadata to overall metadata */
metadata.getAudioStreamMetadata().add(aMetadata);
/* Handle video streams ----------------------------- */
} else if ("video".equals(codecType)) {
/* Extract video stream metadata */
VideoStreamMetadata vMetadata = new VideoStreamMetadata();
/* Codec */
obj = stream.get("codec_long_name");
if (obj != null) {
vMetadata.setFormat((String) obj);
}
/* Duration */
obj = stream.get("duration");
if (obj != null) {
duration = new Double((String) obj) * 1000;
vMetadata.setDuration(duration.longValue());
} else {
/*
* If no duration for this stream is specified assume the duration of the file for this as well.
*/
vMetadata.setDuration(metadata.getDuration());
}
/* Bitrate */
obj = stream.get("bit_rate");
if (obj != null) {
vMetadata.setBitRate(new Float((String) obj));
}
/* Width */
obj = stream.get("width");
if (obj != null) {
vMetadata.setFrameWidth(((Long) obj).intValue());
}
/* Height */
obj = stream.get("height");
if (obj != null) {
vMetadata.setFrameHeight(((Long) obj).intValue());
}
/* Profile */
obj = stream.get("profile");
if (obj != null) {
vMetadata.setFormatProfile((String) obj);
}
/* Aspect Ratio */
obj = stream.get("sample_aspect_ratio");
if (obj != null) {
vMetadata.setPixelAspectRatio(parseFloat((String) obj));
}
/* Frame Rate */
obj = stream.get("avg_frame_rate");
if (obj != null) {
vMetadata.setFrameRate(parseFloat((String) obj));
}
/* Frame Count */
obj = stream.get("nb_read_frames");
if (obj != null) {
vMetadata.setFrames(Long.parseLong((String) obj));
} else {
/* alternate JSON element if accurate frame count is not requested from ffmpeg */
obj = stream.get("nb_frames");
if (obj != null) {
vMetadata.setFrames(Long.parseLong((String) obj));
}
}
/* Add video stream metadata to overall metadata */
metadata.getVideoStreamMetadata().add(vMetadata);
}
}
} catch (ParseException e) {
logger.error("Error parsing ffprobe output: {}", e.getMessage());
}
return metadata;
}
use of org.opencastproject.inspection.ffmpeg.api.VideoStreamMetadata in project opencast by opencast.
the class MediaInspector method addVideoStreamMetadata.
/**
* Adds the video related metadata to the track.
*
* @param track
* the track
* @param metadata
* the container metadata
* @throws Exception
* Media analysis is fragile, and may throw any kind of runtime exceptions due to inconsistencies in the
* media's metadata
*/
private Track addVideoStreamMetadata(TrackImpl track, MediaContainerMetadata metadata) throws Exception {
List<VideoStreamMetadata> videoList = metadata.getVideoStreamMetadata();
if (videoList != null && !videoList.isEmpty()) {
for (int i = 0; i < videoList.size(); i++) {
VideoStreamImpl video = new VideoStreamImpl("video-" + (i + 1));
VideoStreamMetadata v = videoList.get(i);
video.setBitRate(v.getBitRate());
video.setFormat(v.getFormat());
video.setFormatVersion(v.getFormatVersion());
video.setFrameCount(v.getFrames());
video.setFrameHeight(v.getFrameHeight());
video.setFrameRate(v.getFrameRate());
video.setFrameWidth(v.getFrameWidth());
video.setScanOrder(v.getScanOrder());
video.setScanType(v.getScanType());
// TODO: retain the original video metadata
track.addStream(video);
}
}
return track;
}
Aggregations