Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package net.fkasoft.wowzamod;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import com.wowza.wms.application.*;
import com.wowza.wms.module.*;
import static java.util.concurrent.TimeUnit.*;
public class WowzaMod extends ModuleBase {
private final ScheduledExecutorService scheduler = Executors
.newScheduledThreadPool(1);
public void onAppStart(final IApplicationInstance appInstance) {
final Runnable daemon = new Runnable() {
public void run() {
try {
// cd to content folder
Runtime.getRuntime()
.exec(new String[] { "cd /usr/local/WowzaMediaServer/content" });
// current datetime
Date date = new Date();
StringBuilder sb = new StringBuilder();
sb.append("record_").append(
new SimpleDateFormat("yyyy-MM-dd_HH:mm")
.format(date));
String flvName = sb.toString() + ".flv";
String mp4Name = sb.toString() + ".mp4";
// record command
final String[] recordCmd = { "rtmpdump", "-v", "-r",
"rtmp://dev-Wowza.christian.tv:1935/live", "-y",
"mp4:URL/myStream9999", "-o", flvName };
// convert command
final String[] convertCmd = { "ffmpeg", "-i", flvName,
"-c:v", "libx264", "-crf", "20", "-c:a", "libfaac",
"-q:a", "100", mp4Name };
final CountDownLatch latch = new CountDownLatch(2);
new Thread() {
@Override
public void run() {
try {
Process p1 = Runtime.getRuntime().exec(
recordCmd);
// check for a live feed
BufferedReader in = new BufferedReader(
new InputStreamReader(
p1.getInputStream()));
String currentLine;
int i = 0;
while ((currentLine = in.readLine()) != null) {
i++;
if (currentLine.contains("ERROR: Closing connection: NetStream.Play.StreamNotFound")) {
in.close();
p1.destroy();
latch.countDown();
latch.countDown();
}
if (i >= 20) {
in.close();
}
}
if (latch.getCount() == 2) {
latch.countDown();
Runtime.getRuntime().exec(convertCmd);
latch.countDown();
}
} catch (IOException e) {
getLogger().error(e.getMessage());
}
}
}.start();
// to to finish recording and converting...
latch.wait();
} catch (IOException e) {
getLogger().error(e.getMessage());
} catch (InterruptedException e) {
getLogger().error(e.getMessage());
}
}
};
// when not recording or converting check
// try to record every 25 seconds from Remote Host
final ScheduledFuture<?> daemonHandle = scheduler.scheduleAtFixedRate(
daemon, 25, 25, SECONDS);
scheduler.schedule(new Runnable() {
public void run() {
daemonHandle.cancel(true);
}
}, (1000000000), SECONDS);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment