{"id":1877,"date":"2016-08-28T18:59:05","date_gmt":"2016-08-28T10:59:05","guid":{"rendered":"http:\/\/www.magicandlove.com\/blog\/?p=1877"},"modified":"2016-09-11T14:50:02","modified_gmt":"2016-09-11T06:50:02","slug":"save-video-in-processing","status":"publish","type":"post","link":"http:\/\/www.magicandlove.com\/blog\/2016\/08\/28\/save-video-in-processing\/","title":{"rendered":"Save video in Processing with JCodec"},"content":{"rendered":"<p>As a side product of current research, I manage to save a Processing screen in an MP4 video file with the use of the <a href=\"http:\/\/jcodec.org\/\">JCodec library<\/a>. Download the former\u00a0jcodec-0.1.5.jar into the <strong>code<\/strong> folder of your Processing sketch. The simplest way is to use the <strong>SequenceEncoder<\/strong> class to add a <a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/java\/awt\/image\/BufferedImage.html\">BufferedImage<\/a> to the MP4 video. Remember to <strong>finish<\/strong> the video file before ending.<\/p>\n<p>The following example captures the live video stream from a webcam and outputs to an external MP4 file in the <strong>data<\/strong> folder. Use the mouse click to control the recording.<\/p>\n<p>Here is the source code.<\/p>\n<pre lang=\"java\">import processing.video.*;\r\nimport org.jcodec.api.SequenceEncoder;\r\nimport java.awt.image.BufferedImage;\r\nimport java.io.File;\r\n\r\nCapture cap;\r\nSequenceEncoder enc;\r\nString videoName;\r\nboolean recording;\r\n\r\nvoid setup() {\r\n  size(640, 480);\r\n  background(0);\r\n  cap = new Capture(this, width, height);\r\n  videoName = \"bear.mp4\";\r\n  recording = false;\r\n  frameRate(25);\r\n  smooth();\r\n  noStroke();\r\n  fill(255);\r\n  cap.start();\r\n  try {\r\n    enc = new SequenceEncoder(new File(dataPath(videoName)));\r\n  } \r\n  catch (IOException e) {\r\n    e.printStackTrace();\r\n  }\r\n}\r\n\r\nvoid draw() {\r\n  image(cap, 0, 0);\r\n  String fStr = nf(round(frameRate));\r\n  text(fStr, 10, 20);\r\n  if (recording) {\r\n    BufferedImage bi = (BufferedImage) this.getGraphics().getImage();\r\n    try {\r\n      enc.encodeImage(bi);\r\n    } \r\n    catch (IOException e) {\r\n      e.printStackTrace();\r\n    }\r\n  }\r\n}\r\n\r\nvoid captureEvent(Capture c) {\r\n  c.read();\r\n}\r\n\r\nvoid mousePressed() {\r\n  recording = !recording;\r\n  println(\"Recording : \" + recording);\r\n}\r\n\r\nvoid keyPressed() {\r\n  if (keyCode == 32) {\r\n    try {\r\n      enc.finish();\r\n    } \r\n    catch (IOException e) {\r\n      e.printStackTrace();\r\n    }\r\n  }\r\n}<\/pre>\n<p>The program also uses the undocumented functions, <strong>getGraphics()<\/strong> and <strong>getImage()<\/strong> to obtain the raw image of the Processing sketch window.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As a side product of current research, I manage to save a Processing screen in an MP4 video file with the use of the JCodec library. Download the former\u00a0jcodec-0.1.5.jar into the code folder of your Processing sketch. The simplest way is to use the SequenceEncoder class to add a BufferedImage to the MP4 video. Remember [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[89,79,66],"tags":[149,62,150,151],"class_list":["post-1877","post","type-post","status-publish","format-standard","hentry","category-research","category-software-2","category-testing","tag-jcodec","tag-processing-org","tag-save-video","tag-sketch-recording"],"_links":{"self":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1877","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/comments?post=1877"}],"version-history":[{"count":5,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1877\/revisions"}],"predecessor-version":[{"id":1879,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1877\/revisions\/1879"}],"wp:attachment":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/media?parent=1877"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/categories?post=1877"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/tags?post=1877"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}