-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
1 lines (1 loc) · 8.98 KB
/
main.js
1
"use strict";function t(t,e,o){if(o||2===arguments.length)for(var n,r=0,i=e.length;r<i;r++)!n&&r in e||(n||(n=Array.prototype.slice.call(e,0,r)),n[r]=e[r]);return t.concat(n||Array.prototype.slice.call(e))}var e,o={executable:"ffmpeg",pure_mode:!0,ffmpeg_params:"",input_seeking:!0,selection:"primary",copy_utility:"detect",hide_osc_on_crop:!1,box_color:"#FF1493",key_crop:"c",key_preview:"ctrl+shift+w",key_pure_mode:"ctrl+p",key_file_path:"ctrl+w",key_timestamp:"ctrl+e",key_timestamp_end:"ctrl+shift+e"},n={isCropping:!1,color:"9314FF",toString:function(){return"number"==typeof this.w&&"number"==typeof this.h?"".concat(n.w,":").concat(n.h,":").concat(n.x,":").concat(n.y):""}},r={options:o,getKeys:function(){return{crop:o.key_crop,preview:o.key_preview,mode:o.key_pure_mode,path:o.key_file_path,time:o.key_timestamp,timeEnd:o.key_timestamp_end}},setBoxColor:function(){var t=r.options.box_color.toUpperCase();if(/^#[0-9A-F]{6}$/.test(t)){var e=t.slice(1),o="".concat(e[0]).concat(e[1]),n="".concat(e[2]).concat(e[3]),i="".concat(e[4]).concat(e[5])+n+o;r.cropBox.color=i}else{mp.msg.warn("Invalid hex color: ".concat(t));r.cropBox.color="9314FF"}},timestamps:{},cropBox:n},i=function(e){var o,n=r.options,i=n.copy_utility,c=n.selection;"xclip"!==i&&"wl-copy"!==i&&(mp.msg.error("ERROR: ".concat(i," is not a known copy utility. ")+"Possible values are: xclip, wl-copy"),print("INFO: setting copy utility to 'xclip'"),i="xclip"),"primary"!=c&&"clipboard"!=c&&(mp.msg.error("ERROR: ".concat(c," is not a valid selection. ")+"Possible values are: primary, clipboard"),print("INFO: setting selection to 'primary'"),c="primary"),"xclip"===i?o=["xclip","-selection",c]:(o=["wl-copy"],"primary"===c&&(o=t(t([],o,!0),["--primary"],!1)));var a=mp.command_native({name:"subprocess",args:o,stdin_data:e,detach:!0}).status;if(-3===a)return mp.msg.error("Received status: ".concat(a)),void p("Error occurred during the execution of ".concat(i,". ")+"Please verify your ".concat(i," installation."));p("Copied to ".concat(c,": ").concat(e))},c=function(){var t=mp.get_property("path");if("string"!=typeof t)throw new Error("Unable to get the path");return t},p=function(t){mp.osd_message(t),print(t)},a=function(t){if(-1!==t.search("youtube|youtu.be"))return"youtube"},s=function(e,o){return e.reduce((function(e,n){var r=n.match(/http[s]?:\/\/.+/);return-1!==n.search(o)&&r?t(t([],e,!0),[r[0]],!1):e}),[])},m=r.cropBox,u=mp.create_osd_overlay("ass-events"),d=function(){m.isCropping?(u.remove(),l(),m.isCropping=!1,mp.unobserve_property(h),r.options.hide_osc_on_crop&&mp.command("script-message osc-visibility auto"),print("Cropping ended")):(m.isCropping=!0,y(),mp.observe_property("mouse-pos","native",h),r.options.hide_osc_on_crop&&mp.command("script-message osc-visibility never"),print("Cropping started"))},y=function(){var t=v("mouse");m.x=t.x,m.y=t.y,m.constX=t.x,m.constY=t.y},l=function(){var t=v("osd"),e=v("video");if(!x(m))throw new Error("cropBox is not set");var o=t.width,n=t.height,r=[0,0],i=r[0],c=r[1],p=n*e.width/e.height,a=o*e.height/e.width;p>o?(p=o,i=n-a):a>n&&(a=n,c=o-p),m.y-=Math.ceil(i/2),m.x-=Math.ceil(c/2);var s=Math.min(e.width/p,e.height/a);m.w=Math.ceil(m.w*s),m.h=Math.ceil(m.h*s),m.x=Math.ceil(m.x*s),m.y=Math.ceil(m.y*s)},f=function(){delete m.h,delete m.w,delete m.y,delete m.x,delete m.constY,delete m.constX,p("Crop reset")},h=function(t,e){if(!e||"object"!=typeof e||!("x"in e)||!("y"in e)||"number"!=typeof e.x||"number"!=typeof e.y)throw new Error("Did not receive mouse coordinates: ".concat(JSON.stringify(e)));g({x:e.x,y:e.y}),_()},_=function(){if(!x(m))throw new Error("cropbox is not set");var t="{\\3c&".concat(r.cropBox.color,"&}"),e=v("osd");u.res_y=e.height,u.res_x=e.width;var o=m.x,n=m.y,i=m.w,c=m.h,p="{\\p1}m ".concat(o," ").concat(n," l ").concat(o+i," ").concat(n," ").concat(o+i," ")+"".concat(n+c," ").concat(o," ").concat(n+c," {\\p0}"),a="".concat("{\\pos(0, 0)}").concat(t).concat("{\\1a&FF&}").concat("{\\bord4}").concat(p);u.data=a,u.update()},g=function(t){if("number"!=typeof m.constX||"number"!=typeof m.constY||"number"!=typeof m.x||"number"!=typeof m.y)throw new Error("the cropbox was not initialized: ".concat(JSON.stringify(m)));t.x<m.constX?(m.x=t.x,t.x=m.constX,m.x=Math.min(t.x,m.x),m.w=t.x-m.x):(t.x=Math.max(t.x,m.x),m.x=Math.min(t.x,m.x),m.w=t.x-m.x),t.y<m.constY?(m.y=t.y,t.y=m.constY,m.y=Math.min(t.y,m.y),m.h=t.y-m.y):(t.y=Math.max(t.y,m.y),m.y=Math.min(t.y,m.y),m.h=t.y-m.y)},v=function(t){if("mouse"===t){var e=mp.get_property_native("mouse-pos");if(!e||"object"!=typeof e||!("x"in e)||!("y"in e)||"number"!=typeof e.x||"number"!=typeof e.y)throw new Error("Unable to retrieve mouse properties");return{x:e.x,y:e.y}}if("osd"===t){var o=mp.get_osd_size();if(!o||!("height"in o)||!("width"in o)||"number"!=typeof o.width||"number"!=typeof o.height)throw new Error("Unable to retrieve OSD size");return{height:o.height,width:o.width}}var n=mp.get_property_native("width"),r=mp.get_property_native("height");if("number"!=typeof r||"number"!=typeof n)throw new Error("Unable to retrieve video properties");return{width:n,height:r}},x=function(t){return"number"==typeof t.w&&"number"==typeof t.h&&"number"==typeof t.x&&"number"==typeof t.constX&&"number"==typeof t.constY},b=function(){p("Processing preview");var t="yes"===mp.get_property("mute")?"-an":"",e=w(),o=k(),n="".concat(t," -map_metadata -1 -map_chapters -1 -f matroska ")+"-c:v libx264 -preset ultrafast - | mpv - --loop",r=e.map((function(t,e){return"-map ".concat(e,":a?")+(o?"":" -map ".concat(e,":v?"))})),i="ffmpeg -hide_banner ".concat(e.join(" ")," ").concat(r.join(" ")," ").concat(o," ").concat(n);mp.commandv("run","bash","-c","(".concat(i,")"))},w=function(e){void 0===e&&(e={subProcessMode:!1});var o,n,i,p=r.options.input_seeking,m=(o=r.timestamps,n=o.start,i=o.end,"".concat(n?"-ss "+n:"").concat(i?(n?" ":"")+"-to "+i:"")),u=c(),d=-1!==u.search("^http[s]?://");if(!m&&!d)return e.subProcessMode?["-i","".concat(u)]:['-i "'.concat(u,'"')];if(!d)return e.subProcessMode?t(t([],m.split(" "),!0),["-i","".concat(u)],!1):p?["".concat(m,' -i "').concat(u,'"')]:['-i "'.concat(u,'" ').concat(m)];var y=function(t){var e=a(t),o=mp.get_property("stream-open-filename");if("string"!=typeof o)throw new Error("Unable to retrieve the open stream");var n=o.split(";");if("youtube"===e)return s(n,"googlevideo")}(u),l=[];if(!y)throw new Error("FIX ME: Unable to parse the stream urls. Source is unknown");return y.forEach((function(o){e.subProcessMode?l.push.apply(l,t(t([],m.split(" "),!1),["-i","".concat(o)],!1)):p?l.push("".concat(m,' -i "').concat(o,'"')):l.push('-i "'.concat(o,'" ').concat(m))})),l},k=function(){return x(r.cropBox)?"-lavfi crop=".concat(r.cropBox.toString()):""},E=function(){var t={w:r.cropBox.w,h:r.cropBox.h,x:r.cropBox.x,y:r.cropBox.y},e={start:r.timestamps.start,end:r.timestamps.end};mp.set_property_native("user-data/PureMPV",{cropbox:t,timestamps:e})},M=function(){r.options.pure_mode&&!m.isCropping&&x(m)?f():d(),r.options.pure_mode||r.cropBox.isCropping||i(r.cropBox.toString()),r.cropBox.isCropping||E()},B=function(){if(r.options.pure_mode){var t,e,o,n,p=(t=r.options.executable,e=r.options.ffmpeg_params,o=w(),n=k(),"".concat(t," ").concat(o.join(" ")," ").concat(n," ").concat(e).trim());i(p)}else{var a=c();i(a)}},C=function(t){var e=function(){var t=mp.get_property_native("time-pos");if("number"!=typeof t)throw new Error("Unable to retrieve the time position");return new Date(1e3*t).toISOString().substring(11,23)}();if((null==t?void 0:t.getEndTime)&&r.options.pure_mode)return r.timestamps.end=e,p("Set end time: ".concat(r.timestamps.end)),void E();r.options.pure_mode?r.timestamps.start?r.timestamps.end?(delete r.timestamps.start,delete r.timestamps.end,p("Times reset"),E()):(r.timestamps.end=e,p("Set end time: ".concat(r.timestamps.end)),E()):(r.timestamps.start=e,p("Set start time: ".concat(r.timestamps.start)),E()):i(e)},F=function(){r.options.pure_mode=!r.options.pure_mode;var t="Pure Mode: ";if(r.options.pure_mode){var e=r.getKeys();t+="ON",mp.add_key_binding(e.preview,"generate-preview",b),mp.add_key_binding(e.timeEnd,"set-endtime",(function(){return C({getEndTime:!0})}))}else t+="OFF",mp.remove_key_binding("generate-preview"),mp.remove_key_binding("set-endtime");p(t)};!function(){if(mp.options.read_options(r.options,"PureMPV"),"detect"===r.options.copy_utility)try{r.options.copy_utility=function(){if(-3!==mp.command_native({name:"subprocess",args:["xclip","-version"],detach:!0,capture_stderr:!0}).status)return"xclip";if(-3!==mp.command_native({name:"subprocess",args:["wl-copy","--version"],detach:!0,capture_stdout:!0}).status)return"wl-copy";throw new Error("No xclip/wl-clipboard found installed. Copying will not work.")}()}catch(t){t instanceof Error&&(mp.msg.error(t.message),r.options.copy_utility="xclip")}}(),e=r.getKeys(),mp.add_key_binding(e.path,"get-file-path",B),mp.add_key_binding(e.time,"get-timestamp",C),mp.add_key_binding(e.crop,"get-crop",M),mp.add_key_binding(e.mode,"toggle-puremode",F),r.options.pure_mode&&(mp.add_key_binding(e.preview,"generate-preview",b),mp.add_key_binding(e.timeEnd,"set-endtime",(function(){return C({getEndTime:!0})}))),r.setBoxColor(),E();