Skip to content

Commit

Permalink
Don't write file each time with timestamps
Browse files Browse the repository at this point in the history
- If the user uses the "Cut with multiple timestamp" feature, ffmpeg-web won't delete the source file until all of the timestamps are converted
- The user can choose when ffmpeg-web should quit the ffmpeg process
  • Loading branch information
dinoosauro authored Sep 2, 2023
1 parent ffc006c commit 253085e
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 18 deletions.
2 changes: 1 addition & 1 deletion dist/index.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/out.js

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion dist/translations/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,11 @@
"languageDesc": "Cambia la lingua utilizzata da ffmpeg-web",
"chooseFile": "Scegli file",
"chooseFileDesc": "Scegli come gestire la selezione dei file multipla",
"smartDesc": "Aggiungi i metadati titolo e numero traccia"
"smartDesc": "Aggiungi i metadati titolo e numero traccia",
"ram": "Gestione della RAM",
"ramDesc": "Gestisci quando il processo ffmpeg debba essere chiuso",
"quitOperation": "Esci alla fine di ogni singola operazione",
"quitTimestamp": "Esci ad ogni taglio del contenuto"
},
"js": {
"added": "Aggiunto",
Expand Down
16 changes: 16 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,22 @@ <h3 data-translate="outputClear">Output clearing</h3>
<l data-translate="maxChar">Maxinum characters:</l><input type="number" min="10" id="maxCharacters" value="150000">
</div>
</div><br>
<div class="rowDisplay textcenter" style="display: block;">
<h3 data-translate="ram">RAM management</h3>
<i data-translate="ramDesc">Manage when ffmpeg-web should exit the current process</i><br><br>
<div class="switchContainer hcenter">
<label class="switch">
<input type="checkbox" id="quitFfmpegGeneral" checked>
<span class="slider"></span>
</label>
<l data-translate="quitOperation">Quit after each operation</l></div><br><br>
<div class="switchContainer hcenter">
<label class="switch">
<input type="checkbox" id="quitFfmpegTimestamp">
<span class="slider"></span>
</label>
<l data-translate="quitTimestamp">Quit for each timestamp cut</l></div><br><br>
</div><br>
<div class="rowDisplay textcenter" style="display: block;">
<h3 data-translate="language">Language</h3>
<i data-translate="languageDesc">Change the language used by ffmpeg-web</i><br><br>
Expand Down
40 changes: 26 additions & 14 deletions script.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,19 +228,20 @@ function getFfmpegItem() { // The function that will manage the start and the en
}
return cutTimestamp;
}
async function ffmpegStart() { // The function that manages most of the ffmpeg conversions
if (conversionOptions.output.custom) readFfmpegScript(); else if (document.querySelector(".sectionSelect").getAttribute("section") === "metadata") await ffmpegReadyMetadata(); else buildFfmpegScript();
async function ffmpegStart(skipImport) { // The function that manages most of the ffmpeg conversions
let finalScript = [];
if (!skipImport) {
if (conversionOptions.output.custom) readFfmpegScript(); else if (document.querySelector(".sectionSelect").getAttribute("section") === "metadata") await ffmpegReadyMetadata(); else buildFfmpegScript();
if (tempOptions.itsscale !== []) finalScript.push(...tempOptions.itsscale); // tempOptions.itsscale contains the options that go before the input files
for (let i = 0; i < tempOptions.ffmpegBuffer.length; i++) { // Add each file to the ffmpeg filesystem
ffmpeg.FS('writeFile', tempOptions.ffmpegName[i], await fetchFile(tempOptions.ffmpegBuffer[i]));
if (conversionOptions.output.custom && tempOptions.ffmpegArray.indexOf(`$input[${i}]`) !== -1) tempOptions.ffmpegArray[tempOptions.ffmpegArray.indexOf(`$input[${i}]`)] = tempOptions.ffmpegName[i]; else finalScript.push("-i", tempOptions.ffmpegName[i]);
tempOptions.deleteFile.push(tempOptions.ffmpegName[i]);
}
} else for (let name of tempOptions.ffmpegName) finalScript.push("-i", name);
if (tempOptions.isSecondCut) { // Cut content by timestamp
let fetchTimestamp = getFfmpegItem();
// Delete the last item of the array (the output file) and insert the start and end of the new item
tempOptions.ffmpegArray.pop();
if (!skipImport) tempOptions.ffmpegArray.pop(); // Delete the last item of the array (the output file) and insert the start and end of the new item. This is necessary only for the first time
tempOptions.ffmpegArray.push("-ss", fetchTimestamp[0]);
if (fetchTimestamp[1] !== "") tempOptions.ffmpegArray.push("-to", fetchTimestamp[1]); else tempOptions.isSecondCut = false;
if (document.getElementById("smartMetadata").checked) tempOptions.ffmpegArray.push("-metadata", `title=${conversionOptions.output.name}`, "-metadata", `track=${conversionOptions.output.dividerProgression}`); // Smart metadata for multiple dividers
Expand Down Expand Up @@ -272,20 +273,27 @@ async function ffmpegStart() { // The function that manages most of the ffmpeg c
document.getElementById("console").innerHTML = consoleText; // Add output text to the console
document.getElementById("console").parentElement.scrollTo({ top: document.getElementById("console").parentElement.scrollHeight, behavior: 'smooth' }); // Scroll to the end of the console text
let textCutSplit = document.getElementById("timestampArea").value.split("\n");
for (let file of tempOptions.deleteFile) try { await ffmpeg.FS('unlink', file) } catch (ex) { console.warn(ex) }; // Delete the files from the ffmpeg file system
tempOptions.deleteFile = []; // Restore the deleteFile array. This is useful especially with multiple timestamp cut, since otherwise ffmpeg-web would continue to try deleting the old files.
if (tempOptions.isSecondCut && textCutSplit.length > tempOptions.secondCutProgress && textCutSplit[tempOptions.secondCutProgress].replaceAll(" ", "").length > 1) { // If there's another timestamp, run again the conversion
tempOptions.ffmpegArray.splice(tempOptions.ffmpegArray.lastIndexOf("-ss"), tempOptions.ffmpegArray.length);
try {
await resetFfmpeg();
await ffmpegStart();
if (document.getElementById("quitFfmpegTimestamp").checked) await resetFfmpeg();
await ffmpegStart(true);
} catch (ex) {
console.warn(ex);
}
}
for (let file of tempOptions.deleteFile) try { await ffmpeg.FS('unlink', file); } catch (ex) { console.warn(ex) }; // Delete the files from the ffmpeg file system
tempOptions.deleteFile = []; // Restore the deleteFile array. This is useful especially with multiple timestamp cut, since otherwise ffmpeg-web would continue to try deleting the old files.
tempOptions = optionGet(); // Delete the conversion-specific informations, so that a new item can be converted
conversionOptions.output.dividerProgression = 0; // Restore the divider progression so that each conversion has its own track progression
if (isMultiCheck[0]) setTimeout(async () => { finalScript = []; await resetFfmpeg(); ffmpegMultiCheck() }, 350); else {document.getElementById("reset").reset(); await resetFfmpeg()} // if antother item must be converted, restart all of this process; otherwise reset the text input so that the user can select another file. In both cases, restore ffmpeg to free memory
if (isMultiCheck[0]) setTimeout(async () => { // if antother item must be converted, restart all of this process; otherwise reset the text input so that the user can select another file. In both cases, restore ffmpeg to free memory
finalScript = [];
if (document.getElementById("quitFfmpegGeneral").checked) await resetFfmpeg();
ffmpegMultiCheck()
}, 350); else {
document.getElementById("reset").reset();
if (document.getElementById("quitFfmpegGeneral").checked) await resetFfmpeg()
}
}
function downloadItem(data, name) { // Function to download a file
downloadName = name !== undefined ? name : `${safeCharacters(conversionOptions.output.name)}.${tempOptions.fileExtension}`; // If no name is provided, fetch the result of the conversion
Expand Down Expand Up @@ -967,13 +975,13 @@ function createAlert(text, noRepeat, showBottom) { // Create an alert at the top
setTimeout(() => deleteAlert(firstAlertContainer), parseInt(localStorage.getItem("ffmpegWeb-alertDuration"))); // Delete the current alert after an amount of ms the user has decided from the settings
for (let item of [noRepeatIndication, img]) addHoverEvents(item);
}
function loadFfmpeg() {
function loadFfmpeg(skipInfo) {
return new Promise((resolve) => {
createAlert(currentTranslation.js.ffmpegLoad, "ffmpegLoading"); // Wait until ffmpeg-web loads the ffmpeg.wasm core component.
if (!skipInfo) createAlert(currentTranslation.js.ffmpegLoad, "ffmpegLoading"); // Wait until ffmpeg-web loads the ffmpeg.wasm core component.
document.getElementById("btnSelect").classList.add("disabled"); // Disable the "Select file" button until it has loaded
if (!ffmpeg.isLoaded()) ffmpeg.load().then(() => {
// ffmpeg is loaded, so the "File select" button can now be clicked
createAlert(currentTranslation.js.successful, "ffmpegSuccessful");
if (!skipInfo) createAlert(currentTranslation.js.successful, "ffmpegSuccessful");
document.getElementById("btnSelect").classList.remove("disabled");
resolve();
}).catch((ex) => {
Expand All @@ -985,7 +993,7 @@ if (!ffmpeg.isLoaded()) ffmpeg.load().then(() => {
loadFfmpeg();
async function resetFfmpeg() {
ffmpeg.exit();
await loadFfmpeg();
await loadFfmpeg(true);
}
// Set up PWA installation prompt: catch the popup and display it when the user clicks the "Install as PWA" button
let installationPrompt;
Expand Down Expand Up @@ -1173,4 +1181,8 @@ for (let lang of supportedLanguages) {
break;
}
}
for (let item of document.querySelectorAll("[data-text]")) item.type = "text"; // Since Webpack delets the "type=text" attribute, it'll be added again from JavaScript
for (let item of document.querySelectorAll("[data-text]")) item.type = "text"; // Since Webpack delets the "type=text" attribute, it'll be added again from JavaScript
document.getElementById("quitFfmpegGeneral").addEventListener("change", () => {document.getElementById("quitFfmpegGeneral").checked ? localStorage.removeItem("ffmpegWeb-GeneralQuit") : localStorage.setItem("ffmpegWeb-GeneralQuit", "a")});
document.getElementById("quitFfmpegTimestamp").addEventListener("change", () => {document.getElementById("quitFfmpegTimestamp").checked ? localStorage.setItem("ffmpegWeb-TimestampQuit", "a") : localStorage.setItem("ffmpegWeb-TimestampQuit", "a")});
if (localStorage.getItem("ffmpegWeb-GeneralQuit") === "a") document.getElementById("quitFfmpegGeneral").checked = false;
if (localStorage.getItem("ffmpegWeb-TimestampQuit") === "a") document.getElementById("quitFfmpegTimestamp").checked = true;
6 changes: 5 additions & 1 deletion translations/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,11 @@
"languageDesc": "Cambia la lingua utilizzata da ffmpeg-web",
"chooseFile": "Scegli file",
"chooseFileDesc": "Scegli come gestire la selezione dei file multipla",
"smartDesc": "Aggiungi i metadati titolo e numero traccia"
"smartDesc": "Aggiungi i metadati titolo e numero traccia",
"ram": "Gestione della RAM",
"ramDesc": "Gestisci quando il processo ffmpeg debba essere chiuso",
"quitOperation": "Esci alla fine di ogni singola operazione",
"quitTimestamp": "Esci ad ogni taglio del contenuto"
},
"js": {
"added": "Aggiunto",
Expand Down

0 comments on commit 253085e

Please sign in to comment.