折腾:
【无需解决】Python中用库分割mp3
后,还是打算去试试:
先搞清楚如何从ffmpeg提取出整个mp3
再去搞清楚如何传入时间段,从mp3分割出对应片段
./ffmpeg -i DeadPoetsSocietyCD1.avi -b:a 128k DeadPoetsSociety.mp3
不指定起始和结束时间,就可以提取整个完整的mp3了。
➜ splitAudio git:(master) ffmpeg -i show_14322648_video.mp4 -b:a 128k show_14322648_audio.mp3 ffmpeg version 4.0.2 Copyright (c) 2000-2018 the FFmpeg developers built with Apple LLVM version 10.0.0 (clang-1000.11.45.2) configuration: --prefix=/usr/local/Cellar/ffmpeg/4.0.2 --enable-shared --enable-pthreads --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-gpl --enable-libass --enable-libfdk-aac --enable-libfontconfig --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-opencl --enable-videotoolbox --disable-lzma --enable-nonfree libavutil 56. 14.100 / 56. 14.100 libavcodec 58. 18.100 / 58. 18.100 libavformat 58. 12.100 / 58. 12.100 libavdevice 58. 3.100 / 58. 3.100 libavfilter 7. 16.100 / 7. 16.100 libavresample 4. 0. 0 / 4. 0. 0 libswscale 5. 1.100 / 5. 1.100 libswresample 3. 1.100 / 3. 1.100 libpostproc 55. 1.100 / 55. 1.100 Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'show_14322648_video.mp4': Metadata: major_brand : isom minor_version : 0 compatible_brands: isomiso2avc1 creation_time : 2016-08-30T00:39:54.000000Z Duration: 00:00:48.22, start: 0.000000, bitrate: 616 kb/s Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 640x360 [SAR 1:1 DAR 16:9], 552kb/s, 25 fps, 25 tbr, 12800 tbn, 50 tbc (default) Stream #0:1(eng): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 61 kb/s (default) Metadata: creation_time : 2016-08-30T00:39:54.000000Z Stream mapping: Stream #0:1 -> #0:0 (aac (native) -> mp3 (libmp3lame)) Press [q] to stop, [?] for help Output #0, mp3, to 'show_14322648_audio.mp3': Metadata: major_brand : isom minor_version : 0 compatible_brands: isomiso2avc1 TSSE : Lavf58.12.100 Stream #0:0(eng): Audio: mp3 (libmp3lame), 44100 Hz, stereo, fltp, 128 kb/s (default) Metadata: creation_time : 2016-08-30T00:39:54.000000Z encoder : Lavc58.18.100 libmp3lame size= 755kB time=00:00:48.24 bitrate= 128.2kbits/s speed= 46x video:0kB audio:754kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.072373% ➜ splitAudio git:(master)
即可得到mp3:
然后再去找找,能否用ffmpeg从mp3,指定起始和结束时间,分割出mp3片段
ffmpeg split mp3
Split mp3 file with ffmpeg – dtbaker.net
目前都是分割出单个的mp3的,没法批量
如果批量,目前看到的只能是:每个Segment是固定的时间长度,而不是可以每个的起始和时间可以批量指定
去找找ffmpeg官网,看看是否支持我所希望的如此高级的用法
看到前面从mp3提取出mp3,都是用的是:
-acodec copy
而看到:
解释了:
“3.2 Stream copy
Stream copy is a mode selected by supplying the copy parameter to the -codec option. It makes ffmpeg omit the decoding and encoding step for the specified stream, so it does only demuxing and muxing. It is useful for changing the container format or modifying container-level metadata.”
“An empty stream specifier matches all streams. For example, -codec copy or -codec: copy would copy all the streams without reencoding.”
几个相关参数:
“-t duration (input/output)
When used as an input option (before -i), limit the duration of data read from the input file.
When used as an output option (before an output url), stop writing the output after its duration reaches duration.
duration must be a time duration specification, see (ffmpeg-utils)the Time duration section in the ffmpeg-utils(1) manual.
-to and -t are mutually exclusive and -t has priority.
-to position (input/output)
Stop writing the output or reading the input at position. position must be a time duration specification, see (ffmpeg-utils)the Time duration section in the ffmpeg-utils(1) manual.
-to and -t are mutually exclusive and -t has priority.
-ss position (input/output)
When used as an input option (before -i), seeks in this input file to position. Note that in most formats it is not possible to seek exactly, so ffmpeg will seek to the closest seek point before position. When transcoding and -accurate_seek is enabled (the default), this extra segment between the seek point and position will be decoded and discarded. When doing stream copy or when -noaccurate_seek is used, it will be preserved.
When used as an output option (before an output url), decodes but discards input until the timestamps reach position.
position must be a time duration specification, see (ffmpeg-utils)the Time duration section in the ffmpeg-utils(1) manual.”
那就去试试吧
ffmpeg -i show_14322648_audio.mp3 -acodec copy -ss 00:00:03.110 -to 00:00:06.110 show_14322648_audio_000003110_000006110.mp3 ffmpeg -i show_14322648_audio.mp3 -c copy -ss 00:00:03.110 -to 00:00:06.110 show_14322648_audio_000003110_000006110_cCopy.mp3
输出:
➜ splitAudio git:(master) ffmpeg -i show_14322648_audio.mp3 -c copy -ss 00:00:03.110 -to 00:00:06.110 show_14322648_audio_000003110_000006110_cCopy.mp3 ffmpeg version 4.0.2 Copyright (c) 2000-2018 the FFmpeg developers built with Apple LLVM version 10.0.0 (clang-1000.11.45.2) configuration: --prefix=/usr/local/Cellar/ffmpeg/4.0.2 --enable-shared --enable-pthreads --enable-version3 --enable-hardcoded-tables --enable-avresample --cc=clang --host-cflags= --host-ldflags= --enable-gpl --enable-libass --enable-libfdk-aac --enable-libfontconfig --enable-libfreetype --enable-libmp3lame --enable-libopus --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-opencl --enable-videotoolbox --disable-lzma --enable-nonfree libavutil 56. 14.100 / 56. 14.100 libavcodec 58. 18.100 / 58. 18.100 libavformat 58. 12.100 / 58. 12.100 libavdevice 58. 3.100 / 58. 3.100 libavfilter 7. 16.100 / 7. 16.100 libavresample 4. 0. 0 / 4. 0. 0 libswscale 5. 1.100 / 5. 1.100 libswresample 3. 1.100 / 3. 1.100 libpostproc 55. 1.100 / 55. 1.100 Input #0, mp3, from 'show_14322648_audio.mp3': Metadata: major_brand : isom minor_version : 0 compatible_brands: isomiso2avc1 encoder : Lavf58.12.100 Duration: 00:00:48.27, start: 0.025057, bitrate: 128 kb/s Stream #0:0: Audio: mp3, 44100 Hz, stereo, fltp, 128 kb/s Metadata: encoder : Lavc58.18 Output #0, mp3, to 'show_14322648_audio_000003110_000006110_cCopy.mp3': Metadata: major_brand : isom minor_version : 0 compatible_brands: isomiso2avc1 TSSE : Lavf58.12.100 Stream #0:0: Audio: mp3, 44100 Hz, stereo, fltp, 128 kb/s Metadata: encoder : Lavc58.18 Stream mapping: Stream #0:0 -> #0:0 (copy) Press [q] to stop, [?] for help size= 47kB time=00:00:02.97 bitrate= 129.5kbits/s speed=1.76e+03x video:0kB audio:47kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 1.173211% ➜ splitAudio git:(master)
【总结】
此处:
(1)从mp4中提取整个mp3
ffmpeg -i show_14322648_video.mp4 -b:a 128k show_14322648_audio.mp3
(2)从mp3中提取某个时间段的mp3
ffmpeg -i show_14322648_audio.mp3 -acodec copy -ss 00:00:03.110 -to 00:00:06.110 show_14322648_audio_000003110_000006110.mp3
其中:
-acodec copy
可以简写为:
-c copy
变成:
ffmpeg -i show_14322648_audio.mp3 -c copy -ss 00:00:03.110 -to 00:00:06.110 show_14322648_audio_000003110_000006110_cCopy.mp3