#11571 closed defect (invalid)

Unexpected shell variable overwrite with `read` and `ffmpeg`

Reported by: Jimmy Leung Owned by:
Priority: normal Component: undetermined
Version: 7.1 Keywords:
Cc: Jimmy Leung, MasterQuestionable Blocked By:
Blocking: Reproduced by developer: no
Analyzed by developer: no

Description

Summary of the bug:

As the ticket title suggests, I find that ffmpeg is overwriting my other shell buffers.

Environment

ffmpeg version 7.1.1
macos 15.3.2

How to reproduce:

Below is the script that can reproduce the bug.

#!/bin/sh

set -e

# Or use another other short-enough video to speed up the testing
vid="https://github.com/tsoding/musializer/assets/165283/8b9f9653-9b3d-4c04-9569-338fa19af071"
folder=./temp
_index=0

mkdir -p $folder

    echo "$vid\n$vid\n$vid\n$vid\n$vid" \
|   while read -r vid_item;
do
    echo "vid@$_index: $vid_item"
    ffmpeg -hide_banner -loglevel error -c copy "$folder/${_index}.mp4" -i $vid_item

    _index=$((_index+1));
done

Apparently the $vid_item buffer is overwritten based on command output.

vid@0: https://github.com/tsoding/musializer/assets/165283/8b9f9653-9b3d-4c04-9569-338fa19af071
vid@1: s://github.com/tsoding/musializer/assets/165283/8b9f9653-9b3d-4c04-9569-338fa19af071
[in#0 @ 0x147e337a0] Error opening input: Protocol not found

And if I "patch" the ffmpeg command with echo & xargs, no such error can be found.

echo $vid_item | xargs ffmpeg -hide_banner -loglevel error -c copy "$folder/${_index}.mp4" -i

Change History (7)

comment:1 by MasterQuestionable, 18 months ago

Cc: MasterQuestionable added
Summary: Bug: Unexpected buffer overwrite incurred by ffmpeg command → Unexpected shell variable overwrite with `read` and `ffmpeg`

͏    Looks like user instrumented shell error.
͏    Probably something to do with `read`:
͏    ​https://www.gnu.org/software/bash/manual/bash.html#index-read

͏    Maybe it could be irrelevant with FFmpeg at all... (shell bug?)

Last edited 18 months ago by MasterQuestionable (previous) (diff)

comment:2 by Jimmy Leung, 18 months ago

Maybe...(?)

My other script has a loop like this, and the read buffer actually get piped to several utilities (sed, grep, curl, etc), and the missing character issue can only be found at the line I call ffmpeg.

I still suspect it is ffmpeg handing, but anyway I get my workaround. If project maintainer think this error is irrelevant to ffmpeg itself, feel free to close this ticket :)

Btw, this will not happen if I use ffprobe -i $var instead of ffmpeg command, which is also interesting :)

Last edited 18 months ago by Jimmy Leung (previous) (diff)

comment:3 by MasterQuestionable, 18 months ago

͏    Would `ffmpeg -i "$var"` reproduce?
͏    Caveat the shell's field splitting on unquoted variables.

comment:4 by Cigaes, 18 months ago

ffmpeg reads commands on its standard input. You can disable that with -nostdin.

in reply to:  3 comment:5 by Jimmy Leung, 18 months ago

Replying to MasterQuestionable:

͏    Would `ffmpeg -i "$var"` reproduce?
͏    Caveat the shell's field splitting on unquoted variables.

Edit: Ignore the below messages please. Probably I will go with -nostdin for my script

To make the loop actually work, I will need to supply output file argument.

So that's what I could do, changing the original ffmpeg command into the below one.

- ffmpeg -hide_banner -loglevel error -c copy "$folder/${_index}.mp4" -i $vid_item
+ ffmpeg "$folder/${_index}.mp4" -i $vid_item

Without stripping away banner and log, I can reproduce a long piece of error message obtained at the 2nd iteration of the loop (indicated by the label vid@1:).

vid@1: ttps://github.com/tsoding/musializer/assets/165283/8b9f9653-9b3d-4c04-9569-338fa19af071
ffmpeg version 7.1.1 Copyright (c) 2000-2025 the FFmpeg developers
  built with Apple clang version 16.0.0 (clang-1600.0.26.6)
  configuration: --prefix=/opt/homebrew/Cellar/ffmpeg/7.1.1_1 --enable-shared --enable-pthreads --enable-version3 --cc=clang --host-cflags= --host-ldflags='-Wl,-ld_classic' --enable-ffplay --enable-gnutls --enable-gpl --enable-libaom --enable-libaribb24 --enable-libbluray --enable-libdav1d --enable-libharfbuzz --enable-libjxl --enable-libmp3lame --enable-libopus --enable-librav1e --enable-librist --enable-librubberband --enable-libsnappy --enable-libsrt --enable-libssh --enable-libsvtav1 --enable-libtesseract --enable-libtheora --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libxvid --enable-lzma --enable-libfontconfig --enable-libfreetype --enable-frei0r --enable-libass --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-libspeex --enable-libsoxr --enable-libzmq --enable-libzimg --disable-libjack --disable-indev=jack --enable-videotoolbox --enable-audiotoolbox --enable-neon
  libavutil      59. 39.100 / 59. 39.100
  libavcodec     61. 19.101 / 61. 19.101
  libavformat    61.  7.100 / 61.  7.100
  libavdevice    61.  3.100 / 61.  3.100
  libavfilter    10.  4.100 / 10.  4.100
  libswscale      8.  3.100 /  8.  3.100
  libswresample   5.  3.100 /  5.  3.100
  libpostproc    58.  3.100 / 58.  3.100
[in#0 @ 0x138632ca0] Error opening input: Protocol not found
[in#0 @ 0x138632ca0] Did you mean file:ttps://github.com/tsoding/musializer/assets/165283/8b9f9653-9b3d-4c04-9569-338fa19af071?
Error opening input file ttps://github.com/tsoding/musializer/assets/165283/8b9f9653-9b3d-4c04-9569-338fa19af071.
Error opening input files: Protocol not found

Somehow the first few characters go missing.

Tested it few more times. Seems it only happens to ffmpeg, but not ffprobe nor grep nor other shell utilities.

Last edited 18 months ago by Jimmy Leung (previous) (diff)

in reply to:  4 comment:6 by Jimmy Leung, 18 months ago

Replying to Cigaes:

ffmpeg reads commands on its standard input. You can disable that with -nostdin.

Ok I just learn this from your reply. Then maybe it is not an issue at all, but my own misuse on ffmpeg :)

comment:7 by Marton Balint, 17 months ago

Resolution: → invalid
Status: new → closed

Closing as invalid then.

Note: See TracTickets for help on using tickets.