golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.7k stars 17.62k forks source link

Executing ffmpeg command failed in use go exec lib, while success in linux shell #64733

Closed GostGrimmy closed 10 months ago

GostGrimmy commented 10 months ago

Go version

go version go1.20.2 linux/amd64

What operating system and processor architecture are you using (go env)?

GO111MODULE="on"
GOARCH="amd64"
GOBIN="/home/ubuntu/go/bin"
GOCACHE="/home/ubuntu/.cache/go-build"
GOENV="/home/ubuntu/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/ubuntu/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/ubuntu/go"
GOPRIVATE=""
GOPROXY="https://goproxy.cn"
GOROOT="/home/ubuntu/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/ubuntu/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.20.2"
GCCGO="gccgo"
GOAMD64="v1"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/home/ubuntu/GoProjects/svideo/go.mod"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2912409774=/tmp/go-build -gno-record-gcc-switches"

What did you do?

Bug:

Description

same command , use linux shell success. but failed in use goland exec lib

Test code

    var sss = make([]string, 0)
    sss = append(sss,
        "-re",
        "-i", "path/source/video.mp4",
        "-map", "0",
        "-c:a", "aac",
        "-c:v", "libx264",
        "-b:v:0", "750k",
        "-s:v:0", "640x360",
        "-vf:v:0", "scale=-1:360",
        //"filter:v",       "fps=24",
        "-profile:v:0", "main",
        "-bf", "1",
        "-keyint_min", "120",
        "-g", "120",
        "-sc_threshold", "0",
        "-b_strategy", "0",
        "-ar:a:0", "22050",
        "-use_timeline", "1",
        "-use_template", "1",
        "-window_size", "5",
        "-adaptation_sets", "\"id=0,streams=v id=1,streams=a\"",
        "-f", "dash",
        "/path/out.mpd",
    )
    command := exec.Command("ffmpeg",
        sss...,
    )
        ffmt.Println(command.String())
    output, err := command.CombinedOutput()
    fmt.Println(string(output))
    if err != nil {
        fmt.Println(err.Error())
    }

The exec command

/usr/bin/ffmpeg -re -i /path/source/video.mp4 -map 0 -c:a aac -c:v libx264 -b:v:0 750k -s:v:0 640x360 -vf:v:0 scale=-1:360 -profile:v:0 main -bf 1 -keyint_min 120 -g 120 -sc_threshold 0 -b_strategy 0 -ar:a:0 22050 -use_timeline 1 -use_template 1 -window_size 5 -adaptation_sets "id=0,streams=v id=1,streams=a" -f dash /path/out.mpd

What did you expect to see?

ffmpeg exec success

ffmpeg version 6.0-6ubuntu1 Copyright (c) 2000-2023 the FFmpeg developers
  built with gcc 13 (Ubuntu 13.2.0-2ubuntu1)
  configuration: --prefix=/usr --extra-version=6ubuntu1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl --disable-stripping --enable-gnutls --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libcodec2 --enable-libdav1d --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libglslang --enable-libgme --enable-libgsm --enable-libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librabbitmq --enable-librist --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libsrt --enable-libssh --enable-libsvtav1 --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzimg --enable-libzmq --enable-libzvbi --enable-lv2 --enable-omx --enable-openal --enable-opencl --enable-opengl --enable-sdl2 --disable-sndio --enable-libjxl --enable-pocketsphinx --enable-librsvg --enable-libvpl --disable-libmfx --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libx264 --enable-libplacebo --enable-librav1e --enable-shared
  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
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/home/ubuntu/视频/1641278835_L0bLYHpw8vF8ODVAy_Source.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.20.100
  Duration: 00:01:08.93, start: 0.000000, bitrate: 16222 kb/s
  Stream #0:0[0x1](und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(progressive), 2560x1440, 16081 kb/s, 60 fps, 60 tbr, 15360 tbn (default)
    Metadata:
      handler_name    : VideoHandler
      vendor_id       : [0][0][0][0]
  Stream #0:1[0x2](und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
      vendor_id       : [0][0][0][0]
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
  Stream #0:1 -> #0:1 (aac (native) -> aac (native))
Press [q] to stop, [?] for help
[libx264 @ 0x559a4bd47c80] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
[libx264 @ 0x559a4bd47c80] profile High, level 3.1, 4:2:0, 8-bit
[libx264 @ 0x559a4bd47c80] 264 - core 164 r3095 baee400 - H.264/MPEG-4 AVC codec - Copyleft 2003-2022 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x3:0x113 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=1 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=11 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=1 b_pyramid=0 b_adapt=0 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=120 keyint_min=61 scenecut=0 intra_refresh=0 rc_lookahead=40 rc=abr mbtree=1 bitrate=750 ratetol=1.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
[dash @ 0x559a4bc73400] Opening '/home/ubuntu/.svideo/data/video/init-stream0.m4s' for writing
[dash @ 0x559a4bc73400] Opening '/home/ubuntu/.svideo/data/video/init-stream1.m4s' for writing
Output #0, dash, to '/home/ubuntu/.svideo/data/video/out.mpd':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf60.3.100
  Stream #0:0(und): Video: h264, yuv420p(tv, progressive), 640x360, q=2-31, 750 kb/s, 60 fps, 15360 tbn (default)
    Metadata:
      handler_name    : VideoHandler
      vendor_id       : [0][0][0][0]
      encoder         : Lavc60.3.100 libx264
    Side data:
      cpb: bitrate max/min/avg: 0/0/750000 buffer size: 0 vbv_delay: N/A
  Stream #0:1(und): Audio: aac (LC), 44100 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
      vendor_id       : [0][0][0][0]
      encoder         : Lavc60.3.100 aac
[dash @ 0x559a4bc73400] Opening '/home/ubuntu/.svideo/data/video/chunk-stream1-00001.m4s.tmp' for writing
[dash @ 0x559a4bc73400] Opening '/home/ubuntu/.svideo/data/video/chunk-stream0-00001.m4s.tmp' for writing
[dash @ 0x559a4bc73400] Opening '/home/ubuntu/.svideo/data/video/out.mpd.tmp' for writing
frame=  236 fps= 56 q=-1.0 Lsize=N/A time=00:00:04.17 bitrate=N/A speed=0.985x    
video:389kB audio:65kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown
[libx264 @ 0x559a4bd47c80] frame I:2     Avg QP:25.38  size:  5422
[libx264 @ 0x559a4bd47c80] frame P:118   Avg QP:23.94  size:  2433
[libx264 @ 0x559a4bd47c80] frame B:116   Avg QP:26.45  size:   859
[libx264 @ 0x559a4bd47c80] consecutive B-frames:  1.7% 98.3%
[libx264 @ 0x559a4bd47c80] mb I  I16..4: 23.2% 67.6%  9.2%
[libx264 @ 0x559a4bd47c80] mb P  I16..4:  1.6%  4.2%  1.1%  P16..4: 28.0%  8.6%  3.6%  0.0%  0.0%    skip:52.9%
[libx264 @ 0x559a4bd47c80] mb B  I16..4:  0.3%  0.3%  0.1%  B16..8: 26.8%  4.2%  0.6%  direct: 0.5%  skip:67.1%  L0:32.8% L1:62.0% BI: 5.2%
[libx264 @ 0x559a4bd47c80] final ratefactor: 20.82
[libx264 @ 0x559a4bd47c80] 8x8 transform intra:61.4% inter:72.6%
[libx264 @ 0x559a4bd47c80] coded y,uvDC,uvAC intra: 35.7% 39.5% 13.2% inter: 7.3% 4.9% 0.2%
[libx264 @ 0x559a4bd47c80] i16 v,h,dc,p: 21% 52%  9% 18%
[libx264 @ 0x559a4bd47c80] i8 v,h,dc,ddl,ddr,vr,hd,vl,hu: 20% 19% 37%  3%  4%  5%  4%  4%  4%
[libx264 @ 0x559a4bd47c80] i4 v,h,dc,ddl,ddr,vr,hd,vl,hu: 24% 13% 16%  5% 12% 12%  7%  6%  4%
[libx264 @ 0x559a4bd47c80] i8c dc,h,v,p: 64% 17% 16%  3%
[libx264 @ 0x559a4bd47c80] Weighted P-Frames: Y:0.8% UV:0.0%
[libx264 @ 0x559a4bd47c80] ref P L0: 71.5% 11.9% 11.7%  4.9%  0.1%
[libx264 @ 0x559a4bd47c80] ref B L0: 87.6% 12.4%
[libx264 @ 0x559a4bd47c80] kb/s:808.50
....

What did you see instead?

ffmpeg version 6.0-6ubuntu1 Copyright (c) 2000-2023 the FFmpeg developers
  built with gcc 13 (Ubuntu 13.2.0-2ubuntu1)
  configuration: --prefix=/usr --extra-version=6ubuntu1 --toolchain=hardened --libdir=/usr/lib/x86_64-linux-gnu --incdir=/usr/include/x86_64-linux-gnu --arch=amd64 --enable-gpl --disable-stripping --enable-gnutls --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libcaca --enable-libcdio --enable-libcodec2 --enable-libdav1d --enable-libflite --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libglslang --enable-libgme --enable-libgsm --enable-libjack --enable-libmp3lame --enable-libmysofa --enable-libopenjpeg --enable-libopenmpt --enable-libopus --enable-libpulse --enable-librabbitmq --enable-librist --enable-librubberband --enable-libshine --enable-libsnappy --enable-libsoxr --enable-libspeex --enable-libsrt --enable-libssh --enable-libsvtav1 --enable-libtheora --enable-libtwolame --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx265 --enable-libxml2 --enable-libxvid --enable-libzimg --enable-libzmq --enable-libzvbi --enable-lv2 --enable-omx --enable-openal --enable-opencl --enable-opengl --enable-sdl2 --disable-sndio --enable-libjxl --enable-pocketsphinx --enable-librsvg --enable-libvpl --disable-libmfx --enable-libdc1394 --enable-libdrm --enable-libiec61883 --enable-chromaprint --enable-frei0r --enable-libx264 --enable-libplacebo --enable-librav1e --enable-shared
  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
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/home/ubuntu/视频/1641278835_L0bLYHpw8vF8ODVAy_Source.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.20.100
  Duration: 00:01:08.93, start: 0.000000, bitrate: 16222 kb/s
  Stream #0:0[0x1](und): Video: h264 (High) (avc1 / 0x31637661), yuv420p(progressive), 2560x1440, 16081 kb/s, 60 fps, 60 tbr, 15360 tbn (default)
    Metadata:
      handler_name    : VideoHandler
      vendor_id       : [0][0][0][0]
  Stream #0:1[0x2](und): Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      handler_name    : SoundHandler
      vendor_id       : [0][0][0][0]
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> h264 (libx264))
  Stream #0:1 -> #0:1 (aac (native) -> aac (native))
Press [q] to stop, [?] for help
[libx264 @ 0x55966cb8dfc0] using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX FMA3 BMI2 AVX2
[libx264 @ 0x55966cb8dfc0] profile Main, level 3.1, 4:2:0, 8-bit
[libx264 @ 0x55966cb8dfc0] 264 - core 164 r3095 baee400 - H.264/MPEG-4 AVC codec - Copyleft 2003-2022 - http://www.videolan.org/x264.html - options: cabac=1 ref=3 deblock=1:0:0 analyse=0x1:0x111 me=hex subme=7 psy=1 psy_rd=1.00:0.00 mixed_ref=1 me_range=16 chroma_me=1 trellis=1 8x8dct=0 cqm=0 deadzone=21,11 fast_pskip=1 chroma_qp_offset=-2 threads=11 lookahead_threads=1 sliced_threads=0 nr=0 decimate=1 interlaced=0 bluray_compat=0 constrained_intra=0 bframes=1 b_pyramid=0 b_adapt=0 b_bias=0 direct=1 weightb=1 open_gop=0 weightp=2 keyint=120 keyint_min=61 scenecut=0 intra_refresh=0 rc_lookahead=40 rc=abr mbtree=1 bitrate=750 ratetol=1.0 qcomp=0.60 qpmin=0 qpmax=69 qpstep=4 ip_ratio=1.40 aq=1:1.00
[out#0/dash @ 0x55966ca9b900] Could not write header (incorrect codec parameters ?): Invalid argument
[vost#0:0/libx264 @ 0x55966cabac40] Error initializing output stream: 
[libx264 @ 0x55966cb8dfc0] final ratefactor: 32.52
[aac @ 0x55966caf4ec0] Qavg: 65536.000
[aac @ 0x55966caf4ec0] 2 frames left in the queue on closing
Conversion failed!

exit status 1
--- PASS: TestCmd (0.34s)
PASS

Process finished with the exit code 0
ulikunitz commented 10 months ago

The argument "path/source/video.mp4", is wrong, it needs to be absolute, "/path/source/video.mp4". This is not a Go bug.

GostGrimmy commented 10 months ago

This is the my test file,I use the absolute path

func TestCmd(t *testing.T) {
    var sss = make([]string, 0)
    sss = append(sss,
        "-re",
        "-i", "/home/ubuntu/视频/1641278835_L0bLYHpw8vF8ODVAy_Source.mp4",
        "-map", "0",
        "-c:a", "aac",
        "-c:v", "libx264",
        "-b:v:0", "750k",
        "-s:v:0", "640x360",
        "-vf:v:0", "scale=-1:360",
        //"filter:v",       "fps=24",
        "-profile:v:0", "main",
        "-bf", "1",
        "-keyint_min", "120",
        "-g", "120",
        "-sc_threshold", "0",
        "-b_strategy", "0",
        "-ar:a:0", "22050",
        "-use_timeline", "1",
        "-use_template", "1",
        "-window_size", "5",
        "-adaptation_sets", "\"id=0,streams=v id=1,streams=a\"",
        "-f", "dash",
        "/home/ubuntu/.svideo/data/video/out.mpd",
    )
    fmt.Println("command", sss)
    command := exec.Command("ffmpeg",
        sss...,
    )
    output, err := command.CombinedOutput()
    fmt.Println(string(output))
    if err != nil {
        fmt.Println(err.Error())
    }
}
GostGrimmy commented 10 months ago

Not the path problem, It also appears to the ffmpeg-go lib. That lib also use exec command lib.

artyom commented 10 months ago

@GostGrimmy what I'd check for first — try dropping quoting from your arguments, use

"-adaptation_sets", "id=0,streams=v id=1,streams=a",

instead of the current

"-adaptation_sets", "\"id=0,streams=v id=1,streams=a\"",

You don't need to quote things as in shell, ffmpeg in your case is handed the quoted string, so I suspect it fails on parsing it.

seankhliao commented 10 months ago

Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For questions please refer to https://github.com/golang/go/wiki/Questions

GostGrimmy commented 10 months ago

@GostGrimmy what I'd check for first — try dropping quoting from your arguments, use

"-adaptation_sets", "id=0,streams=v id=1,streams=a",

instead of the current

"-adaptation_sets", "\"id=0,streams=v id=1,streams=a\"",

You don't need to quote things as in shell, ffmpeg in your case is handed the quoted string, so I suspect it fails on parsing it.

Yes, you are right. Thanks for reply

GostGrimmy commented 10 months ago

Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For questions please refer to https://github.com/golang/go/wiki/Questions

Okay, Sorry about not know that.