折腾:
【已解决】用Python代码从视频中提取出音频mp3文件
期间,在写python代码调用ffmpeg之前,先要去终端中确保用法正确。
先看看用法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | ➜ 61264527 which ffmpeg / usr / local / bin / ffmpeg ➜ 61264527 ffmpeg - - help ffmpeg version 3.4 . 2 Copyright (c) 2000 - 2018 the FFmpeg developers built with Apple LLVM version 9.0 . 0 (clang - 900.0 . 39.2 ) configuration: - - prefix = / usr / local / Cellar / ffmpeg / 3.4 . 2 - - enable - shared - - enable - pthreads - - enable - version3 - - enable - hardcoded - tables - - enable - avresample - - cc = clang - - host - cflags = - - host - ldflags = - - disable - jack - - enable - gpl - - enable - libmp3lame - - enable - libx264 - - enable - libxvid - - enable - opencl - - enable - videotoolbox - - disable - lzma libavutil 55. 78.100 / 55. 78.100 libavcodec 57.107 . 100 / 57.107 . 100 libavformat 57. 83.100 / 57. 83.100 libavdevice 57. 10.100 / 57. 10.100 libavfilter 6.107 . 100 / 6.107 . 100 libavresample 3. 7. 0 / 3. 7. 0 libswscale 4. 8.100 / 4. 8.100 libswresample 2. 9.100 / 2. 9.100 libpostproc 54. 7.100 / 54. 7.100 Hyper fast Audio and Video encoder usage: ffmpeg [options] [[infile options] - i infile]... {[outfile options] outfile}... Getting help : - h - - print basic options - h long - - print more options - h full - - print all options (including all format and codec specific options, very long ) - h type = name - - print all options for the named decoder / encoder / demuxer / muxer / filter See man ffmpeg for detailed description of the options. Print help / information / capabilities: - L show license - h topic show help - ? topic show help - help topic show help - - help topic show help - version show version - buildconf show build configuration - formats show available formats - muxers show available muxers - demuxers show available demuxers - devices show available devices - codecs show available codecs - decoders show available decoders - encoders show available encoders - bsfs show available bit stream filters - protocols show available protocols - filters show available filters - pix_fmts show available pixel formats - layouts show standard channel layouts - sample_fmts show available audio sample formats - colors show available color names - opencl_bench run benchmark on all OpenCL devices and show results - sources device list sources of the input device - sinks device list sinks of the output device - hwaccels show available HW acceleration methods Global options (affect whole program instead of just one file : - loglevel loglevel set logging level - v loglevel set logging level - report generate a report - max_alloc bytes set maximum size of a single allocated block - opencl_options set OpenCL environment options - y overwrite output files - n never overwrite output files - ignore_unknown Ignore unknown stream types - filter_threads number of non - complex filter threads - filter_complex_threads number of threads for - filter_complex - stats print progress report during encoding - max_error_rate ratio of errors ( 0.0 : no errors, 1.0 : 100 % error maximum error rate - bits_per_raw_sample number set the number of bits per raw sample - vol volume change audio volume ( 256 = normal) Per - file main options: - f fmt force format - c codec codec name - codec codec codec name - pre preset preset name - map_metadata outfile[,metadata]:infile[,metadata] set metadata information of outfile from infile - t duration record or transcode "duration" seconds of audio / video - to time_stop record or transcode stop time - fs limit_size set the limit file size in bytes - ss time_off set the start time offset - sseof time_off set the start time offset relative to EOF - seek_timestamp enable / disable seeking by timestamp with - ss - timestamp time set the recording timestamp ( 'now' to set the current time) - metadata string = string add metadata - program title = string:st = number... add program with specified streams - target type specify target file type ( "vcd" , "svcd" , "dvd" , "dv" or "dv50" with optional prefixes "pal-" , "ntsc-" or "film-" ) - apad audio pad - frames number set the number of frames to output - filter filter_graph set stream filtergraph - filter_script filename read stream filtergraph description from a file - reinit_filter reinit filtergraph on input parameter changes - discard discard - disposition disposition Video options: - vframes number set the number of video frames to output - r rate set frame rate (Hz value, fraction or abbreviation) - s size set frame size (WxH or abbreviation) - aspect aspect set aspect ratio ( 4 : 3 , 16 : 9 or 1.3333 , 1.7777 ) - bits_per_raw_sample number set the number of bits per raw sample - vn disable video - vcodec codec force video codec ( 'copy' to copy stream) - timecode hh:mm:ss[:;.]ff set initial TimeCode value. - pass n select the pass number ( 1 to 3 ) - vf filter_graph set video filters - ab bitrate audio bitrate (please use - b:a) - b bitrate video bitrate (please use - b:v) - dn disable data Audio options: - aframes number set the number of audio frames to output - aq quality set audio quality (codec - specific) - ar rate set audio sampling rate ( in Hz) - ac channels set number of audio channels - an disable audio - acodec codec force audio codec ( 'copy' to copy stream) - vol volume change audio volume ( 256 = normal) - af filter_graph set audio filters Subtitle options: - s size set frame size (WxH or abbreviation) - sn disable subtitle - scodec codec force subtitle codec ( 'copy' to copy stream) - stag fourcc / tag force subtitle tag / fourcc - fix_sub_duration fix subtitles duration - canvas_size size set canvas size (WxH or abbreviation) - spre preset set the subtitle options to the indicated preset |
然后继续参考:
的
1 | . /ffmpeg -i DeadPoetsSocietyCD1.avi -ss 00:00:00.000 -to 00:02:00.000 -b:a 128k output.mp3 |
先去看看对应参数含义
1 2 3 4 5 6 | usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}... -ss time_off set the start time offset -to time_stop record or transcode stop time -ab bitrate audio bitrate (please use -b:a) |
去试试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | ➜ 157712932 ll total 10192 - rw - r - - r - - 1 crifan staff 1.7K 9 26 15 : 28 show_157712932_info.json - rw - r - - r - - 1 crifan staff 5.0M 9 26 15 : 27 show_157712932_video.mp4 ➜ 157712932 ffmpeg - i show_157712932_video.mp4 - ss 00 : 00 : 11.270 - to 00 : 00 : 14.550 - b:a 128k show_157712932_audio_000011270_000014550.mp3 ffmpeg version 3.4 . 2 Copyright (c) 2000 - 2018 the FFmpeg developers built with Apple LLVM version 9.0 . 0 (clang - 900.0 . 39.2 ) configuration: - - prefix = / usr / local / Cellar / ffmpeg / 3.4 . 2 - - enable - shared - - enable - pthreads - - enable - version3 - - enable - hardcoded - tables - - enable - avresample - - cc = clang - - host - cflags = - - host - ldflags = - - disable - jack - - enable - gpl - - enable - libmp3lame - - enable - libx264 - - enable - libxvid - - enable - opencl - - enable - videotoolbox - - disable - lzma libavutil 55. 78.100 / 55. 78.100 libavcodec 57.107 . 100 / 57.107 . 100 libavformat 57. 83.100 / 57. 83.100 libavdevice 57. 10.100 / 57. 10.100 libavfilter 6.107 . 100 / 6.107 . 100 libavresample 3. 7. 0 / 3. 7. 0 libswscale 4. 8.100 / 4. 8.100 libswresample 2. 9.100 / 2. 9.100 libpostproc 54. 7.100 / 54. 7.100 Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'show_157712932_video.mp4': Metadata: major_brand : isom minor_version : 0 compatible_brands: isomiso2avc1 creation_time : 2018 - 09 - 25T14 : 26 : 33.000000Z Duration: 00 : 01 : 05.45 , start: 0.000000 , bitrate: 637 kb / s Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 640x360 [SAR 1:1 DAR 16:9], 573 kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default) Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 60 kb/s (default) Metadata: creation_time : 2018 - 09 - 25T14 : 26 : 33.000000Z Stream mapping: Stream #0:1 -> #0:0 (aac (native) -> mp3 (libmp3lame)) Press [q] to stop, [?] for help Output #0, mp3, to 'show_157712932_audio_000011270_000014550.mp3': Metadata: major_brand : isom minor_version : 0 compatible_brands: isomiso2avc1 TSSE : Lavf57. 83.100 Stream #0:0(eng): Audio: mp3 (libmp3lame), 44100 Hz, stereo, fltp, 128 kb/s (default) Metadata: creation_time : 2018 - 09 - 25T14 : 26 : 33.000000Z encoder : Lavc57. 107.100 libmp3lame size = 52kB time = 00 : 00 : 03.29 bitrate = 130.3kbits / s speed = 36.6x video: 0kB audio: 52kB subtitle: 0kB other streams: 0kB global headers: 0kB muxing overhead: 1.053127 % ➜ 157712932 ll total 10304 - rw - r - - r - - 1 crifan staff 52K 10 4 10 : 35 show_157712932_audio_000011270_000014550.mp3 - rw - r - - r - - 1 crifan staff 1.7K 9 26 15 : 28 show_157712932_info.json - rw - r - - r - - 1 crifan staff 5.0M 9 26 15 : 27 show_157712932_video.mp4 ➜ 157712932 |
【总结】
用
1 | ffmpeg -i show_157712932_video.mp4 -ss 00:00:11.270 -to 00:00:14.550 -b:a 128k show_157712932_audio_000011270_000014550.mp3 |
其中各个参数的含义是:
1 2 3 4 5 6 | usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}... -ss time_off set the start time offset -to time_stop record or transcode stop time -ab bitrate audio bitrate (please use -b:a) |
可以从mp4中提取出我们需要的mp3的:

转载请注明:在路上 » 【已解决】Mac中用ffmpeg从mp4视频中提取mp3音频