如何在關閉分頁後繼續轉換有聲書
How I Resume an Audiobook Conversion After Closing the Tab
All to Audiobook 會將文件轉為語音,並在每段音訊完成後先儲存,讓用戶關閉分頁後仍可接續轉換。重新開啟網站時,工作會暫停;用戶按下 Continue 後,程式會略過已儲存的段落,只生成尚未完成的音訊。工作及音訊儲存在該裝置的瀏覽器空間,不會跨裝置同步,清除網站資料也會刪除本機副本。
Part 2 of Building All to Audiobook
A browser tab can close halfway through a book. If the audio only exists in memory, reopening the page means making it again. I save each finished passage before moving to the next one.
Building All to Audiobook
I ran into these problems while building All to Audiobook for my mom. After neck surgery, holding a book became difficult for her, and many of the ebooks she wanted to read had no audiobook edition. I wanted her to turn books she already had into audio she could keep and listen to in her usual player.
The app processes documents and generates speech on the reader's own device, then exports MP3 or chaptered M4B files. This series follows the practical problems I encountered while building it, the approaches I chose, and what still needs work.
In the first part, I wrote about preparing PDF text for narration. Once that text reaches the voice model, I need to keep the work it produces. A whole book can outlast the time someone wants to keep a browser tab open.
What has to survive closing the tab?
The saved job needs the text it started with, the selected voice, and a record of its finished audio.
I copy the document when a conversion starts. The job also keeps the prepared chapter text, split into passages. Editing the working document later leaves that saved copy intact.
Without the copy, resuming could mix audio made from an earlier version with text the reader has since changed. I want Continue to finish the job they started. A new edit belongs to a new conversion.
Each passage gets an ID based on the job, chapter and passage position. The same passage has the same ID when I reopen the job. That gives the runner something specific to look up before asking the model to generate more audio.
Save one passage before starting the next
The voice worker returns audio samples for a passage. I encode those samples as MP3, write the file, and then save its record. That record includes the passage text, its position in the chapter, and its duration.
Job and passage records live in IndexedDB. The audio files normally go into the browser's origin private file system, or OPFS. These are files kept for the site inside browser storage. They do not appear in the reader's Downloads folder.
When OPFS cannot be used, the storage layer can put the audio blob in IndexedDB instead. Either way, I wait for the storage operation to finish before moving on. For IndexedDB, the helper waits for the transaction to complete.
This puts the saving point inside the chapter. If the reader closes the tab before the next passage finishes saving, that passage has to be made again. The earlier saved passages remain available.
An example sequence within one chapter. A finished storage write is what makes a passage available after reopening.
Reopen into a paused job
When the reader returns, I load the job's document copy, chapter records and saved passages from local storage. A job left marked as running becomes paused. The reader presses Continue when they are ready.
The runner walks through the stored passage list in order. For each passage, it checks whether a corresponding audio record already exists. It skips a successfully saved passage and generates one that is missing.
I keep errors in the saved records too. A passage that failed to generate is marked as a failure, so the app can offer a retry. Its presence in the database does not mean there is usable audio for it.
Once a chapter is ready, the export step reads its saved audio in chapter and passage order. It can build the download from those files without asking the voice model to read the chapter again.
The storage tests mark a job as running, apply the recovery step, and check that its saved pieces remain. They also check that automatic continuation stays off. Another test changes the working document after creating a job and checks that the saved copy still contains the original text.
Those checks matter together. Keeping the audio would be less useful if reopening silently changed which text the rest of the job would read.
The browser still owns this storage
Closing the tab stops the work. The app resumes when the reader returns and presses Continue; it does not keep making audio with the page closed.
The saved job also belongs to that browser's storage on that device. Signing into the same account on another computer does not bring the book or its audio along. Clearing the site's data removes the local copy.
I request persistent storage, which the browser can grant or refuse. Storage limits still apply. If the browser reports that its storage is full, the conversion pauses with an error. The reader can download or remove a finished job before continuing.
That is why I keep saving a job and downloading an audiobook as separate steps. The saved job lets the reader continue making it. The downloaded MP3 or M4B is the file they can keep and move to their usual player.
You can try All to Audiobook's PDF-to-MP3 conversion with a short chapter. Let a few passages finish, pause, and reopen the site in the same browser and account. Continue should pick up the remaining work while keeping the saved audio.
Next in the series: voice consistency, checking for missing words, and remaking an individual passage.
來源:DEV Community · dev.to
