advplyr / audiobookshelf

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

[Bug]: Some audiobooks refuse to play. #1872

Closed Bitwolfies closed 1 year ago

Bitwolfies commented 1 year ago

Describe the issue

I wish I could offer more, but some audiobooks ripped from openaudible refuse to play, I thought it was due to my tag editing, but even untouched ones fail, and the ffmpeg logs are of no help.

Files play fine in VLC, Elisa, even the Synology DSM audioplayer.

If there is any way for me to send info on the file I will, as I get the legal grey area of actually sending you affected files.

2023-06-27 01:33:56

INFO

[STREAM] START STREAM - Num Segments: 4972

2023-06-27 01:33:56

INFO

[INFO] FFMPEG transcoding started with command: ffmpeg -seek_timestamp 1 -f concat -safe 0 -i /metadata/streams/play_5rlpoecr8u1draa3rl/files.txt -y -loglevel error -map 0:a -c:a copy -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 0 -hls_playlist_type vod -hls_list_size 0 -hls_allow_cache 0 -hls_segment_filename /metadata/streams/play_5rlpoecr8u1draa3rl/output-%d.ts /metadata/streams/play_5rlpoecr8u1draa3rl/final-output.m3u8

2023-06-27 01:33:56

INFO

2023-06-27 01:33:56

INFO

[adts @ 0x7fef1376f2c0] Scalable configurations are not allowed in ADTS

2023-06-27 01:33:56

INFO

[out#0/hls @ 0x7fef1383bcc0] Could not write header (incorrect codec parameters ?): Invalid data found when processing input

2023-06-27 01:33:56

INFO

[aost#0:0/copy @ 0x7fef128c4900] Error initializing output stream:

2023-06-27 01:33:56

INFO

2023-06-27 01:33:56

INFO

2023-06-27 01:33:56

ERROR

Ffmpeg Err "ffmpeg exited with code 1: "

Audiobookshelf version

v2.2.23

How are you running audiobookshelf?

Docker

GlozingNeuter commented 1 year ago

Same issue on v2.2.23 through Docker with a .m4b file (457MB) at a 44.1kHz sample rate. Could it be related to #172 ?

advplyr commented 1 year ago

Possibly related to #172. I would need a sample audio file to reproduce this

Bitwolfies commented 1 year ago

Possibly related to #172. I would need a sample audio file to reproduce this

I have no issue providing a sample file if you have a method.

advplyr commented 1 year ago

Have you used OpenAudible to convert your audiobooks?

Bitwolfies commented 1 year ago

Have you used OpenAudible to convert your audiobooks?

Yes, may very well be an issue with open audible, but as said even the most obscure players play them fine.

gianmarcotoso commented 1 year ago

@advplyr I have an easy reproduction for this, at least it seems to be consistent as far as I can tell. Downloading books from Audible you get AAX files that have filenames that end with either ep6 or ep7, I'm not sure what it means but I'd guess it's a quality format thing. Using AAX Audio Converter to convert these books to m4b yields files that are playable without issues via Chrome or via the Android App, but with Firefox (that doesn't probably support the codec) transcoding is required. All ep6 files play without issues, while all ep7 files fail to play. I can, however, transcode them manually with ffmpeg with no problems using this command:

ffmpeg -i INPUT.m4b -c:a libmp3lame -q:a 2 OUTPUT.mp3

Incurring in a quality loss, of course.

To recap:

  1. Download AAX from the Audible Website (NOT OpenAudible, as it seems to download AAXC files)
  2. Check if the filename ends with ep7
  3. Convert to m4b using AAX Audio Converter
  4. Move to Audiobookshelf library
  5. Attempt to play from Firefox, the logs should show the errors mentioned in the issue

All three acts of The Sandman are in ep7 format, so if you purchased those you can test with them, otherwise most new books should also be in this format.

EDIT: AAX Audio Converter requires activation bytes, you can get those from here using any book downloaded from Audible's website (it won't be uploaded, just parsed locally).

advplyr commented 1 year ago

@gianmarcotoso Thanks for breaking this down. It may be a while before I dig into this. I'm trying to migrate the current file-based JSON database to a sqlite db. On quick glance it looks like the solution to this will be to update the transcoder located in Stream.js https://github.com/advplyr/audiobookshelf/blob/master/server/objects/Stream.js#L250

I've been using -c:a aac for some codecs and mime types when creating the HLS stream

gianmarcotoso commented 1 year ago

Thank you and thanks for pointing out a starting point, if I get some time I'll attempt a PR myself!

gianmarcotoso commented 1 year ago

@advplyr Upon further investigation, the fix is already in place and strangely enough it does work on a clean dev environment. On my production server, however, the error outputted by ffmpeg is slightly different, and therefore is not caught by the temporary workaround.

The error in my dev container:

[2023-07-12 08:40:55] ERROR: Ffmpeg Err "ffmpeg exited with code 1: Could not write header for output file #0 (incorrect codec parameters ?): Invalid data found when processing input

The error on my production server:

audiobookshelf  | [2023-07-12 08:41:01] INFO: 
audiobookshelf  | [2023-07-12 08:41:01] INFO: [adts @ 0x7f5cabe7d500] Scalable configurations are not allowed in ADTS
audiobookshelf  | [2023-07-12 08:41:01] INFO: [out#0/hls @ 0x7f5caac5bac0] Could not write header (incorrect codec parameters ?): Invalid data found when processing input
audiobookshelf  | [2023-07-12 08:41:01] INFO: [aost#0:0/copy @ 0x7f5caac49940] Error initializing output stream: 
audiobookshelf  | [2023-07-12 08:41:01] INFO: 
audiobookshelf  | [2023-07-12 08:41:01] INFO: 
audiobookshelf  | [2023-07-12 08:41:01] ERROR: Ffmpeg Err "ffmpeg exited with code 1: 
audiobookshelf  | " (Stream.js:340)

Changing the condition to force re-encoding from err.message.startsWith to err.message.includes might solve the issue but in order to test this I would have to build a docker image to run on my production server instead of the official one. Do you have any pointers on how to do that?

That said, I'm going to investigate further to see if there is some way of detecting this problem earlier and force transcoding without failing first, as it would speed up the start of the playback session. The files I'm analyzing (one good, one bad) are both AAC encoded, so there must be some other property to check. Also, this only happens in Firefox, so saving the user-agent and forcing the workaround only for Firefox clients might help.

I'll keep you posted.

advplyr commented 1 year ago

The error on your production server is not showing anything after the : so it doesn't look like changing to includes would do anything.

The only difference between the production and development server would be the log level. I'm not sure if this would make a difference but you could try changing

https://github.com/advplyr/audiobookshelf/blob/78fe6d47ba2c572b824f01f7d109463b006c2f0d/server/objects/Stream.js#L276

to const logLevel = process.env.NODE_ENV === 'production' ? 'error' : 'error

Then you can see if that is the reason the production side is different. If that is the case then we can just set the loglevel to warning on both dev & prod

JonesGreen commented 1 year ago

Hi, I had the same error and found this: https://rentry.co/n4ost I changed my m4b file and it worked fine again. But I believe the file worked sometime in the past. But I don't know when it got broken.

gianmarcotoso commented 1 year ago

Hi, I had the same error and found this: https://rentry.co/n4ost I changed my m4b file and it worked fine again. But I believe the file worked sometime in the past. But I don't know when it got broken.

Thanks for the tip, I've tried it and it does indeed work. The best solution, I think, would then be to detect broken files (maybe during library scan) and allow for a one-click fix from the UI, so that unnecessary transcoding is avoided. This might require a bit of work though, I'll try to investigate.

patrickrobbins commented 1 year ago

I found a file with this issue as well today, I didn't notice for a while b/c it worked fine playing locally downloaded to an android device. It seemed like it was working and then broke mid file, but I think I had only listened on mobile at that point.

gianmarcotoso commented 1 year ago

Since it's not a common occurrence, right now the easiest way to fix this is to fix the problematic files directly. I have added this script to my path:

#!/bin/bash
#filename: fix-aac

f="$1"
t=`mktemp`
mp4extract-bento4 moov/trak/mdia/minf/stbl/stsd/mp4a/esds \
    "$f" /dev/stdout | xxd -p | tr -d '\n' >$t
magic=$(sed -re 's/^.*0580808002(....).*$/\1/' $t)
if [ "$magic" != "1212" ]; then
    echo "no need to fix"
    rm "$t"
    exit 0
fi
t2=`mktemp`
new=$(sed -re 's/05808080021212/05808080021210/' $t | xxd -r -p >$t2)
rm $t
old="${f}.pre-fix"
mv -v "$f" "$old"
mp4edit --replace \
    moov/trak/mdia/minf/stbl/stsd/mp4a/esds:"$t2" \
    "$old" \
    "$f"

It's the same one from the link mentioned above (it does not appear to be working right now though), modified to make it work using the bento4 AUR package on Arch.

Then I did a simple:

find . -name "*.m4b" -exec fix-aac {} \;

In my library folder, deleted the old files and restarted audiobookshelf for good measure.

jufy111 commented 1 year ago

I have been having similar issues, files don't play on Firefox but will play fine on Edge browser, so it could be an issue with Firefox.

Is anyone else able to test if using a different browser will allow for playback of problematic files?

I have just resorted to re-encoding the problematic files with FFMPEG.

gianmarcotoso commented 1 year ago

I have been having similar issues, files don't play on Firefox but will play fine on Edge browser, so it could be an issue with Firefox.

Is anyone else able to test if using a different browser will allow for playback of problematic files?

I have just resorted to re-encoding the problematic files with FFMPEG.

I believe it's the same issue. Instead of re-encoding you could give a try to the method I've proposed in my previous comment, it takes a lot less to complete (a couple of seconds per file).

advplyr commented 1 year ago

This is an issue with the audio file itself and not Abs but in the next release they should properly fallback to transcoding with #2171. Discussed also in #2157

Bitwolfies commented 1 year ago

To update, the issue is that firefox seemingly cant play 44.1hz files, but it can play 22hz fine. 44hz plays fine in the android app with no transcoidng, so its good to see that trancoidng kicks in when needed.

AudioAnchorite commented 7 months ago

Since it's not a common occurrence, right now the easiest way to fix this is to fix the problematic files directly. I have added this script to my path:

#!/bin/bash
#filename: fix-aac

f="$1"
t=`mktemp`
mp4extract-bento4 moov/trak/mdia/minf/stbl/stsd/mp4a/esds \
    "$f" /dev/stdout | xxd -p | tr -d '\n' >$t
magic=$(sed -re 's/^.*0580808002(....).*$/\1/' $t)
if [ "$magic" != "1212" ]; then
    echo "no need to fix"
    rm "$t"
    exit 0
fi
t2=`mktemp`
new=$(sed -re 's/05808080021212/05808080021210/' $t | xxd -r -p >$t2)
rm $t
old="${f}.pre-fix"
mv -v "$f" "$old"
mp4edit --replace \
    moov/trak/mdia/minf/stbl/stsd/mp4a/esds:"$t2" \
    "$old" \
    "$f"

It's the same one from the link mentioned above (it does not appear to be working right now though), modified to make it work using the bento4 AUR package on Arch.

Then I did a simple:

find . -name "*.m4b" -exec fix-aac {} \;

In my library folder, deleted the old files and restarted audiobookshelf for good measure.

Hi could anyone explain how this works? I am running Windows 11, and I need to fix a few audiobooks with this glitch because they don't play properly in iTunes or on my iPad or iPhone.

How do I install Bento4 and what format do I save that script as? I do know how to add environmental variables to PATH. Do I save the script itself to the path, or the Bento4 package?