Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.nio.channels.FileChannel;
- import java.nio.MappedByteBuffer;
- import java.nio.channels.Channels;
- import java.nio.channels.WritableByteChannel;
- int ww = 1280;
- int hh = 720;
- String tmpLoc = "C:\\p5tmp";
- String saveLoc = "test\\";
- int ind = -1;
- boolean doAudio;
- boolean started = true;
- boolean over;
- MappedByteBuffer mbb;
- String[] throb =
- new String[]{
- ".", " .", " .", " .", " .", " ."," ."};
- RunCmd ffVid, ffAud;
- void setup() {
- size(200, 100);
- frameRate(30);
- saveLoc = sketchPath(saveLoc);
- try {
- FileChannel fc =
- new RandomAccessFile(tmpLoc, "rw").getChannel();
- mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, ww*hh*4);
- ffVid = new RunCmd(true, false, ww*hh*16) {
- public String getCmd() {
- return
- "ffmpeg " +
- "-y " +
- "-f rawvideo -vcodec rawvideo -pix_fmt bgra " +
- "-s " + ww + "x" + hh + " -i - " +
- "-r 30 -vcodec h264 -crf 18 " +
- "-x264opts open_gop=0:cabac=1 " +
- "-preset veryfast -rtbufsize 3686400 " +
- "-vf vflip " +
- "testv.mp4";
- }
- // public void doResponse(String re) {
- // println(re);
- // }
- };
- if(doAudio) {
- ffAud = new RunCmd(true, false, 5120) {
- public String getCmd() {
- return
- "ffmpeg " +
- "-y " +
- "-f dshow -i audio=\"virtual-audio-capturer\" " +
- "-ac 2 " +
- "testa.mp4";
- }
- // public void doResponse(String re) {
- // println(re);
- // }
- };
- }
- } catch(Throwable ex) {ex.printStackTrace();}
- }
- void mouseClicked() {
- if(!started) {
- println("starting output");
- started = true;
- return;
- } else {
- println("ending output");
- started = false;
- try {
- ffVid.proc.getOutputStream().close();
- if(doAudio) {
- OutputStream os = ffAud.proc.getOutputStream();
- os.write("q\n".getBytes());
- os.flush();
- Thread.sleep(500);
- new RunCmd(true, true, 4096) {
- public String getCmd() {
- return
- "ffmpeg -y " +
- "-i testv.mp4 -i testa.mp4 " +
- "-c copy -shortest " +
- "testm.mp4";
- }
- public void doResponse(String re) {
- println(re);
- }
- public void ended() {
- new File(sketchPath("testa.mp4")).delete();
- new File(sketchPath("testv.mp4")).delete();
- }
- };
- }
- } catch(Throwable ex) {ex.printStackTrace();}
- exit();
- }
- }
- void draw() {
- frame.setTitle(
- nf((ind>-1?ind:0), 6) + " " + throb[frameCount%7]);
- try {
- if(started) {
- ind++;
- mbb.rewind();
- if(!ffVid.dead) ffVid.wbc.write(mbb);
- }
- } catch(Throwable ex) {}
- }
- void exit() {
- try {
- Thread.currentThread().sleep(500);
- } catch(Throwable ex) {ex.printStackTrace();}
- super.exit();
- }
- class RunCmd {
- boolean dead;
- Process proc;
- WritableByteChannel wbc;
- RunCmd(boolean doCom, boolean doEnd, int bf) throws Throwable {
- ProcessBuilder builder = new ProcessBuilder(
- Arrays.asList(splitTokens(getCmd())));
- builder.directory(new File(getDir()));
- builder.redirectErrorStream(true);
- proc = builder.start();
- BufferedOutputStream bos = new BufferedOutputStream(
- proc.getOutputStream(), bf);
- final BufferedReader br = new BufferedReader(
- new InputStreamReader(proc.getInputStream()));
- if(doCom) {
- wbc = Channels.newChannel(bos);
- new Thread(new Runnable() {
- public void run() {
- String ln;
- try {
- while(!dead && (ln = br.readLine()) != null) {
- doResponse(ln);
- }
- } catch(Throwable ex) {ex.printStackTrace();}
- }
- }).start();
- }
- if(doEnd) {
- new Thread(new Runnable() {
- public void run() {
- try {
- proc.waitFor();
- die();
- proc.destroy();
- } catch(Throwable ex) {ex.printStackTrace();}
- }
- }).start();
- }
- }
- void die() throws Throwable {
- dead = true;
- ended();
- }
- String getDir() {return sketchPath("");}
- String getCmd() {return "";}
- void doResponse(String re) {}
- void ended() {}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement