赞
踩
目录
本文提供将视频中音频提取出来存为mp3格式,一如既往的实用主义。
主要也是学习一下golang使用ffmpeg工具的方式。
ffmpeg环境安装,可以参考我的另一篇文章:windows ffmpeg安装部署_阿良的博客-CSDN博客
本文主要使用到的不是ffmpeg,而是ffprobe也在上面这篇文章中的zip包中。
golang主要依赖库
github.com/u2takey/ffmpeg-go
不废话,上代码。下面为功能实现的主要代码。
- package main
-
- import (
- "fmt"
- "github.com/gofrs/uuid"
- "github.com/google/logger"
- ffmpeg "github.com/u2takey/ffmpeg-go"
- "path/filepath"
- "strings"
- )
-
- func InFormat1(target string, str_array []string) bool {
- for _, element := range str_array {
- if target == element {
- return true
- }
- }
- return false
- }
-
- //提取MP3
- func ExtractMp3(inputVideoPath, outputDir string) string {
- _formatArr := []string{"mp4", "flv"}
- _, _file := filepath.Split(inputVideoPath)
- _tmps := strings.Split(_file, ".")
- _ext := _tmps[len(_tmps)-1]
- if !InFormat1(_ext, _formatArr) {
- logger.Fatal("格式不支持")
- }
- _name, err := uuid.NewV4()
- if err != nil {
- logger.Fatal(err)
- }
- _resultVideoPath := filepath.Join(outputDir, fmt.Sprintf("%s.%s", _name.String(), "mp3"))
- err = ffmpeg.Input(inputVideoPath).
- Output(_resultVideoPath, ffmpeg.KwArgs{"acodec": "libmp3lame"}).
- OverWriteOutput().ErrorToStdOut().Run()
- if err != nil {
- logger.Fatal(err)
- }
- return _resultVideoPath
- }
-
-
- func main() {
- _extractMp3 := ExtractMp3("E:\\360MoveData\\Users\\xxx\\Desktop\\3333333.mp4", "E:\\360MoveData\\Users\\xxx\\Desktop")
- fmt.Println(_extractMp3)
- }
日志工具代码如下,非必要。
- package logutil
-
- import (
- "github.com/google/logger"
- "os"
- )
-
- var pLogger *logger.Logger
-
- // Init init logger
- func Init(verbose, systemLog bool, logPath string) {
- file, err := os.OpenFile(logPath, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0660)
- if err != nil {
- logger.Fatalf("logger Init: open log file(%v) error(%v)", logPath, err)
- }
- pLogger = logger.Init("FFmpegDemoLogger", verbose, systemLog, file)
- }
-
- // Close close logger
- func Close() {
- pLogger.Close()
- }
代码说明
1、ExtractMp3方法参数分别为,输入视频地址、输出mp3目录地址值。
2、做了简单的视频格式校验,如需添加,可以自己看着来。
3、最终视频名使用uuid避免重复。
验证一下
准备的视频信息。
执行结果
ffmpeg version n4.3.1-20-g8a2acdc6da Copyright (c) 2000-2020 the FFmpeg developers
built with gcc 9.3-win32 (GCC) 20200320
configuration: --prefix=/ffbuild/prefix --pkg-config-flags=--static --pkg-config=pkg-config --cross-prefix=x86_64-w64-mingw32- --arch=x86_64 --target-os=mingw32 --enable-gpl --enabl
e-version3 --disable-debug --enable-iconv --enable-zlib --enable-libxml2 --enable-libfreetype --enable-libfribidi --enable-gmp --enable-lzma --enable-fontconfig --enable-libvmaf --dis
able-vulkan --enable-libvorbis --enable-amf --enable-libaom --enable-avisynth --enable-libdav1d --enable-ffnvcodec --enable-cuda-llvm --disable-libglslang --enable-libass --enable-lib
bluray --enable-libmp3lame --enable-libopus --enable-libtheora --enable-libvpx --enable-libwebp --enable-libmfx --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenj
peg --enable-librav1e --enable-schannel --enable-sdl2 --enable-libsoxr --enable-libsrt --enable-libtwolame --enable-libvidstab --enable-libx264 --enable-libx265 --enable-libxvid --ena
ble-libzimg --extra-cflags=-DLIBTWOLAME_STATIC --extra-cxxflags= --extra-ldflags=-pthread --extra-libs=-lgomp
libavutil 56. 51.100 / 56. 51.100
libavcodec 58. 91.100 / 58. 91.100
libavformat 58. 45.100 / 58. 45.100
libavdevice 58. 10.100 / 58. 10.100
libavfilter 7. 85.100 / 7. 85.100
libswscale 5. 7.100 / 5. 7.100
libswresample 3. 7.100 / 3. 7.100
libpostproc 55. 7.100 / 55. 7.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'E:\360MoveData\Users\huyi\Desktop\3333333.mp4':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
creation_time : 2022-09-08T12:04:43.000000Z
Hw : 1
:
bitRate : 16000000
:
com.apple.quicktime.artwork: {"data":{"edittime":22,"infoStickerId":"","musicId":"","os":"windows","product":"lv","stickerId":"","videoEffectId":"","videoId":"245ba6f1-f2ab-4d70-b
c77-c70ea30c171a","videoParams":{"be":0,"ef":0,"ft":0,"ma":0,"me":0,"mu":0,"re":0,"sp":0,"st":0,"te":0,"t
maxrate : 16000000
:
te_is_reencode : 1
:
encoder : Lavf58.76.100
Duration: 00:00:26.91, start: 0.000000, bitrate: 11898 kb/s
Stream #0:0(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709), 1920x1080 [SAR 1:1 DAR 16:9], 11741 kb/s, 30 fps, 30 tbr, 15360 tbn, 60 tbc (default)
Metadata:
creation_time : 2022-09-08T12:04:43.000000Z
handler_name : VideoHandler
Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 156 kb/s (default)
Metadata:
creation_time : 2022-09-08T12:04:43.000000Z
handler_name : SoundHandler
Stream mapping:
Stream #0:1 -> #0:0 (aac (native) -> mp3 (libmp3lame))
Press [q] to stop, [?] for help
Output #0, mp3, to 'E:\360MoveData\Users\huyi\Desktop\7e1acd79-5117-4d4a-85eb-bff1bf01c80b.mp3':
Metadata:
major_brand : isom
minor_version : 512
compatible_brands: isomiso2avc1mp41
te_is_reencode : 1
:
Hw : 1
:
bitRate : 16000000
:
com.apple.quicktime.artwork: {"data":{"edittime":22,"infoStickerId":"","musicId":"","os":"windows","product":"lv","stickerId":"","videoEffectId":"","videoId":"245ba6f1-f2ab-4d70-b
c77-c70ea30c171a","videoParams":{"be":0,"ef":0,"ft":0,"ma":0,"me":0,"mu":0,"re":0,"sp":0,"st":0,"te":0,"t
maxrate : 16000000
:
TSSE : Lavf58.45.100
Stream #0:0(und): Audio: mp3 (libmp3lame), 44100 Hz, stereo, fltp (default)
Metadata:
creation_time : 2022-09-08T12:04:43.000000Z
handler_name : SoundHandler
encoder : Lavc58.91.100 libmp3lame
size= 422kB time=00:00:26.93 bitrate= 128.4kbits/s speed=84.1x
video:0kB audio:421kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.187326%
E:\360MoveData\Users\huyi\Desktop\7e1acd79-5117-4d4a-85eb-bff1bf01c80b.mp3
结果音频信息
没啥好总结的,正好用到了就分享下,大家用的时候看着改。
分享:
有些事情是勉强不来的,勉强自己做的事就是不合适的事,硬逼自己只会把自己逼垮喔。
如果本文对你有帮助的话,点个赞吧,谢谢!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。