advplyr / audiobookshelf

Self-hosted audiobook and podcast server
https://audiobookshelf.org
GNU General Public License v3.0
5.83k stars 404 forks source link

[Bug]: Audiobook restarts after a few seconds when using m4b files. #2405

Closed 20CCAA07 closed 7 months ago

20CCAA07 commented 7 months ago

Describe the issue

I use Audiobookshelf on iOS within Safari as PWA. When trying to listen to an audiobook which consists of one m4b file, Audiobookshelf will start playing, then stopps after 10 to 15 seconds and then starts again from the beginning. This behaviour repeats itself several times. Sometimes Audiobookshelf will then continue to play and sometimes it will simply stop or jump forth and back.

If I isten to an audiobook with multiple mp3-files (one mp3 file per chapter), I did not observe this. All audiobooks with mp3 files have been playing as expected from start to finish so far.

I host Audiobookshelf on my Synology NAS within a Docker Container. I can only reproduce this, when I use Audiobookshelf on a mobile device (tried on three different devices with iOS 15, 16 and 17). On my Desktop-PC, m4b-audiobooks work flawlessly in Audiobookshelf (which proves that the files itself are not broken).

Steps to reproduce the issue

  1. Use Safari on iOS (tested in iOS 15, 16 and 17)
  2. Start an audiobook which consists of an m4b file

Audiobookshelf version

v2.6.0

How are you running audiobookshelf?

Docker

advplyr commented 7 months ago

Do you have another browser on your device that you can test with?

20CCAA07 commented 7 months ago

Yes, I have just tried it with Vivaldi and my wife tried it with Chrome. We both got the same result. I can just download Firefox and try again.

20CCAA07 commented 7 months ago

Just tested with Firefox. Unfortunately, I got the same result.

advplyr commented 7 months ago

I've just tested this on iOS 16 with Safari and it is working well for me so my guess is this is something to do with the m4b file not being able to direct play in that browser.

DEBUG: [PlaybackSessionManager] "root" starting direct play session for item "e591d1-ad77-4723-b090-71352a405" with id cbb95-2b4c-45ac-9c1b-17235f7 (Device: iOS 16.7.2 / Mobile Safari)

If you check the server logs (after enabling Debug logs in config -> logs) you should see the direct play attempt then if that failed in the browser for whatever reason it is going to make another request to start a transcode play session. If that transcode is happening you will see a bunch more logs about it.

20CCAA07 commented 7 months ago

Thanks for the hint. I just checked the logs. When I hit the play button on a mobile device, it first says:

[INFO] FFMPEG transcoding started with command: ffmpeg -seek_timestamp 1 -f concat -safe 0 -ss 198s -noaccurate_seek -i /metadata/streams/a99f8dd9-c4d9-4cd7-ae9f-397e3a372523/files.txt -y -loglevel error -map 0:a -c:a aac -f hls -copyts -avoid_negative_ts make_non_negative -max_delay 5000000 -max_muxing_queue_size 2048 -hls_time 6 -hls_segment_type mpegts -start_number 33 -hls_playlist_type vod -hls_list_size 0 -hls_allow_cache 0 -hls_segment_filename /metadata/streams/a99f8dd9-c4d9-4cd7-ae9f-397e3a372523/output-%d.ts /metadata/streams/a99f8dd9-c4d9-4cd7-ae9f-397e3a372523/final-output.m3u8

... then the log spams a lot of this:

WARN File path does not exist /metadata/streams/a99f8dd9-c4d9-4cd7-ae9f-397e3a372523/output-44.ts 2023-12-09 21:28:14.931 INFO [HlsRouter] Stream a99f8dd9-c4d9-4cd7-ae9f-397e3a372523 is currently resetting 2023-12-09 21:28:14.958

... and then, after a lot of similar messages like the above, the log states:

DEBUG [PlaybackSessionManager] syncSession "a99f8dd9-c4d9-4cd7-ae9f-397e3a372523" (Device: iOS 16.7.2 / Mobile Safari) | Total Time Listened: 20 2023-12-09 21:28:17.643 DEBUG [ApiCacheManager] playbackSession.afterCreate: Clearing cache

... and:

[STREAM] Segment #31 Request is before starting segment number #33 - Reset Transcode 2023-12-09 21:28:17.832 INFO [FFMPEG] Transcode Killed

... and finally:

[STREAM] Starting Stream at startTime 2:06.0 (User startTime 2:36.0) and Segment #21 2023-12-09 21:28:18.331 DEBUG [Stream] Forcing AAC for tracks with mime type audio/mp4 and codec aac 2023-12-09 21:28:18.340 INFO [INFO] FFMPEG transcoding started with command: ffmpeg -seek_timestamp 1 -f concat -safe 0 -ss 126s -noaccurate_seek -i /metadata/streams/a99f8dd9-c4d9-4cd7-ae9f-397e3a372523/files.txt -y -loglevel error -map 0:a -c:a aac -f hls -copyts -avoid_negative_ts make_non_negative -max_delay 5000000 -max_muxing_queue_size 2048 -hls_time 6 -hls_segment_type mpegts -start_number 21 -hls_playlist_type vod -hls_list_size 0 -hls_allow_cache 0 -hls_segment_filename /metadata/streams/a99f8dd9-c4d9-4cd7-ae9f-397e3a372523/output-%d.ts /metadata/streams/a99f8dd9-c4d9-4cd7-ae9f-397e3a372523/final-output.m3u8

When I play the audiobook on my Desktop-PC, the only log message is:

2023-12-09 21:39:53.804 DEBUG [PlaybackSessionManager] startSessionRequest for device Windows 10 / Chrome 2023-12-09 21:39:53.805 DEBUG [PlaybackSessionManager] "user" starting direct play session for item "d7955999-143e-405c-8e4d-3aa6bf199bb0" with id 1da3f9f7-1387-4cea-b896-8f51c2442459 (Device: Windows 10 / Chrome)

20CCAA07 commented 7 months ago

One addition. It seems like I overlooked the important log entry when start playing on a mobile:

2023-12-09 21:08:38.718 ERROR Ffmpeg Err "ffmpeg exited with code 1: "

2023-12-09 21:08:38.719 INFO [Stream] Re-attempting stream with AAC encode

advplyr commented 7 months ago

There are a lot of issues that have been opened related to m4b files that were encoded in a way that cannot be direct played on devices. Most of the time the transcode will work for these still after forcing AAC so I'm not sure why it isn't for you.

Some related issues https://github.com/advplyr/audiobookshelf/issues/1872 https://github.com/advplyr/audiobookshelf/issues/172 https://github.com/advplyr/audiobookshelf/issues/910

Maybe there is some info in there that would make sense to your m4b files. Like they were de-DRMed

advplyr commented 7 months ago

Also https://github.com/advplyr/audiobookshelf/issues/2157 where we originally discussed the force AAC fallback for these m4bs

20CCAA07 commented 7 months ago

Ok, thanks! I will take a look at the related issues tomorrow and report back if this fits to the audiobooks which cause problems in my case.

20CCAA07 commented 7 months ago

Here is the promised feedback: I have read through the issues you mentioned and believe that my problem is related to the one described in #172 . After using a script as described within the write up linked here in #172 (https://rentry.co/n4ost) the problematic file seems to no longer cause trouble within audiobookshelf (I have listened to the first 15 minutes without any restarts so far and the logfile looked "clean" as well).

advplyr commented 7 months ago

Ah yeah it may be worth creating a tool in Abs that can fix these since it's common but it's a poorly encoded audio file by Audible that I wouldn't consider a bug with Abs