YouTube Transcript Export

Estrae tutti i sottotitoli da un video di YouTube e li esporta in formato .txt o .docx - Script to transcribe youtube videos and export them to txt or docx format

  1. // ==UserScript==
  2. // @name YouTube Transcript Export
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.8
  5. // @description Estrae tutti i sottotitoli da un video di YouTube e li esporta in formato .txt o .docx - Script to transcribe youtube videos and export them to txt or docx format
  6. // @author Magneto1
  7. // @match https://www.youtube.com/watch*
  8. // @grant GM_xmlhttpRequest
  9. // @connect youtube.com
  10. // @require https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2.0.5/FileSaver.min.js
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17. let transcriptText = "";
  18. const recordedSubtitles = new Set(); // Utilizza un Set per evitare duplicati
  19.  
  20. // Funzione per estrarre i sottotitoli dal DOM
  21. function extractTranscript() {
  22. const transcriptElements = document.querySelectorAll('.ytp-caption-segment');
  23. transcriptElements.forEach(element => {
  24. const subtitleText = element.innerText;
  25. if (!recordedSubtitles.has(subtitleText)) {
  26. transcriptText += subtitleText + "\n";
  27. recordedSubtitles.add(subtitleText); // Aggiungi il sottotitolo al Set
  28. }
  29. });
  30. }
  31.  
  32. // Funzione per esportare la trascrizione
  33. function exportTranscript() {
  34. const format = prompt("In quale formato vuoi esportare la trascrizione? (txt/docx)", "txt");
  35.  
  36. if (format === "txt") {
  37. const blob = new Blob([transcriptText], { type: "text/plain;charset=utf-8" });
  38. saveAs(blob, "trascrizione.txt");
  39. } else if (format === "docx") {
  40. const docxContent = `
  41. <!DOCTYPE html>
  42. <html xmlns:w="urn:schemas-microsoft-com:office:word">
  43. <head>
  44. <meta charset="utf-8">
  45. <title>Trascrizione</title>
  46. </head>
  47. <body>
  48. <pre>${transcriptText}</pre>
  49. </body>
  50. </html>
  51. `;
  52. const blob = new Blob([docxContent], { type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document" });
  53. saveAs(blob, "trascrizione.docx");
  54. } else {
  55. alert("Formato non valido. Usa 'txt' o 'docx'.");
  56. }
  57. }
  58.  
  59. // Funzione per avviare l'estrazione dei sottotitoli
  60. function startExtracting() {
  61. transcriptText = ""; // Resetta il testo della trascrizione
  62. recordedSubtitles.clear(); // Resetta il Set dei sottotitoli registrati
  63.  
  64. const interval = setInterval(() => {
  65. extractTranscript();
  66. }, 1000); // Estrae i sottotitoli ogni secondo
  67.  
  68. // Aggiungi un pulsante per fermare l'estrazione e esportare
  69. const stopButton = document.createElement("button");
  70. stopButton.innerText = "Ferma e Esporta Trascrizione";
  71. stopButton.style.position = "fixed";
  72. stopButton.style.bottom = "10px"; // Posizione in basso
  73. stopButton.style.left = "150px"; // Posizione a sinistra
  74. stopButton.style.zIndex = "1000";
  75. stopButton.onclick = function() {
  76. clearInterval(interval);
  77. exportTranscript();
  78. document.body.removeChild(stopButton); // Rimuovi il pulsante dopo l'uso
  79. };
  80.  
  81. document.body.appendChild(stopButton);
  82. }
  83.  
  84. // Aggiungi un pulsante per avviare l'estrazione
  85. const button = document.createElement("button");
  86. button.innerText = "Inizia Estrazione Trascrizione";
  87. button.style.position = "fixed";
  88. button.style.bottom = "10px"; // Posizione in basso
  89. button.style.left = "10px"; // Posizione a sinistra
  90. button.style.zIndex = "1000";
  91. button.onclick = function() {
  92. startExtracting();
  93. button.disabled = true; // Disabilita il pulsante dopo l'avvio
  94. };
  95.  
  96. document.body.appendChild(button);
  97. })();