Skip to content

Commit

Permalink
Merge pull request #3128 from lkiesow/encode-hls
Browse files Browse the repository at this point in the history
Let Encode Handle HLS
  • Loading branch information
mtneug committed Dec 15, 2021
2 parents 5cde421 + 19c0940 commit 86c1683
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 22 deletions.
Expand Up @@ -490,41 +490,66 @@ private List <Track> parallelEncode(Job job, Track mediaTrack, String profileId)
// List of encoded tracks
LinkedList<Track> encodedTracks = new LinkedList<>();
// Do the work
int i = 0;
Map<String, File> source = new HashMap<>();
source.put("video", mediaFile);
List<File> outputFiles = encoderEngine.process(source, profile, properties);
var returnURLs = new ArrayList<URI>();
activeEncoder.remove(encoderEngine);
for (File encodingOutput: outputFiles) {
// Put the file in the workspace
URI returnURL;
final String targetTrackId = IdImpl.fromUUID().toString();

try (InputStream in = new FileInputStream(encodingOutput)) {
returnURL = workspace.putInCollection(COLLECTION,
job.getId() + "-" + i + "." + FilenameUtils.getExtension(encodingOutput.getAbsolutePath()), in);
logger.info("Copied the encoded file to the workspace at {}", returnURL);
if (encodingOutput.delete()) {
logger.info("Deleted the local copy of the encoded file at {}", encodingOutput.getAbsolutePath());
} else {
logger.warn("Unable to delete the encoding output at {}", encodingOutput);
int i = 0;
var fileMapping = new HashMap<String, String>();
for (File file: outputFiles) {
fileMapping.put(file.getName(), job.getId() + "_" + i + "." + FilenameUtils.getExtension(file.getName()));
i++;
}
boolean isHLS = false;
for (File file: outputFiles) {
// Rewrite HLS references if necessary
if (AdaptivePlaylist.isPlaylist(file)) {
isHLS = true;
logger.debug("Rewriting HLS references in {}", file);
try {
AdaptivePlaylist.hlsRewriteFileReference(file, fileMapping);
} catch (IOException e) {
throw new EncoderException("Unable to rewrite HLS references", e);
}
}

// Put the file in the workspace
try (InputStream in = new FileInputStream(file)) {
var filename = fileMapping.get(file.getName());
var url = workspace.putInCollection(COLLECTION, filename, in);
returnURLs.add(url);
logger.info("Copied the encoded file to the workspace at {}", url);
} catch (Exception e) {
throw new EncoderException("Unable to put the encoded file into the workspace", e);
}
}

// Have the encoded track inspected and return the result
Track inspectedTrack = inspect(job, returnURL);
// Have the encoded track inspected and return the result
final List<String> tags = profile.getTags();
for (Track inspectedTrack: inspect(job, returnURLs)) {
final String targetTrackId = IdImpl.fromUUID().toString();
inspectedTrack.setIdentifier(targetTrackId);

List<String> tags = profile.getTags();
for (String tag : tags) {
if (encodingOutput.getName().endsWith(profile.getSuffix(tag)))
for (final String tag : tags) {
if (inspectedTrack.getURI().getPath().endsWith(profile.getSuffix(tag))) {
inspectedTrack.addTag(tag);
}
}
if (isHLS) {
AdaptivePlaylist.setLogicalName(inspectedTrack);
}

encodedTracks.add(inspectedTrack);
i++;
}

// Clean up workspace
for (File encodingOutput: outputFiles) {
if (encodingOutput.delete()) {
logger.info("Deleted the local copy of the encoded file at {}", encodingOutput.getAbsolutePath());
} else {
logger.warn("Unable to delete the encoding output at {}", encodingOutput);
}
}

return encodedTracks;
Expand Down
Expand Up @@ -216,8 +216,12 @@ public void setUp() throws Exception {
// Create and populate the composer service
composerService = new ComposerServiceImpl() {
@Override
protected Track inspect(Job job, URI workspaceURI) throws EncoderException {
return inspectedTrack;
protected List<Track> inspect(Job job, List<URI> uris) throws EncoderException {
final var result = new ArrayList<Track>(uris.size());
for (URI uri: uris) {
result.add(inspectedTrack);
}
return result;
}
};

Expand Down

0 comments on commit 86c1683

Please sign in to comment.