简体   繁体   中英

Bash script to generate thumbnail for all video files in directory

I have a folder with over 10,000 videos. For each of those videos I want to create a random thumbnail image.

My video files are in the directory /videos The thumbnails are in the directory /thumbs

All the videos are named ID.mp4, where ID is the arbitrary name of the video file. I want the thumbs to be ID.jpg.

I currently use this with php to generate thumbnails but its not efficient enough to do it for all those videos.

$video = "/videos";
$image = "/thumbs";
$time = rand(1,300);
shell_exec("ffmpeg -i $video -an -ss $time -f mjpeg -t 1 -r 1 -y -s 620x370 $image 2>&1");

Anyone care to help me on this?

是的,shell可以为您提供帮助,此外,您可以同时运行许多脚本,仅在转换之前检查jpg是否可用。

you can try this:

for f in $MOVIEDIR
do
 ffmpeg -i "$f" -t 2 -r 0.5 "$f"%d.jpg
done

You should get the ffmpeg line doing what u want and with OK efficiency. The wrap it in the script with smaller movie folders...

I did it with this code...

testApp="avconv"; which $testApp >/dev/null 2>&1 || { echo "$testApp not found, try 'apt-get install libav -y'"; exit 1; }
for f in *.mp4 ; do f2=${f%.*}; r1=$((RANDOM%300+30)); [ -f "$f2.jpg" ] || { avconv -ss $r1 -i "$f" -vsync 1 -r 1 -an -y -vframes 1 -timelimit 1 -vf scale='-1':'640',crop=640:480 "$f2.jpg"; }; done

same code

testApp="avconv"; which $testApp >/dev/null 2>&1 || { echo "$testApp not found, try 'apt-get install libav -y'"; exit 1; }
for f in *.mp4 ; do
   f2=${f%.*}; r1=$((RANDOM%300+30))
   [ -f "$f2.jpg" ] || { 
      avconv -ss $r1 -i "$f" -vsync 1 -r 1 -an -y -vframes 1 -timelimit 1 -vf scale='-1':'640',crop=640:480 "$f2.jpg"
   }
done

f=filename

f2=filename without extension

r1=random integer, between 20-300, this is the time in seconds for the snapshot

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM