index.html 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <html>
  2. <head>
  3. <meta charset="UTF-8">
  4. <meta name="description" content="Online Media File Converter">
  5. <meta name="author" content="tobychui">
  6. <title>Universal Format Converter | imuslab</title>
  7. <link rel="icon" type="image/png" href="/favicon.png" />
  8. <meta name="apple-mobile-web-app-capable" content="yes" />
  9. <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1, maximum-scale=1"/>
  10. <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js" integrity="sha512-v2CJ7UaYy4JwqLDIrZUI/4hqeoQieOmAZNXBeQyjo21dadnwR+8ZaIJVT8EE2iyI61OV8e6M8PP2/4hpQINQ/g==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
  11. <script src="https://cdnjs.cloudflare.com/ajax/libs/fomantic-ui/2.9.3/semantic.min.js" integrity="sha512-gnoBksrDbaMnlE0rhhkcx3iwzvgBGz6mOEj4/Y5ZY09n55dYddx6+WYc72A55qEesV8VX2iMomteIwobeGK1BQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
  12. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fomantic-ui/2.9.3/semantic.min.css" integrity="sha512-3quBdRGJyLy79hzhDDcBzANW+mVqPctrGCfIPosHQtMKb3rKsCxfyslzwlz2wj1dT8A7UX+sEvDjaUv+WExQrA==" crossorigin="anonymous" referrerpolicy="no-referrer" />
  13. <link rel="preconnect" href="https://fonts.googleapis.com">
  14. <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  15. <link href="https://fonts.googleapis.com/css2?family=Noto+Sans+TC:wght@100;200;300;400&display=swap" rel="stylesheet">
  16. <style>
  17. .main{
  18. display: flex;
  19. align-items: center;
  20. justify-content: center;
  21. height: calc(100vh);
  22. margin: 0;
  23. }
  24. .item.disabled{
  25. opacity: 0.5;
  26. }
  27. .ui.header,button,input{
  28. font-family: 'Noto Sans TC', sans-serif !important;
  29. }
  30. @media screen and (min-width: 768px) {
  31. #title{
  32. border-right: 1px solid #dedede;
  33. padding-right: 5em;
  34. min-width: 300px;
  35. }
  36. #title .ui.header{
  37. width: 300px;
  38. }
  39. #qrgrid{
  40. padding-left: 5em;
  41. }
  42. }
  43. </style>
  44. </head>
  45. <body>
  46. <div class="main">
  47. <div class="ui stackable grid">
  48. <div id="title" class="eight wide column">
  49. <h2 class="ui header">
  50. Universal Format Converter
  51. <div class="sub header">通用多媒體格式轉換工具</div>
  52. </h2>
  53. <div class="ui divider"></div>
  54. <p>Select a file | 選擇檔案</p>
  55. <div class="ui file input">
  56. <input type="file" id="uploader">
  57. </div>
  58. <br><br>
  59. <p>Convert selected file to | 轉換檔案格式至</p>
  60. <div class="ui fluid selection dropdown">
  61. <div class="default text">Select format | 選擇格式</div>
  62. <i class="dropdown icon"></i>
  63. <div class="menu">
  64. <div class="header">
  65. <i class="play icon"></i>
  66. Video | 影片
  67. </div>
  68. <div class="item format fVideo disabled">MP4</div>
  69. <div class="item format fVideo disabled">MKV</div>
  70. <div class="item format fVideo disabled">AVI</div>
  71. <div class="header">
  72. <i class="music icon"></i>
  73. Audio | 音訊
  74. </div>
  75. <div class="item format fAudio disabled">MP3</div>
  76. <div class="item format fAudio disabled">WAV</div>
  77. <div class="item format fAudio disabled">FLAC</div>
  78. <div class="header">
  79. <i class="image icon"></i>
  80. Image | 圖片
  81. </div>
  82. <div class="item format fImage disabled">PNG</div>
  83. <div class="item format fImage disabled">JPEG</div>
  84. <div class="item format fImage disabled">WebP</div>
  85. <div class="item format fImage fVideo disabled">GIF</div>
  86. </div>
  87. </div>
  88. <button id="startconv" class="ui basic fluid button" style="margin-top: 1em;">Start Convert | 轉換</button>
  89. <div class="ui divider"></div>
  90. <p>Powered by <a href="/">imuslab</a></p>
  91. </div>
  92. <div id="conv" class="eight wide column" align="center">
  93. <div style="width: 300px">
  94. <video id="output-video" style="display:none; max-width: 100%;" controls></video><br/>
  95. <img id="output-image" class="ui fluid image" style="display:none;"></img>
  96. <p id="message" style="margin-top: 1em;"><i class="ui arrow left icon"></i> Select a file to start conversion</p>
  97. <a id="downloadbtn" href="#" download="">Download</a>
  98. </div>
  99. </div>
  100. </div>
  101. </div>
  102. <script>
  103. $(".dropdown").dropdown();
  104. </script>
  105. <script type="module">
  106. import { FFmpeg } from "./assets/ffmpeg/package/dist/esm/index.js";
  107. import { fetchFile } from "./assets/util/package/dist/esm/index.js";
  108. let ffmpeg = null;
  109. function getMimeFromExt(ext){
  110. if (ext == "mp4" || ext == "webm"){
  111. return "video/" + ext;
  112. }else if (ext == "mp3" || ext == "wav" || ext == "ogg" || ext == "flac"){
  113. return "audio/" + ext;
  114. }else if (ext == "png" || ext == "jpeg" || ext == "webp" || ext == "gif"){
  115. return "image/" + ext;
  116. }else{
  117. return "application/" + ext;
  118. }
  119. }
  120. const transcode = async ({ target: { files } }) => {
  121. const message = document.getElementById('message');
  122. if (ffmpeg === null) {
  123. ffmpeg = new FFmpeg();
  124. ffmpeg.on("log", ({ message }) => {
  125. console.log(message);
  126. })
  127. ffmpeg.on("progress", ({ progress }) => {
  128. message.innerHTML = `${(progress * 100).toFixed(3)} %`;
  129. });
  130. await ffmpeg.load({
  131. coreURL: "../../../../../assets/core-mt/package/dist/esm/ffmpeg-core.js",
  132. });
  133. }
  134. const { name } = files[0];
  135. let format = $(".dropdown").dropdown("get value")[0];
  136. await ffmpeg.writeFile(name, await fetchFile(files[0]));
  137. message.innerHTML = 'Start transcoding';
  138. await ffmpeg.exec(['-i', name, '-threads', '4', 'output.' + format]);
  139. message.innerHTML = 'Complete transcoding';
  140. const data = await ffmpeg.readFile('output.' + format);
  141. let mime = getMimeFromExt(format);
  142. let datablob = URL.createObjectURL(new Blob([data.buffer], { type: mime }));
  143. if (mime.includes("image")){
  144. $("#output-image").show();
  145. $("#output-video").hide();
  146. $("#output-image").attr('src', datablob);
  147. }else{
  148. $("#output-image").hide();
  149. $("#output-video").show();
  150. const video = document.getElementById('output-video');
  151. video.src = datablob;
  152. }
  153. //Generate a download button
  154. let outputFilename = name.split(".");
  155. outputFilename.pop();
  156. outputFilename = outputFilename + "." + format;
  157. $("#downloadbtn").attr('href', datablob);
  158. $("#downloadbtn").attr("download",outputFilename);
  159. }
  160. function disableInvalidOptions(input){
  161. let selectedFile = input.target.files;
  162. window.selectedFilelist = selectedFile;
  163. $(".dropdown").dropdown("clear");
  164. const message = document.getElementById('message');
  165. if (selectedFile.length > 0){
  166. selectedFile = selectedFile[0];
  167. let formats = document.getElementsByClassName("format");
  168. console.log(formats);
  169. for (var i = 0; i < formats.length; i++){
  170. let format = formats[i];
  171. format.classList.add("disabled");
  172. }
  173. let formatsToEnable;
  174. if (selectedFile.type.includes("video/")){
  175. formatsToEnable = document.querySelectorAll(".fVideo,.fAudio");
  176. }else if (selectedFile.type.includes("audio/")){
  177. formatsToEnable = document.getElementsByClassName("fAudio");
  178. }else if (selectedFile.type.includes("image/")){
  179. formatsToEnable = document.getElementsByClassName("fImage");
  180. }else{
  181. message.innerHTML = 'Input format not supported';
  182. return;
  183. }
  184. for (let item of formatsToEnable) {
  185. item.classList.remove("disabled");
  186. }
  187. }
  188. }
  189. function startConv(){
  190. let format = $(".dropdown").dropdown("get value")[0];
  191. if (format == ""){
  192. const message = document.getElementById('message');
  193. message.innerHTML = 'No format selected';
  194. return;
  195. }
  196. let selectedFile = {target: {
  197. files: window.selectedFilelist
  198. }
  199. };
  200. const message = document.getElementById('message');
  201. message.innerHTML = "Waiting server response"
  202. console.log(selectedFile);
  203. transcode(selectedFile);
  204. }
  205. const elm = document.getElementById('uploader');
  206. //elm.addEventListener('change', transcode);
  207. elm.addEventListener('change', disableInvalidOptions);
  208. const sconv = document.getElementById('startconv');
  209. sconv.addEventListener('click', startConv);
  210. </script>
  211. </body>
  212. </html>