2013-08-12 29 views
10

Tôi có một thư mục chứa đầy hình ảnh mà tôi muốn biến thành hình gif.imagemagick chuyển đổi độ trễ khung hình riêng lẻ trong gif

Mỗi tên tập tin sau mô hình này: <zero_padded_index>_<delay_in_milliseconds>.gif

Ví dụ:00001_1432.gif

tôi có thể tạo ra các hình ảnh động gif với ImageMagick: convert -loop 0 -delay 10 *.gif out.gif

Vấn đề là, tôi muốn mỗi frame để có một độ trễ khác nhau dựa trên chữ số thứ hai trong tên của nó.

convert -delay 0 -loop 0 *.gif output.gif 

for gif in *.gif; do 

    name=${gif%.gif} 
    index=$(echo ${name%-*} | sed 's/0*//') 
    delay=${name#*-} 

    # 1. convert milliseconds to w/e imagemagick -delay uses. 
    # 2. update the frame at the correct index. 

done; 

Tôi có từng bước xây dựng gif không? Hoặc quay trở lại và thay đổi chúng sau khi thực tế? Sườn tưởng tượng của tôi không được tính ngang.

+1

[Không phân tích đầu ra của 'ls'] (http://mywiki.wooledge.org/ParsingLs), bạn có thể sử dụng globbing thay thế:' cho gif_file trong * .gif; làm [...]; done'. Cũng trích dẫn các biến của bạn. 'cắt' là không cần thiết, bạn có thể sử dụng thay thế tham số cho cùng một nhiệm vụ. –

Trả lời

13

Vì vậy, nếu tôi đã tiếp cận vấn đề này tôi muốn làm như sau (Nếu tôi hiểu đúng)

Với các tập tin sau đây:

[[email protected] ~]# ls -lta so/ 
total 728 
drwxr-xr-x 2 root root 4096 Aug 13 18:35 . 
dr-xr-x---. 17 root root 4096 Aug 13 18:35 .. 
-rw-r--r-- 1 root root 18933 Aug 13 18:23 00007_1432.gif 
-rw-r--r-- 1 root root 18594 Aug 13 18:23 00006_1432.gif 
-rw-r--r-- 1 root root 18984 Aug 13 18:23 00005_1432.gif 
-rw-r--r-- 1 root root 19601 Aug 13 18:23 00004_1444.gif 
-rw-r--r-- 1 root root 19408 Aug 13 18:23 00003_1432.gif 
-rw-r--r-- 1 root root 18632 Aug 13 18:23 00002_1552.gif 
-rw-r--r-- 1 root root 20104 Aug 13 18:23 00001_1432.gif 
[[email protected] ~]# 

kịch bản của tôi sẽ giống như thế này:

#!/bin/bash -x 

# directory of the individual gifs 
_dir=/root/so/ 

# get gifs and make sure your sort them in order 
gifs=$(find $_dir -name *.gif|sort|xargs) 

# this is going to be the imagemagick command 
_CONVERT="convert " 

# make sure the list of gifs look correct 
echo $gifs 

for gif in $gifs; do 

    # full path of each gif 
    full_path=$gif 

    # get just the name of the gif (originally I was going to use this if everything was happing within the same directory) 
    name=$(echo ${gif##*/}) 
    #echo -e "\n$name" 

    # Get the index 
    index=$(echo ${gif##*/} | cut -d\_ -f1) 
    #echo -e "\n$index" 

    # Get the delay of the current image 
    delay=$(echo ${gif##*/} | cut -d\_ -f2| sed "s,.gif,,") 
    # echo -e "\n$delay" 

    # add correct delay options to current gif, append to existing command 
    _CONVERT="${_CONVERT} -delay $delay $gif " 

done; 

# add the outpt of where you're going to put your gif 
_CONVERT="${_CONVERT} -loop 0 -layers Optimize /root/so/stackoverflow.gif" 

# show full command 
echo "Convert cmd: $_CONVERT" 

# run it, then go get your image 
eval $_CONVERT 

Ví dụ về lệnh được tạo:

Convert cmd: convert -delay 1432 /root/so/00001_1432.gif -delay 1552 /root/so/00002_1552.gif -delay 1432 /root/so/00003_1432.gif -delay 1444 /root/so/00004_1444.gif -delay 1432 /root/so/00005_1432.gif -delay 1432 /root/so/00006_1432.gif -delay 1432 /root/so/00007_1432.gif -layers Optimize /root/so/stackoverflow.gif 

Hy vọng đây là những gì bạn đang tìm kiếm.

+2

Điều này thật tuyệt vời, cảm ơn! –

+0

Vui vì tôi có thể giúp !!! –

+1

Cảm ơn kịch bản. Tôi gặp lỗi khi tìm, tôi phải thêm dấu ngoặc kép. Cf. http://stackoverflow.com/a/6495536/113305 – Manu

Các vấn đề liên quan