Mac OS 下使用 afconvert 命令,处理音频格式转换

在上一篇《Mac OS 下使用 you-get 下载音悦台 MV 、网易云音乐等》给大家介绍了如何下载MV、音乐等资源,但是出现了一些不常见的音频格式,导致在有些播放器上无法解码播放,所以在这篇文章中教大家一个利用 Apple 自带的工具 afconvert 做音频格式转换,当然如果有格式工厂等格式转换软件的话就没必要使用这种方法了,不过学习倒还是可以的,毕竟自己动手,丰衣足食嘛,哈哈哈😆....

coverImage
afconvert 介绍

afconvert 是 Apple 提供的一个音频格式转换工具。

  • 语法
      afconvert [option...] input_file [output_file]

      Options may appear before or after the direct arguments. If output_file
      is not specified, a name is generated programmatically and the file
      is written into the same directory as input_file.

      afconvert input_file [-o output_file [option...]]...

      Output file options apply to the previous output_file.
      Other options may appear anywhere.
  • 一般选项
    { -d | --data } data_format[@sample_rate][/format_flags][#frames_per_packet]
        [-][BE|LE]{F|[U]I}{8|16|24|32|64}          (PCM)
            e.g.   BEI16   F32@44100
        or a data format appropriate to file format (see -hf)
        format_flags: hex digits, e.g. '80'
        Frames per packet can be specified for some encoders, e.g.: samr#12
        A format of "0" specifies the same format as the source file,
            with packets copied exactly.
        A format of "N" specifies the destination format should be the
            native format of the lossless encoded source file (alac, FLAC only)

    { -c | --channels } number_of_channels
        add/remove channels without regard to order.

    { -l | --channellayout } layout_tag
        layout_tag: name of a constant from CoreAudioTypes.h
          (prefix "kAudioChannelLayoutTag_" may be omitted)
        if specified once, applies to output file; if twice, the first
          applies to the input file, the second to the output file

    { -b | --bitrate } total_bit_rate_bps
         e.g. 256000 will give you roughly:
             for stereo source: 128000 bits per channel
             for 5.1 source: 51000 bits per channel
                 (the .1 channel consumes few bits and can be discounted in the
                 total bit rate calculation)

    { -q | --quality } codec_quality
        codec_quality: 0-127

    { -r | --src-quality } src_quality
        src_quality (sample rate converter quality): 0-127 (default is 127)

    { --src-complexity } src_complexity
        src_complexity (sample rate converter complexity): line, norm, bats

    { -s | --strategy } strategy
        bitrate allocation strategy for encoding an audio track
        0 for CBR, 1 for ABR, 2 for VBR_constrained, 3 for VBR

    --prime-method method
        decode priming method (see AudioConverter.h)

    --prime-override samples_prime samples_remain
        can be used to override the priming information stored in the source
        file to the specified values. If -1 is specified for either, the value
        in the file is used.

    --no-filler
        don't page-align audio data in the output file

    --soundcheck-generate
        analyze audio, add SoundCheck data to the output file

    --media-kind "media kind string"
        media kinds are: "Audio Ad", "Video Ad"

    --anchor-loudness
        set a single precision floating point value to
        indicate the anchor loudness of the content in dB

    --generate-hash
        generate an SHA-1 hash of the input audio data and add it to the output file.

    --codec-manuf codec_manuf
        specify the codec with the specified 4-character component manufacturer code

    --dither algorithm
        algorithm: 1-2

    --mix
        enable channel downmixing

    { -u | --userproperty } property value
        set an arbitrary AudioConverter property to a given value
        property is a four-character code; value can be a signed
        32-bit integer or a single precision floating point value.
        e.g. '-u vbrq ' sets the sound quality level
             (: 0-127)
        May not be used in a transcoding situation.

    -ud property value
        identical to -u except only applies to a decoder. Fails if there is no
        decoder.
    -ue property value
        identical to -u except only applies to an encoder. Fails if there is no
        encoder.
  • 输入文件选项
    --read-track track_index
        For input files containing multiple tracks, the index (0..n-1)
        of the track to read and convert.

    --offset number_of_frames
        the starting offset in the input file in frames. (The first frame is
        frame zero.)

    --soundcheck-read
         read SoundCheck data from source file and set it on any destination
         file(s) of appropriate filetype (.m4a, .caf).

    --copy-hash
         copy an SHA-1 hash chunk, if present, from the source file to the output file.

    --gapless-before filename
        file coming before the current input file of a gapless album

    --gapless-after filename
        file coming after the current input file of a gapless album
  • 输出文件选项
    -o filename
        specify an (additional) output file.
    { -f | --file } file_format
        use -hf for a complete list of supported file/data formats

    --condensed-framing field_size_in_bits
        specify storage size in bits for externally framed packet sizes.
        Supported value is 16 for aac in m4a file format.
  • 其他选项
    { -v | --verbose }
        print progress verbosely

    { -t | --tag }
        If encoding to CAF, store the source file's format and name in a user
        chunk. If decoding from CAF, use the destination format and filename
        found in a user chunk.

    { --leaks }
        run leaks at the end of the conversion

    { --profile }
        collect and print performance information
  • 帮助选项
    { -hf | --help-formats }
        print a list of supported file/data formats, as below.

    { -h | --help }
        print help
查看所支持的文件和数据格式列表

Audio file (-f) and the data formats (-d) which each supports:

    '3gpp' = 3GP Audio (.3gp)
               data_formats: 'Qclp' 'aac ' 'aace' 'aacf' 'aacg' 'aach' 
                             'aacl' 'aacp' 'samr' 
    '3gp2' = 3GPP-2 Audio (.3g2)
               data_formats: 'Qclp' 'aac ' 'aace' 'aacf' 'aacg' 'aach' 
                             'aacl' 'aacp' 'samr' 
    'adts' = AAC ADTS (.aac, .adts)
               data_formats: 'aac ' 'aach' 'aacp' 
    'ac-3' = AC3 (.ac3)
               data_formats: 'ac-3' 
    'AIFC' = AIFC (.aifc, .aiff, .aif)
               data_formats: I8 BEI16 BEI24 BEI32 BEF32 BEF64 UI8 'ulaw' 
                             'alaw' 'MAC3' 'MAC6' 'ima4' 'QDMC' 'QDM2' 
                             'Qclp' 'agsm' 
    'AIFF' = AIFF (.aiff, .aif)
               data_formats: I8 BEI16 BEI24 BEI32 
    'amrf' = AMR (.amr)
               data_formats: 'samr' 'sawb' 
    'm4af' = Apple MPEG-4 Audio (.m4a, .m4r)
               data_formats: 'aac ' 'aace' 'aacf' 'aacg' 'aach' 'aacl' 
                             'aacp' 'ac-3' 'alac' 'ec-3' 'paac' 'pac3' 
                             'pec3' 
    'm4bf' = Apple MPEG-4 AudioBooks (.m4b)
               data_formats: 'aac ' 'aace' 'aacf' 'aacg' 'aach' 'aacl' 
                             'aacp' 'paac' 
    'caff' = CAF (.caf)
               data_formats: '.mp1' '.mp2' '.mp3' 'QDM2' 'QDMC' 'Qclp' 
                             'Qclq' 'aac ' 'aace' 'aacf' 'aacg' 'aach' 
                             'aacl' 'aacp' 'ac-3' 'alac' 'alaw' 'dvi8' 
                             'ec-3' 'flac' 'ilbc' 'ima4' I8 BEI16 BEI24 
                             BEI32 BEF32 BEF64 LEI16 LEI24 LEI32 LEF32 
                             LEF64 'ms\x00\x02' 'ms\x00\x11' 'ms\x001' 
                             'opus' 'paac' 'pac3' 'pec3' 'qaac' 'qac3' 
                             'qach' 'qacp' 'qec3' 'samr' 'ulaw' 'zaac' 
                             'zac3' 'zach' 'zacp' 'zec3' 
    'ec-3' = EC3 (.ec3)
               data_formats: 'ec-3' 
    'flac' = FLAC (.flac)
               data_formats: 'flac' 
    'MPG1' = MPEG Layer 1 (.mp1, .mpeg, .mpa)
               data_formats: '.mp1' 
    'MPG2' = MPEG Layer 2 (.mp2, .mpeg, .mpa)
               data_formats: '.mp2' 
    'MPG3' = MPEG Layer 3 (.mp3, .mpeg, .mpa)
               data_formats: '.mp3' 
    'mp4f' = MPEG-4 Audio (.mp4)
               data_formats: 'aac ' 'aace' 'aacf' 'aacg' 'aach' 'aacl' 
                             'aacp' 'ac-3' 'ec-3' 
    'NeXT' = NeXT/Sun (.snd, .au)
               data_formats: I8 BEI16 BEI24 BEI32 BEF32 BEF64 'ulaw' 
    'Sd2f' = Sound Designer II (.sd2)
               data_formats: I8 BEI16 BEI24 BEI32 
    'WAVE' = WAVE (.wav)
               data_formats: UI8 LEI16 LEI24 LEI32 LEF32 LEF64 'ulaw' 
                             'alaw' 
    'RF64' = WAVE (.wav)
               data_formats: UI8 LEI16 LEI24 LEI32 LEF32 LEF64 'ulaw' 
                             'alaw' 

对于以上这么多参数,是不是一脸懵逼,通常情况下,我们只需要转换音乐文件格式,其他的比如比特率、音轨、采样速率等我们不需要关心,那么我们只要用到 -f-d 两个参数即可。

  • -f 指的是 file format,也就是需要转换的目标格式;
  • -d d指的是 data format,是数据格式的意思,理解这些参数的含义需要一定的音乐知识背景

举个栗子:

//将格式为 .m4a 的 input.m4a 文件转换成格式为 .m4b 的文件格式
afconvert -f m4bf -d 'aac ' "input.m4a"

在终端执行完以上命令,如果不出异常,会在与 input.m4a 文件的同级目录有一个 input.m4b的文件。

如果需要指定输出文件的名字可以这样写:

afconvert -f m4bf -d 'aac ' "input.m4a" "output.m4b"

经测试,.m4a格式的音频文件可以转换成.flac.wav.m4b.mp4.caf.snd.au等很多的格式。

But 不能转换成.mp3的格式,悲催😭😭😭

报如下错误信息:Error: ExtAudioFileSetProperty ('cfmt') failed ('fmt?')

通过查阅文档,发现了一条:

MacOS and afconvert do not include an MP3 encoder, though there is a MP4 (lossy) encoder. If you need to create MP3, use Lame or iTunes.
大致意思是:MacOS和afconvert不包括MP3编码器,但有一个MP4(有损)编码器。如果你需要创建MP3,请使用Lame或iTunes。

不过不用太悲观,转不了.mp3的格式,我们还可以转其它格式,毕竟现在的很多音乐播放器支持很多种格式的😁😁😁,如果非得转成.mp3不可的话,可以到在这里cloudconvert在线转换。

网易云音乐的.ncm 格式无法转换,任何格式都转不了,因为 afconvert压根就不支持 .ncm 格式,直接报未知错误:An unknown error occurred.

引用 官方🌰

  • Convert an Audio MP3 File to an iPhone Ringtone (m4af file):
    $ afconvert input.mp3 ringtone.m4r --file m4af

  • Convert an AAC audio file to the iPhone 'Core Audio File Format' (CAFF) at a low 32 kbs bitrate:
    $ afconvert --data aac --bitrate 32000 input.aac output.caf --file 'caff'

  • Convert a WAV file to an MP4 file with lossy aac format at a high bitrate of 256 kbs:
    $ afconvert -d aac -b 256000 input.wav output.mp4 -f mp4f

题外话😂
“If I were going to convert to any religion I would probably choose Catholicism because it at least has female saints and the Virgin Mary” ~ Margaret Atwood

有道翻译🤦‍♀️:“如果我要皈依任何宗教,我可能会选择天主教,因为它至少有女圣人和圣母玛利亚”~玛格丽特·阿特伍德

说到这了,顺便再分享一个查看音频详细信息的命令 afinfo ,举个栗子:

➜  我的MV afinfo input.m4a
File:           input.m4a
File type ID:   m4af
Num Tracks:     1
----
Data format:     2 ch,  44100 Hz, 'aac ' (0x00000000) 0 bits/channel, 0 bytes/packet, 1024 frames/packet, 0 bytes/frame
                no channel layout.
estimated duration: 333.333333 sec
audio bytes: 4088592
audio packets: 14359
bit rate: 98102 bits per second
packet size upper bound: 490
maximum packet size: 490
audio data file offset: 99759
optimized
audio 14700000 valid frames + 2624 priming + 992 remainder = 14703616
format list:
[ 0] format:      2 ch,  44100 Hz, 'aac ' (0x00000000) 0 bits/channel, 0 bytes/packet, 1024 frames/packet, 0 bytes/frame
Channel layout: Stereo (L R)
----

由此可以看出音频详细数据(能看懂的就下面这些数据,其他的也不懂😂😂😂)
音频文件类型:m4af
音频数据格式:'aac '
采样速率:44100 Hz = 44.100 kHz
比特率:98102 bit/s ≈ 98 kbps
时长:333.333333秒 ≈ 5分33秒

afinfo 其它选项可通过 afinfo -h 查看

➜  我的MV afinfo -h       

     Audio File Info
     Version: 2.0
     Copyright 2003-2016, Apple Inc. All Rights Reserved.
     Specify -h (-help) for command options

Usage:
afinfo [option...] audio_file(s)

Options: (may appear before or after arguments)
  {-h --help}
    print help
  {-b --brief}
    print a brief (one line) description of the audio file
  {-r --real}
    get the estimated duration after obtaining the real packet count
  { --leaks }
        run leaks at the end
  { -i --info }
      print contents of the InfoDictionary
  { -u --userprop } 4-cc
      find and print a property or user data property (as string or bytes) [does not print to xml]
  { -x --xml }
      print output in xml format
  { --warnings }
      print warnings if any (by default warnings are not printed in non-xml output mode)
编辑和更改音频文件的封面图片

通过上面格式转换操作后,大家会发现格式转换后,音频文件的封面图没了,如果想要给音频文件添加文件可按如下操作。

  1. 双击要添加封面图的音频文件,此时会被自动添加到 iTunes 的资料库中,开始播放;
  2. 找到音频,点击右键,选择歌曲信息;


    歌曲信息
  3. 选择插图一栏,点击下面的添加图片,把你想要设置的封面图添加进去就可以了,大功告成~_~
    添加图片

参考资料:An A-Z Index of the Apple macOS command line (OS X)




🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺🌺

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270

推荐阅读更多精彩内容