mkb79 / audible-cli

A command line interface for audible package. With the cli you can download your Audible books, cover, chapter files.
GNU Affero General Public License v3.0
423 stars 45 forks source link

fix: decrypt command filename escape #202

Closed devnoname120 closed 3 months ago

devnoname120 commented 3 months ago

Fix #199

subprocess.check_output() is not called with the option shell = True so it doesn't make sense to escape any arguments because ffmpeg will already receive them as is.

When e.g. a filename contains escapable characters, then the quote() function encloses it with quotation marks ''. FFmpeg interprets those literally and they don't match any file (the actual file doesn't start with ').

Here is the error that this PR fixes:

❯ audible --verbosity DEBUG --profile de decrypt --all --rebuild-chapters --force-rebuild-chapters
debug: Audible-cli version: 0.3.1
debug: App dir: /root/.audible
debug: Plugin dir: /root/dev/audible-cli/plugin_cmds
debug: Config loaded from config.toml
debug: Auth file de.json for profile de loaded.
ffmpeg version 5.1.2-0ubuntu1~20.04.sav1 Copyright (c) 2000-2022 the FFmpeg developers
  built with gcc 9 (Ubuntu 9.4.0-1ubuntu1~20.04.1)
  configuration: --prefix=/usr --extra-version='0ubuntu1~20.04.sav1' --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl --disable-stripping --enable-gnutls --enable-ladspa --enable-lcms2 --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libcodec2 --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libgme --enable-libgsm --enable-libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librabbitmq --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libsrt --enable-libssh --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzmq --enable-libzvbi --enable-lv2 --enable-omx --enable-openal --enable-opencl --enable-opengl --enable-sdl2 --enable-sndio --enable-pocketsphinx --enable-librsvg --enable-libdav1d --enable-libjxl --enable-librist --enable-libvmaf --enable-libzimg --enable-crystalhd --enable-libmfx --enable-libsvtav1 --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libx264 --enable-libplacebo --enable-librav1e --enable-shared
  libavutil      57. 28.100 / 57. 28.100
  libavcodec     59. 37.100 / 59. 37.100
  libavformat    59. 27.100 / 59. 27.100
  libavdevice    59.  7.100 / 59.  7.100
  libavfilter     8. 44.100 /  8. 44.100
  libswscale      6.  7.100 /  6.  7.100
  libswresample   4.  7.100 /  4.  7.100
  libpostproc    56.  6.100 / 56.  6.100
'A_Redacted_Book_Name_(that_Contains_Parentheses)_(Some_Editor)-AAX_44_128.aaxc': No such file or directory
Uncaught Exception
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/audible_cli/cli.py", line 60, in main
    sys.exit(cli(*args, **kwargs))
  File "/usr/lib/python3/dist-packages/click/core.py", line 1128, in __call__
    return self.main(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/click/core.py", line 1053, in main
    rv = self.invoke(ctx)
  File "/usr/lib/python3/dist-packages/click/core.py", line 1659, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/lib/python3/dist-packages/click/core.py", line 1395, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/lib/python3/dist-packages/click/core.py", line 754, in invoke
    return __callback(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/click/decorators.py", line 84, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
  File "/usr/lib/python3/dist-packages/click/core.py", line 754, in invoke
    return __callback(*args, **kwargs)
  File "/root/dev/audible-cli/plugin_cmds/cmd_decrypt.py", line 608, in cli
    decrypter.run()
  File "/root/dev/audible-cli/plugin_cmds/cmd_decrypt.py", line 463, in run
    self.rebuild_chapters()
  File "/root/dev/audible-cli/plugin_cmds/cmd_decrypt.py", line 415, in rebuild_chapters
    self.ffmeta.update_chapters_from_chapter_info(
  File "/root/dev/audible-cli/plugin_cmds/cmd_decrypt.py", line 408, in ffmeta
    subprocess.check_output(base_cmd, text=True)  # noqa: S603
  File "/usr/lib/python3.10/subprocess.py", line 421, in check_output
    return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
  File "/usr/lib/python3.10/subprocess.py", line 526, in run
    raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['ffmpeg', '-stats', '-audible_key', '[redacted], '-audible_iv', '[redacted]', '-i', "'A_Redacted_Book_Name_(that_Contains_Parentheses)_(Some_Editor)-AAX_44_128.aaxc'", '-f', 'ffmetadata', '/root/audible-backup-library/tmp/audible/de/A_Redacted_Book_Name_(that_Contains_Parentheses)_(Some_Editor)-AAX_44_128.meta']' returned non-zero exit status 1.

In this example ffmpeg incorrectly looks for the file 'A_Redacted_Book_Name_(that_Contains_Parentheses)_(Some_Editor)-AAX_44_128.aaxc' that doesn't exist. The actual file is A_Redacted_Book_Name_(that_Contains_Parentheses)_(Some_Editor)-AAX_44_128.aaxc and it shouldn't have been quoted.

philgoetz commented 3 months ago

When e.g. a filename contains escapable characters, then the quote() function encloses it with quotation marks ''. FFmpeg interprets those literally and they don't match any file (the actual file doesn't start with ').

This isn't the case if you call ffmpeg on the command line. This works: ffmpeg -stats -audible_key blahblahblah -audible_iv blahblahblah -i 'Dispatches_from_Planet_3Thirty-Two(Brief)_Tales_on_the_Solar_System_the_Milky_Way_and_Beyond-AAX_22_32.aaxc' -c copy '../decrypted/Dispatches_from_Planet_3Thirty-Two(Brief)_Tales_on_the_Solar_System_the_Milky_Way_and_Beyond-AAX_22_32.m4b'

philgoetz commented 3 months ago

You've marked this a "fix", but you never said what the fix for it is.

devnoname120 commented 3 months ago

This isn't the case if you call ffmpeg on the command line.

No shit Sherlock. But here we're not calling ffmpeg from the command line but executing the binary directly:

subprocess.check_output() is not called with the option shell = True


You've marked this a "fix", but you never said what the fix for it is.

It's literally written in the description of the PR.

philgoetz commented 3 months ago

This isn't the case if you call ffmpeg on the command line.

No shit Sherlock. But here we're not calling ffmpeg from the command line but executing the binary directly:

subprocess.check_output() is not called with the option shell = True

You've marked this a "fix", but you never said what the fix for it is.

It's literally written in the description of the PR.

Do you mean that we should replace the line "subprocess.check_output(base_cmd, text=True)" with "subprocess.check_output(base_cmd)"? Or does text need some other argument?

devnoname120 commented 3 months ago

@philgoetz Did you try to apply the changes of this pull request?

https://github.com/mkb79/audible-cli/pull/202/files

philgoetz commented 3 months ago

@philgoetz Did you try to apply the changes of this pull request?

https://github.com/mkb79/audible-cli/pull/202/files

No. Thank you. How was I supposed to know that even existed? I think you're assuming more familiarity with github than I have.