jiaaro / pydub

Manipulate audio with a simple and easy high level interface
http://pydub.com
MIT License
8.82k stars 1.04k forks source link

Could not export as m4a #755

Closed ksk0629 closed 10 months ago

ksk0629 commented 1 year ago

Steps to reproduce

  1. Load m4a file.
  2. Export it as m4a file.
from pydub import AudioSegment

input_path = "path/to/input/file.m4a"
audio_data = AudioSegment.from_file(input_path)

output_path = "path/to/output/file.m4a"
audio_data.export(output_path, format="m4a")

Expected behavior

Export an m4a file correctly

Actual behavior

The following error happens.

---------------------------------------------------------------------------
CouldntEncodeError                        Traceback (most recent call last)
Cell In[22], line 4
      1 format = "m4a"
      2 output_path = os.path.join(".", file_name)
----> 4 audio_data.export(output_path, format=format)

File /opt/homebrew/Caskroom/miniforge/base/envs/volume_adjuster/lib/python3.11/site-packages/pydub/audio_segment.py:970, in AudioSegment.export(self, out_f, format, codec, bitrate, parameters, tags, id3v2_version, cover)
    967 log_subprocess_output(p_err)
    969 if p.returncode != 0:
--> 970     raise CouldntEncodeError(
    971         "Encoding failed. ffmpeg/avlib returned error code: {0}\n\nCommand:{1}\n\nOutput from ffmpeg/avlib:\n\n{2}".format(
    972             p.returncode, conversion_command, p_err.decode(errors='ignore') ))
    974 output.seek(0)
    975 out_f.write(output.read())

CouldntEncodeError: Encoding failed. ffmpeg/avlib returned error code: 1

Command:['ffmpeg', '-y', '-f', 'wav', '-i', '/var/folders/ll/szc7m7151ml5z8g80dysyqh00000gn/T/tmpstv6ljpt', '-f', 'm4a', '/var/folders/ll/szc7m7151ml5z8g80dysyqh00000gn/T/tmpxn742nbi']

Output from ffmpeg/avlib:

ffmpeg version 6.0 Copyright (c) 2000-2023 the FFmpeg developers
  built with Apple clang version 14.0.3 (clang-1403.0.22.14.1)
  configuration: --prefix=/opt/homebrew/Cellar/ffmpeg/6.0_1 --enable-shared --enable-pthreads --enable-version3 --cc=clang --host-cflags= --host-ldflags= --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libaribb24 --enable-libbluray --enable-libdav1d --enable-libjxl --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librist --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libsvtav1 --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox --enable-audiotoolbox --enable-neon
  libavutil      58.  2.100 / 58.  2.100
  libavcodec     60.  3.100 / 60.  3.100
  libavformat    60.  3.100 / 60.  3.100
  libavdevice    60.  1.100 / 60.  1.100
  libavfilter     9.  3.100 /  9.  3.100
  libswscale      7.  1.100 /  7.  1.100
  libswresample   4. 10.100 /  4. 10.100
  libpostproc    57.  1.100 / 57.  1.100
Guessed Channel Layout for Input Stream #0.0 : stereo
Input #0, wav, from '/var/folders/ll/szc7m7151ml5z8g80dysyqh00000gn/T/tmpstv6ljpt':
  Duration: 00:00:03.23, bitrate: 1411 kb/s
  Stream #0:0: Audio: pcm_s16le ([1][0][0][0] / 0x0001), 44100 Hz, 2 channels, s16, 1411 kb/s
[NULL @ 0x12ae38720] Requested output format 'm4a' is not a suitable output format
/var/folders/ll/szc7m7151ml5z8g80dysyqh00000gn/T/tmpxn742nbi: Invalid argument

Your System configuration

zxul767 commented 10 months ago

You can use the ipod format to export to m4a (see original answer)

ksk0629 commented 10 months ago

Thank you!!! It worked well!

JiesenSun commented 6 months ago

Thank you!!! It worked well!

May I ask how the problem was ultimately resolved

ksk0629 commented 6 months ago

I put ipod as format.

audio_data.export(output_path, format="ipod")
JiesenSun commented 6 months ago

tks,but The from_file function does not support the m4a format

CouldntDecodeError Traceback (most recent call last) Cell In[40], line 10 7 audio_path = "audio/londonbridge-practice-3.m4a" 8 format = os.path.basename(audio_path).split(".")[1] ---> 10 match_target_amplitude(audio_path, format)

Cell In[36], line 5, in match_target_amplitude(audio_path, audio_format, target_dBFS) 3 def match_target_amplitude(audio_path, audio_format, target_dBFS=-20): 4 # sound = AudioSegment.from_mp3(audio_path) ----> 5 sound = AudioSegment.from_file(audio_path) 6 change_in_dBFS = target_dBFS - sound.dBFS 7 normalized_sound = sound.apply_gain(change_in_dBFS)

File /opt/anaconda3/envs/solfeggio_env/lib/python3.10/site-packages/pydub/audio_segment.py:773, in AudioSegment.from_file(cls, file, format, codec, parameters, start_second, duration, **kwargs) 771 if close_file: 772 file.close() --> 773 raise CouldntDecodeError( 774 "Decoding failed. ffmpeg returned error code: {0}\n\nOutput from ffmpeg/avlib:\n\n{1}".format( 775 p.returncode, p_err.decode(errors='ignore') )) 777 p_out = bytearray(p_out) 778 fix_wav_headers(p_out)

CouldntDecodeError: Decoding failed. ffmpeg returned error code: 183

Output from ffmpeg/avlib:

ffmpeg version 6.1.1 Copyright (c) 2000-2023 the FFmpeg developers built with gcc 12.3.0 (conda-forge gcc 12.3.0-5) configuration: --prefix=/home/conda/feedstock_root/build_artifacts/ffmpeg_1706918361713/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_plac --cc=/home/conda/feedstock_root/build_artifacts/ffmpeg_1706918361713/_build_env/bin/x86_64-conda-linux-gnu-cc --cxx=/home/conda/feedstock_root/build_artifacts/ffmpeg_1706918361713/_build_env/bin/x86_64-conda-linux-gnu-c++ --nm=/home/conda/feedstock_root/build_artifacts/ffmpeg_1706918361713/_build_env/bin/x86_64-conda-linux-gnu-nm --ar=/home/conda/feedstock_root/build_artifacts/ffmpeg_1706918361713/_build_env/bin/x86_64-conda-linux-gnu-ar --disable-doc --disable-openssl --enable-demuxer=dash --enable-hardcoded-tables --enable-libfreetype --enable-libharfbuzz --enable-libfontconfig --enable-libopenh264 --enable-libdav1d --enable-gnutls --enable-libmp3lame --enable-libvpx --enable-libass --enable-pthreads --enable-vaapi --enable-libopenvino --enable-gpl --enable-libx264 --enable-libx265 --enable-libaom --enable-libsvtav1 --enable-libxml2 --enable-pic --enable-shared --disable-static --enable-version3 --enable-zlib --enable-libopus --pkg-config=/home/conda/feedstock_root/build_artifacts/ffmpeg_1706918361713/_build_env/bin/pkg-config libavutil 58. 29.100 / 58. 29.100 libavcodec 60. 31.102 / 60. 31.102 libavformat 60. 16.100 / 60. 16.100 libavdevice 60. 3.100 / 60. 3.100 libavfilter 9. 12.100 / 9. 12.100 libswscale 7. 5.100 / 7. 5.100 libswresample 4. 12.100 / 4. 12.100 libpostproc 57. 3.100 / 57. 3.100 [mov,mp4,m4a,3gp,3g2,mj2 @ 0x557992c22bc0] Format mov,mp4,m4a,3gp,3g2,mj2 detected only with low score of 1, misdetection possible! [mov,mp4,m4a,3gp,3g2,mj2 @ 0x557992c22bc0] moov atom not found [in#0 @ 0x557992c22a80] Error opening input: Invalid data found when processing input Error opening input file audio/londonbridge-practice-3.m4a. Error opening input files: Invalid data found when processing input

milahu commented 4 months ago

Requested output format 'm4a' is not a suitable output format

similar error with ffmpeg 6.1.1

$ ffmpeg -y -f wav -i /run/user/1000/tmpt9qeh4tl -f mka /run/user/1000/tmp_fpjg2b6
[AVFormatContext @ 0x1b1dc80] Requested output format 'mka' is not known.

but it works when i remove -f mka and add the output file extension .mka

$ ffmpeg -y -f wav -i /run/user/1000/tmpt9qeh4tl /run/user/1000/tmp_fpjg2b6.mka
Output #0, matroska, to '/run/user/1000/tmp_fpjg2b6.mka':
  Metadata:
    encoder         : Lavf60.16.100
  Stream #0:0: Audio: vorbis (oV[0][0] / 0x566F), 44100 Hz, mono, fltp
    Metadata:
      encoder         : Lavc60.31.102 libvorbis

ffmpeg -muxers shows no mka but matroska, so this works

ffmpeg -y -f wav -i /run/user/1000/tmpt9qeh4tl -f matroska /run/user/1000/tmp_fpjg2b6
wrong fix: use format as output file extension `pydub/audio_segment.py` ```py #output = NamedTemporaryFile(mode="w+b", delete=False) output = NamedTemporaryFile(mode="w+b", delete=False, suffix=f".{format}") ### conversion_command.extend([ #"-f", format, output.name, # output options (filename last) output.name, # output options (filename last) ]) ```

format != extension, see also What are all of the file extensions supported by FFmpeg

but... pydub is unmaintained #389 #444