{"id":1988,"date":"2018-04-09T18:02:50","date_gmt":"2018-04-09T10:02:50","guid":{"rendered":"http:\/\/www.magicandlove.com\/blog\/?p=1988"},"modified":"2018-04-09T18:02:50","modified_gmt":"2018-04-09T10:02:50","slug":"saving-video-from-processing-with-the-jcodec-2-3","status":"publish","type":"post","link":"http:\/\/www.magicandlove.com\/blog\/2018\/04\/09\/saving-video-from-processing-with-the-jcodec-2-3\/","title":{"rendered":"Saving video from Processing with the jCodec 2.3"},"content":{"rendered":"<p>In the <a href=\"http:\/\/www.magicandlove.com\/blog\/2016\/09\/11\/save-processing-screen-as-video-with-jcodec-new\/\">former post<\/a>, I have tested using the jCodec 0.1.5 and 0.2.0 to save the Processing screen into an MP4 file. The latest version of jCodec 0.2.3 has, however, changed its functions for the AWT based applications. Here is the new code for Processing to use jCodec 0.2.3 to save any BufferedImage to an external MP4 file.<\/p>\n<p>To use the code, you need to download from the<a href=\"http:\/\/jcodec.org\/\" target=\"_blank\" rel=\"noopener\"> jCodec website<\/a> the following two jar files and put them into the <strong>code<\/strong> folder of your Processing sketch.<\/p>\n<ul>\n<li>jcodec-0.2.3.jar<\/li>\n<li>jcodec-javase-0.2.3.jar<\/li>\n<\/ul>\n<p>The following code will write a frame of your Processing screen into the MP4 file for every mouse pressed action.<\/p>\n<pre lang=\"java\">import processing.video.*;\r\nimport java.awt.image.BufferedImage;\r\nimport org.jcodec.api.awt.AWTSequenceEncoder;\r\n\r\nCapture cap;\r\nAWTSequenceEncoder enc;\r\n\r\npublic void settings() {\r\n  size(640, 480);\r\n}\r\n\r\npublic void setup() {\r\n  cap = new Capture(this, width, height);\r\n  cap.start();\r\n  String fName = \"recording.mp4\";\r\n  enc = null;\r\n  try {\r\n    enc = AWTSequenceEncoder.createSequenceEncoder(new File(dataPath(fName)), 25);\r\n  } \r\n  catch (IOException e) {\r\n    println(e.getMessage());\r\n  }\r\n}\r\n\r\npublic void draw() {\r\n  image(cap, 0, 0);\r\n}\r\n\r\npublic void captureEvent(Capture c) {\r\n  c.read();\r\n}\r\n\r\nprivate void saveVideo(BufferedImage i) {\r\n  try {\r\n    enc.encodeImage(i);\r\n  } \r\n  catch (IOException e) {\r\n    println(e.getMessage());\r\n  }\r\n}\r\n\r\npublic void mousePressed() {\r\n  saveVideo((BufferedImage) this.getGraphics().getImage());\r\n}\r\n\r\npublic void exit() {\r\n  try {\r\n    enc.finish();\r\n  } \r\n  catch (IOException e) {\r\n    println(e.getMessage());\r\n  }\r\n  super.exit();\r\n}\r\n<\/pre>\n<p>To save only the capture image, you can just replace the following saveVideo command.<\/p>\n<pre lang=\"java\">\r\nsaveVideo((BufferedImage) cap.getNative());\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In the former post, I have tested using the jCodec 0.1.5 and 0.2.0 to save the Processing screen into an MP4 file. The latest version of jCodec 0.2.3 has, however, changed its functions for the AWT based applications. Here is the new code for Processing to use jCodec 0.2.3 to save any BufferedImage to an [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79,66],"tags":[149,159,62],"class_list":["post-1988","post","type-post","status-publish","format-standard","hentry","category-software-2","category-testing","tag-jcodec","tag-mp4","tag-processing-org"],"_links":{"self":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1988","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=1988"}],"version-history":[{"count":1,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1988\/revisions"}],"predecessor-version":[{"id":1989,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1988\/revisions\/1989"}],"wp:attachment":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/media?parent=1988"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/categories?post=1988"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/tags?post=1988"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}