[patch] FFmpeg consuming bytes on stdin

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
tamentis
Posts: 1
Joined: 2015-05-08T05:36:41-07:00
Authentication code: 6789

[patch] FFmpeg consuming bytes on stdin

Post by tamentis »

Hi,

I was debugging one of my friends' script which creates thumbnails for images and videos in a folder. The script does something like that:

Code: Select all

find target/folder | while read filename; do
    convert $filename[0] output.png
done
And he noticed that whenever a video is converted, the next image to be converted would always fail and the path would always lose its first character. Here is a small test case that reproduces this bug (assuming you have a video.mov file in the current directory):

Code: Select all

echo "foobar" | sh -c 'convert video.mov[0] out.png; cat'
oobar
It appears that something in convert is consuming a single byte on stdin when converting videos. After a little bit of digging I figured out that ffmpeg was doing it, here another test case using the same command template used in ImageMagick:

Code: Select all

echo "foobar" | sh -c "ffmpeg -v -1 -i video.mov -vframes 1 -vcodec pam -an -f rawvideo -y wat; cat"
oobar
It looks like ffmpeg is interactive by default and reads for user input unless called with the -nostdin parameter. The following suggested patch against svn trunk fixes that issue:

Code: Select all

Index: MagickCore/delegate.c
===================================================================
--- MagickCore/delegate.c       (revision 18525)
+++ MagickCore/delegate.c       (working copy)
@@ -99,8 +99,8 @@
     "  <delegate decode=\"https\" command=\""wget" -q -O "%o" "https:%M"\"/>"
     "  <delegate decode=\"ilbm\" command=\""ilbmtoppm" "%i" > "%o"\"/>"
     "  <delegate decode=\"man\" command=\""groff" -man -Tps "%i" > "%o"\"/>"
-    "  <delegate decode=\"mpeg:decode\" stealth=\"True\" command=\""ffmpeg" -v -1 -vframes %S -i "%i" -vcodec pam -an -f rawvideo -y "%u.pam" 2> "%Z"\"/>"
-    "  <delegate decode=\"null\" encode=\"mpeg:encode\" stealth=\"True\" command=\""ffmpeg" -v -1 -mbd rd -trellis 2 -cmp 2 -subcmp 2 -g 300 -i "%M%%d.jpg" "%u.%m" 2> "%Z"\"/>"
+    "  <delegate decode=\"mpeg:decode\" stealth=\"True\" command=\""ffmpeg" -nostdin -v -1 -vframes %S -i "%i" -vcodec pam -an -f rawvideo -y "%u.pam" 2> "%Z"\"/>"
+    "  <delegate decode=\"null\" encode=\"mpeg:encode\" stealth=\"True\" command=\""ffmpeg" -nostdin -v -1 -mbd rd -trellis 2 -cmp 2 -subcmp 2 -g 300 -i "%M%%d.jpg" "%u.%m" 2> "%Z"\"/>"
     "  <delegate decode=\"pcl:color\" stealth=\"True\" command=\""pcl6" -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=ppmraw" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" "%s"\"/>
"    
     "  <delegate decode=\"pcl:cmyk\" stealth=\"True\" command=\""pcl6" -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=bmpsep8" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" "%s"\"/>"
     "  <delegate decode=\"pcl:mono\" stealth=\"True\" command=\""pcl6" -dQUIET -dSAFER -dBATCH -dNOPAUSE -dNOPROMPT -dMaxBitmap=500000000 -dAlignToPixels=0 -dGridFitTT=2 "-sDEVICE=pbmraw" -dTextAlphaBits=%u -dGraphicsAlphaBits=%u "-r%s" %s "-sOutputFile=%s" "%s"\"/>"
Suggestions? Thoughts?
Post Reply