templates/audio-to-base64.html.twig line 1

Open in your IDE?
  1. {% extends 'base.html.twig' %}
  2. {% block title %}
  3. ToBase64.dev - Convert audio to base64, Encode audio to base64
  4. {% endblock %}
  5. {% block h1 %}Convert your audio to Base64{% endblock %}
  6. {% block body %}
  7. <div class="row" style="height: 60%;">
  8. <div class="padding col-5 d-flex flex-column">
  9. <div class="row" style="height:85%">
  10. <div class="col-md-12" style="height:100%">
  11. <div id="imageb64" style="
  12. border-left-width: 5px;
  13. border-top-width: 5px;
  14. border-right-width: 5px;
  15. border-bottom-width: 5px;
  16. border-color: #17a2b8;
  17. border-style: solid;
  18. height: 100%;
  19. width: 100%;
  20. ">
  21. <audio controls class="form-control" id="audio" name="output" placeholder="Output will be displayed here..." spellcheck="false" style="width: 100%;height: 100%;">
  22. <source id="output4" src="data:audio/mpeg;base64," type="audio/mpeg">
  23. Your browser does not support the audio element.
  24. </audio>
  25. </div>
  26. </div>
  27. </div>
  28. <div class="custom-file" style="margin-top:auto">
  29. <input type="file" class="custom-file-input" id="files">
  30. <label class="custom-file-label" for="files">Choose file</label>
  31. </div>
  32. <!--<input class="form-control" type="file" id="files" name="file" style="width:100%;height: 100%;">-->
  33. </div>
  34. <div class="col-2 d-flex flex-column">
  35. <div class="padding row" style="margin-top:auto;margin-bottom:auto;">
  36. <button type="button" onclick="document.getElementById('output').value=imageB64" class="col-12 btn btn-info"><span class="spinner-border spinner-border-sm" id="loading" role="status" aria-hidden="true" style="display:none"></span><i class="fas fa-long-arrow-alt-right"></i></button>
  37. </div>
  38. </div>
  39. <div class="padding col-5 d-flex flex-column">
  40. <div style="height: 50%" class="d-flex flex-column">
  41. <p style="margin-bottom: 0; text-align: left;">Base64:</p>
  42. <div class="flex-grow-1">
  43. <textarea class="form-control" id="output1" name="output" placeholder="Output will be displayed here..." spellcheck="false" style="width: 100%;height: 100%;"></textarea>
  44. </div>
  45. <button id="download" type="button" class="btn btn-info mt-3" onclick="copyToClipboard('output1')" style="margin-top:auto;">Copy to clipboard!</button>
  46. </div>
  47. <div style="height: 50%;" class="d-flex flex-column">
  48. <p style="margin-top:1.5em; margin-bottom: 0; text-align: left;">Base64 for use in &lt;object&gt; html elements:</p>
  49. <div class="flex-grow-1">
  50. <textarea class="form-control" id="output2" name="output" placeholder="Output will be displayed here..." spellcheck="false" style="width: 100%;height: 100%;"></textarea>
  51. </div>
  52. <button id="download" type="button" class="btn btn-info mt-3" onclick="copyToClipboard('output2')" style="margin-top:auto;">Copy to clipboard!</button>
  53. </div>
  54. </div>
  55. </div>
  56. <script type="text/javascript">
  57. // Add the following code if you want the name of the file appear on select
  58. $(".custom-file-input").on("change", function() {
  59. var fileName = $(this).val().split("\\").pop();
  60. $(this).siblings(".custom-file-label").addClass("selected").html(fileName);
  61. });
  62. function copyToClipboard(output) {
  63. var copyText = document.getElementById(output);
  64. copyText.select();
  65. document.execCommand("copy");
  66. }
  67. function download() {
  68. var url = document.getElementById('output3').src.replace(/^data:image\/[^;]+/, 'data:image/png');
  69. window.open(url);
  70. }
  71. function toimage() {
  72. var string = document.getElementById('input3').value;
  73. var image = "data:image/png;base64," + string;
  74. document.getElementById('output3').src = image;
  75. document.getElementById('download').disabled = false;
  76. }
  77. function totext() {
  78. var Base64 = { _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", encode: function(e) { var t = ""; var n, r, i, s, o, u, a; var f = 0;
  79. e = Base64._utf8_encode(e); while (f < e.length) { n = e.charCodeAt(f++);
  80. r = e.charCodeAt(f++);
  81. i = e.charCodeAt(f++);
  82. s = n >> 2;
  83. o = (n & 3) << 4 | r >> 4;
  84. u = (r & 15) << 2 | i >> 6;
  85. a = i & 63; if (isNaN(r)) { u = a = 64 } else if (isNaN(i)) { a = 64 } t = t + this._keyStr.charAt(s) + this._keyStr.charAt(o) + this._keyStr.charAt(u) + this._keyStr.charAt(a) } return t }, decode: function(e) { var t = ""; var n, r, i; var s, o, u, a; var f = 0;
  86. e = e.replace(/\\+\\+[++^A-Za-z0-9+/=]/g, ""); while (f < e.length) { s = this._keyStr.indexOf(e.charAt(f++));
  87. o = this._keyStr.indexOf(e.charAt(f++));
  88. u = this._keyStr.indexOf(e.charAt(f++));
  89. a = this._keyStr.indexOf(e.charAt(f++));
  90. n = s << 2 | o >> 4;
  91. r = (o & 15) << 4 | u >> 2;
  92. i = (u & 3) << 6 | a;
  93. t = t + String.fromCharCode(n); if (u != 64) { t = t + String.fromCharCode(r) } if (a != 64) { t = t + String.fromCharCode(i) } } t = Base64._utf8_decode(t); return t }, _utf8_encode: function(e) { e = e.replace(/\r\n/g, "\n"); var t = ""; for (var n = 0; n < e.length; n++) { var r = e.charCodeAt(n); if (r < 128) { t += String.fromCharCode(r) } else if (r > 127 && r < 2048) { t += String.fromCharCode(r >> 6 | 192);
  94. t += String.fromCharCode(r & 63 | 128) } else { t += String.fromCharCode(r >> 12 | 224);
  95. t += String.fromCharCode(r >> 6 & 63 | 128);
  96. t += String.fromCharCode(r & 63 | 128) } } return t }, _utf8_decode: function(e) { var t = ""; var n = 0; var r = c1 = c2 = 0; while (n < e.length) { r = e.charCodeAt(n); if (r < 128) { t += String.fromCharCode(r);
  97. n++ } else if (r > 191 && r < 224) { c2 = e.charCodeAt(n + 1);
  98. t += String.fromCharCode((r & 31) << 6 | c2 & 63);
  99. n += 2 } else { c2 = e.charCodeAt(n + 1);
  100. c3 = e.charCodeAt(n + 2);
  101. t += String.fromCharCode((r & 15) << 12 | (c2 & 63) << 6 | c3 & 63);
  102. n += 3 } } return t } }
  103. // Define the string
  104. var string = document.getElementById('input4').value;
  105. // Encode the String
  106. var decodedString = Base64.decode(string);
  107. document.getElementById('output4').value = decodedString;
  108. }
  109. function tobase64() {
  110. var Base64 = { _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", encode: function(e) { var t = ""; var n, r, i, s, o, u, a; var f = 0;
  111. e = Base64._utf8_encode(e); while (f < e.length) { n = e.charCodeAt(f++);
  112. r = e.charCodeAt(f++);
  113. i = e.charCodeAt(f++);
  114. s = n >> 2;
  115. o = (n & 3) << 4 | r >> 4;
  116. u = (r & 15) << 2 | i >> 6;
  117. a = i & 63; if (isNaN(r)) { u = a = 64 } else if (isNaN(i)) { a = 64 } t = t + this._keyStr.charAt(s) + this._keyStr.charAt(o) + this._keyStr.charAt(u) + this._keyStr.charAt(a) } return t }, decode: function(e) { var t = ""; var n, r, i; var s, o, u, a; var f = 0;
  118. e = e.replace(/\\+\\+[++^A-Za-z0-9+/=]/g, ""); while (f < e.length) { s = this._keyStr.indexOf(e.charAt(f++));
  119. o = this._keyStr.indexOf(e.charAt(f++));
  120. u = this._keyStr.indexOf(e.charAt(f++));
  121. a = this._keyStr.indexOf(e.charAt(f++));
  122. n = s << 2 | o >> 4;
  123. r = (o & 15) << 4 | u >> 2;
  124. i = (u & 3) << 6 | a;
  125. t = t + String.fromCharCode(n); if (u != 64) { t = t + String.fromCharCode(r) } if (a != 64) { t = t + String.fromCharCode(i) } } t = Base64._utf8_decode(t); return t }, _utf8_encode: function(e) { e = e.replace(/\r\n/g, "\n"); var t = ""; for (var n = 0; n < e.length; n++) { var r = e.charCodeAt(n); if (r < 128) { t += String.fromCharCode(r) } else if (r > 127 && r < 2048) { t += String.fromCharCode(r >> 6 | 192);
  126. t += String.fromCharCode(r & 63 | 128) } else { t += String.fromCharCode(r >> 12 | 224);
  127. t += String.fromCharCode(r >> 6 & 63 | 128);
  128. t += String.fromCharCode(r & 63 | 128) } } return t }, _utf8_decode: function(e) { var t = ""; var n = 0; var r = c1 = c2 = 0; while (n < e.length) { r = e.charCodeAt(n); if (r < 128) { t += String.fromCharCode(r);
  129. n++ } else if (r > 191 && r < 224) { c2 = e.charCodeAt(n + 1);
  130. t += String.fromCharCode((r & 31) << 6 | c2 & 63);
  131. n += 2 } else { c2 = e.charCodeAt(n + 1);
  132. c3 = e.charCodeAt(n + 2);
  133. t += String.fromCharCode((r & 15) << 12 | (c2 & 63) << 6 | c3 & 63);
  134. n += 3 } } return t } }
  135. // Define the string
  136. var string = document.getElementById('input2').value;
  137. // Encode the String
  138. var encodedString = Base64.encode(string);
  139. document.getElementById('output2').value = encodedString;
  140. }
  141. </script>
  142. <script>
  143. function handleFileSelect(evt) {
  144. document.getElementById("loading").style.display = "";
  145. var files = evt.target.files; // FileList object
  146. // Loop through the FileList and render image files as thumbnails.
  147. for (var i = 0, f; f = files[i]; i++) {
  148. // Only process image files.
  149. if (!f.type.match('audio/')) {
  150. continue;
  151. }
  152. var reader = new FileReader();
  153. // Closure to capture the file information.
  154. reader.onload = (function(theFile) {
  155. return function(e) {
  156. // Render thumbnail.
  157. document.getElementById('output4').src = e.target.result;
  158. document.getElementById("output2").value = e.target.result;
  159. document.getElementById("output1").value = e.target.result.split(',')[1];
  160. imageB64 = e.target.result;
  161. document.getElementById("loading").style.display = "none";
  162. document.getElementById('audio').load();
  163. };
  164. })(f);
  165. // Read in the image file as a data URL.
  166. reader.readAsDataURL(f);
  167. }
  168. }
  169. document.getElementById('files').addEventListener('change', handleFileSelect, false);
  170. </script>
  171. {% endblock %}