Advertisement
v1ral_ITS

media super converter [shell script]

Aug 18th, 2018
2,301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 77.14 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # marc brumlik, tailored software inc, V0.01 Mon Sep  8 22:50:20 CDT 2008
  4. version="0.999 Thu Dec 29 09:51:46 CST 2016 \n  Accepts translate-shell for Arch Linux\n  Adjust ffmpeg commands because -sameq arg no longer valid"
  5.  
  6. # tsi-inc@comcast.net
  7. # posted to:  http://www.gnome-look.org/content/show.php/Audio%2BVideo%2BImage%2BText%2BISO+Convert?content=92533
  8. # YOUR VOTE WOULD BE APPRECIATED (as would a $1 donation :-)
  9.  
  10. # convert various image, audio, and video files into other likely formats
  11.  
  12. # set -x
  13.  
  14. #####
  15. # Watcher
  16. #####
  17. avwatcher() {
  18. ### this function produces the "progress window" during the conversion.
  19. ### there are two phases...
  20.  
  21. case $veryquiet in
  22.     y)  exit 0 ;;
  23. esac
  24.  
  25. # set -x
  26. watchit="$1"
  27. mult="100"
  28.  
  29. # doing frames??
  30. frames=n
  31. echo "$watchit" | grep "frame-%04d" >/dev/null && frames=y
  32. [ "$frames" = "y" ] && mult="1000"
  33.  
  34. # doing pdf/gif --> multiple images?
  35. [ -z "$multipage" ] || watchit=`echo "$watchit" | sed "s^.$each^-0.$each^"`
  36.  
  37. # phase 1 begins.  sleep for one second while the conversion gets started.
  38. # sleep 1
  39. # using the process NAME and the PPID given, find the PID of the conversion
  40. pid=`ps -ef | grep " $4 " | grep "$2" | awk '{print $2}'`
  41. z="0"
  42. # every second, test for the appearance of the output file
  43.  
  44. wtext1=`mytranslate "Waiting for"`
  45. wtext1="$wtext1 $2\n"
  46. wtext1="$wtext1 "`mytranslate "to begin writing"`
  47. wtext1="$wtext1 $watchit"
  48. wtext2=`mytranslate "Program Cancelled!"`
  49.  
  50. while true
  51. do
  52. # if this is a "frame job", skip watcher phase 1
  53.     [ "$frames" = "y" ] && break
  54. # if the file appears before progress window even comes up, go to phase 2
  55.     if test -s "$watchit"
  56.         then    break
  57.     fi
  58.         for a in 10 20 30 40 50 60 70 80 90 80 70 60 50 40 30 20 10 00
  59.         do
  60. # when file appears, go to phase 2
  61.                 test -s "$watchit" && break 2
  62. # otherwise, increment counter in zenity window for next display
  63.                 echo $a; sleep 1
  64.         z=`expr $z + 1`
  65. # but if no file is seen for 20 seconds, something's not right.  exit.
  66.         if [ "$z" = "20" ]
  67.             then    exit 0
  68.         fi
  69.         done
  70. done | zenity --progress --auto-close --text="$wtext1" || ( kill -9 $pid >/dev/null 2>&1; zenity --info --text="$wtext2" )
  71.  
  72. # phase 2
  73.  
  74. loopsleep=2
  75. # set up for multi-image output if $multipage is non-blank
  76. [ -z "$multipage" ] && multipage="x"
  77. for loop in $multipage
  78. do
  79.  
  80. if [ "$loop" = "x" ]
  81.     then    : single loop
  82.     else    : loop for each output image
  83.         watchit=`echo "$1" | sed "s^.$each^-$loop.$each^"`
  84.         loopsleep=.5
  85. fi
  86.  
  87. wtext1=`mytranslate "Writing to"`
  88. wtext1="$wtext1 $watchit\n"
  89. wtext1="$wtext1 "`mytranslate "Estimate to completion...\n\nIf the progress jumps between 95 and 99, this means\nthe new file is larger than the original.  Be patient."`
  90.  
  91. jump=1
  92.  
  93. while true
  94. do
  95. # is it still there?  some conversions manipulate the name
  96.     if [ "$frames" = "y" ]
  97.         then    cksize=`echo "$watchit" | sed 's/frame-.*/frame-/'`
  98.             ckext=`echo "$watchit" | sed 's/^.*\././'`
  99.         else    if test -s "$watchit"
  100.                 then    cksize="$watchit"
  101.                 else    break
  102.             fi
  103.     fi
  104.  
  105. # find the size of the target output file
  106.     if [ "$frames" = "y" ]
  107.         then
  108.         newsize=`du -a -b -c "$cksize"????$ckext | tail -1 | awk '{print $1}'`
  109.         else
  110.     newsize=`du -a -b "$watchit" | awk '{print $1}'`
  111.     fi
  112. # calculate this as a percentage of target file size
  113.         progress="0"`echo "scale=10; $newsize / $3 * $mult" | bc`
  114.     if [ "$progress" = "$oldprogress" ]
  115. # if we are at 100% zenity will exit on its own
  116. # but if the output file stops growing BEFORE 100%, exit out
  117.         then    break
  118.     fi
  119.     oldprogress="$progress"
  120.     showprogress=`echo "$progress" | sed 's/\..*//'`
  121.     sleep $loopsleep
  122. # if the file is still growing but we're over 95%, get fancy
  123.     if [ "$showprogress" -ge "95" ]
  124.         then    if [ "$jump" = "1" ]
  125.                 then    showprogress=95; jump=2
  126.                 else    showprogress=99; jump=1
  127.             fi
  128.     fi
  129.     echo $showprogress
  130. done | zenity --progress --percentage=01 --auto-close --text="$wtext1" || ( kill -9 $pid >/dev/null 2>&1; zenity --info --text="$wtext2" )
  131.  
  132. # end of multi-image loops
  133. done
  134. }
  135. # end of watcher
  136.  
  137.  
  138. #####
  139. # Translate text to other languages
  140. #####
  141. mytranslate() {
  142. #
  143. # use online translation service on text
  144.  
  145. case $online in
  146.     n)  echo "$*"; exit 0 ;;
  147. esac
  148.  
  149. # A NEWER WAY  :-)
  150. # use gnome's provided "translate"
  151. # (hope this is available to non-gnome users!!)
  152. echo "$*" | sed 's/$/++/' | $trbin -f en -t $lang | sed 's/++/
  153. /g'
  154.  
  155. # A NEW AND BETTER WAY
  156. ### No longer works -- Google Translate API is now a paid service
  157. ### wget -qO- "http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=$1&langpair=en|$lang" | sed 's/.*"translatedText":"\([^"]*\)".*}/\1\n/' | sed "s/\\u0026#39;/'/g"
  158.  
  159. # Keeping old code for posterity...
  160. # and, because, it appears the above will not be available (for free) forever
  161.  
  162. #case $service in
  163. #google)    serviceline="-d langpair=en|$lang http://translate.google.com/translate_t" ;;
  164. #babel) serviceline="-d lp=en_$lang http://babelfish.yahoo.com/translate_txt" ;;
  165. #esac
  166. #
  167. #transline=`curl --connect-timeout 1 -m 1 --retry 1 -s -A "Mozilla/5.0" \
  168. #   -d "hl=en" -d "ie=UTF8" -d text="$*" $serviceline`
  169. #
  170. #case $service in
  171. #   google) echo "$transline" | grep "gtrans" | \
  172. #       gawk -F"gtrans value" '{print $2}' | \
  173. #       cut -d'"' -f2 | $links -dump | $links -dump | sed 's/^ *//'
  174. #       ;;
  175. #   babel)  echo "$transline" | grep "result" | grep "div" | \
  176. #       tr '<>' '~~' | awk -F~ '{print $5}'
  177. #       ;;
  178. #esac
  179. }
  180.  
  181. #####
  182. # Convert
  183. #####
  184. myavconvert() {
  185. #
  186. # processing images or performing text-to-image conversion
  187.  
  188. # set -x
  189.  
  190. # this is for looping after the first item, using the env for settings
  191. looping=n
  192. if test -s /tmp/avconvert.env
  193.     then    . /tmp/avconvert.env
  194.         looping=y
  195. fi
  196.  
  197. # the output types allowed for
  198.  
  199. case $it in
  200.     image)  out=" gif jpg ico pdf png tif OTHER"
  201.         height=460 ;;
  202.     text)   out=" gif jpg ico pdf png tif OTHER"
  203.         height=370 ;;
  204. esac
  205.  
  206. # set default checkbox: all false except the original ext (except txt)
  207. case $imageext in
  208.     none)   out=`echo "$out" | sed -e 's/ / FALSE /g'` ;;
  209.     same)   out=`echo "$out" | sed -e 's/ / FALSE /g' -e "s/FALSE \$ext/TRUE $ext/"` ;;
  210.     *)  out=`echo "$out" | sed -e 's/ / FALSE /g' -e "s/FALSE \$imageext/TRUE $imageext/"` ;;
  211. esac
  212.  
  213. # show output choices to user and loop until selection is made
  214. while true
  215. do
  216. if [ "$looping" = "n" ]
  217.     then
  218. case $prog$it in
  219.         convertimage)   ident=`identify "$target"`
  220.             ident="$ident\n$imident"
  221.             title=`mytranslate "Converting file"`
  222.             c1=`mytranslate "Convert to format"`
  223.             text=`mytranslate "Source file format"`
  224.             text="$text $show,
  225. $ident
  226.  
  227. "
  228.             text="$text"`mytranslate "You can select multiple formats - avconvert will loop to create them all.
  229.  
  230. OUTPUT NAMING: names of the files created will include the proper extension,
  231. and if the resolution differs from the original then a \"-XXX\" before the extension.
  232.  
  233. Choose OTHER for an output type not listed"`
  234.             choice=`zenity --list --height=$height --title="$title $target" --text="$text" --checklist --column "$s1" --column "$c1" $out` || exit 0
  235.             ;;
  236.         converttext)    title=`mytranslate "Convert"`
  237.             text=`mytranslate "Source file format"`
  238.             text="$text $show
  239.  
  240. "
  241.             text="$text"`mytranslate "Default action is to convert TEXT to IMAGE
  242.  
  243. Choose OTHER for an output type not listed"`
  244.             c1=`mytranslate "Convert to format"`
  245.             choice=`zenity --list --height=$height --title="$title $target" --text="$text" --checklist --column "$s1" --column "$c1" $out` || exit 0
  246.             ;;
  247. esac
  248. fi
  249.  
  250. if [ -n "$choice" ]
  251.     then    break
  252. fi
  253. done
  254.  
  255. # was it OTHER?
  256. case $choice in
  257.         OTHER_non-image)  exec myavtext ;;
  258.         OTHER)  title=`mytranslate "Output type"`
  259.         text=`mytranslate "Supply an appropriate file extension"`
  260.         choice=`zenity --entry --title="$title" --text="$text"` || exit 0 ;;
  261. esac
  262.  
  263. # clean up any _audio reference and any leading . in ext
  264. choice=`echo $choice | sed -e 's/_.*//' -e 's/^\..*//'`
  265.  
  266. #       loop over (possibly multiple) choice(s)
  267. # (THE OUTER LOOP)
  268. for each in `echo $choice | sed 's/|/ /g'`
  269. do
  270.  
  271. #       what will the destination filename be?
  272. new=`echo $target | sed -e "s/.$ext$//" -e "s/$/.$each/"`
  273.  
  274. if [ "$looping" = "n" ]
  275.     then
  276. # set resolution here
  277. # results into $dim, will look like "jpg|gif|pdf"
  278. origrez=`echo "$ident" | sed "s/$target//"`
  279. case $it in
  280.     text)   title=`mytranslate "Dimensions"`
  281.         text=`mytranslate "Output dimensions for"`
  282.         text="$text \"$new\"\n\n"
  283.         text="$text"`mytranslate "This will be the dimension along the longer edge
  284. (depending on portrait or landscape mode)
  285.  
  286. Choose one or more.
  287. (\"Original\" cannot be combined with other sizes)"`
  288.         c1=`mytranslate "Choose dimensions"`
  289.         dim=`zenity --list --height=520 --title="$title" --text="$text" --checklist --column "$s1" --column "$c1" FALSE 1280 FALSE 1024 FALSE 800 FALSE 640 FALSE 480 FALSE 320 FALSE 250 FALSE 200 FALSE 150 FALSE 100 FALSE "Something else"` || exit 0
  290.         ;;
  291.     image)  title=`mytranslate "Dimensions"`
  292.         text=`mytranslate "Output dimensions for"`
  293.         text="$text \"$new\"\n\n"
  294.         text="$text"`mytranslate "This will be the dimension along the longer edge
  295. (depending on portrait or landscape mode)
  296.  
  297. Choose one or more.
  298. (\"Original\" cannot be combined with other sizes)"`
  299.         c1=`mytranslate "Choose dimensions"`
  300.         dim=`zenity --list --height=580 --title="$title" --text="$text" --checklist --column "$s1" --column "$c1" FALSE "Original dimensions" FALSE 1280 FALSE 1024 FALSE 800 FALSE 640 FALSE 480 FALSE 320 FALSE 250 FALSE 200 FALSE 150 FALSE 100 FALSE "Something else"` || exit 0
  301.         ;;
  302. esac
  303. fi
  304.  
  305. case "$dim" in
  306.     Original*)  dim="" ;;
  307.     Something*) while true
  308.             do
  309.                 text=`mytranslate "New size (along longer edge)"`
  310.                 dim=`zenity --entry --text="$text"` || exit 0
  311.                 case $dim in
  312.                     [0-9]*[0-9])    break ;;
  313.                     *)      ;;
  314.                 esac
  315.             done ;;
  316. esac
  317.  
  318. # if $safedims is set and $dim is empty
  319. # then set it to what we used last time
  320. if [ -z "$dim" -a -n "$safedims" ]
  321.     then    dims="$safedims"
  322. fi
  323. # parse $dim into $dims
  324. case $dim in
  325.     '') dims=" " ;;
  326.     *)  dims=`echo $dim | sed 's/|/ /g'` ;;
  327. esac
  328.  
  329. # and save these $dims for next time
  330. safedims="$dims"
  331. textdims=`mytranslate "Previously selected resolutions:"`
  332. textdims="$textdims: $safedims"
  333.  
  334. if [ "$looping" = "n" ]
  335.     then
  336. # if this is a jpeg, ask for quality
  337. case "$new" in
  338.     *jpg)   title=`mytranslate "Quality"`
  339.         title="JPG $title"
  340.         text=`mytranslate "Choose quality for"`
  341.         text="$text \"$new\""
  342.         qual=`zenity --list --height=290 --title="$title" --text="$text" --radiolist --column "$s1" --column "$title" FALSE 100 FALSE 90 FALSE 80 TRUE 70 FALSE 60 FALSE 50` || exit 0 ;;
  343. esac
  344.  
  345. # normalize and equalize
  346. case $it in
  347.     image*) ntxt=`mytranslate "Adjust for full range of colors"`
  348.         etxt=`mytranslate "Adjust for full range of brightness"`
  349.         normeq=`zenity --list --height=190 --title="Normalize / Equalize / Rotate" --text="Normalize: $ntxt\nEqualize: $etxt" --checklist --column "$s1" --column "$s2" FALSE normalize FALSE equalize FALSE rotate-90 FALSE rotate-180 FALSE rotate-270 FALSE transpose FALSE transverse` || exit 0
  350.         case $normeq in
  351.             '') ;;
  352.             *)  normeq=`echo "$normeq" | \
  353.                     tr '|' '\012' | sed 's/^/-/'`
  354.                 case $normeq in
  355.                     -normalize*)    normeq=`echo "$normeq" | sed 's/-normalize/-separate -normalize -combine/'`
  356.                             ;;
  357.                 esac
  358.                 normeq=`echo "$normeq" | sed -e 's/-90/ 90/' -e 's/-180/ 180/' -e 's/-270/ 270/'`
  359.                 normeq=`echo $normeq`
  360.                 ;;
  361.         esac
  362.         ;;
  363. esac
  364. fi
  365.  
  366. # create arg to convert for quality
  367. case "$new" in
  368.     *jpg)   case $qual in
  369.             '') quality="-quality 70" ;;
  370.             *)  quality="-quality $qual" ;;
  371.         esac ;;
  372. esac
  373.  
  374. # echo "$dir"
  375. # echo "$outdir"
  376.  
  377. case "$dims" in
  378.     *x*x*)  ;;
  379.     *)  case "$dir$overwrite" in
  380.             "$outdir")  title=`mytranslate "Overwrite?"`
  381.                     text=`mytranslate "Read carefully:
  382.  
  383. MANY places in this program are designed to prevent accidentally overwriting
  384. your original file(s).  However, since you have chosen the source directory as
  385. your destination AND chosen only one conversion, you have another option.
  386. You can choose (with caution) to REPLACE ORIGINALS with the converted files.
  387. In this case, the converted files will have the original name as well (instead
  388. of inserting the resolution) but with the correct extension.
  389.  
  390. Choose wisely..."`
  391.                     c1=`mytranslate "Overwrite the original files in place?"`
  392.                     overwrite=`zenity --list --title="$title" --height=340 --text="$text" --radiolist --column "$s1" --column "$c1" TRUE NO FALSE YES` || exit 0
  393.                     ;;
  394.         esac
  395.         ;;
  396. esac
  397.  
  398. # loop through all the $dims.
  399. for rez in " "$dims
  400. do
  401. # THE INNER LOOP
  402.  
  403. case "$rez" in
  404.     " ")    geom="" ;;
  405.     *)  rez=`echo $rez`; geom="-geometry $rez"x"$rez"
  406.         new=`echo $target | sed -e "s/.$ext$//" -e "s/$/.$rez.$each/"` ;;
  407. esac
  408. if test -s "$outdir/$new"
  409.     then    title=`mytranslate "Choose a new Destination Name"`
  410.         text=`mytranslate "exists - Overwrite?"`
  411.         zenity --question --title="$text" --text="$new $text" || newout=`zenity --file-selection --filename="$outdir/$new" --save --confirm-overwrite --title="$title"` || exit 0
  412.         if [ ! -z "$newout" ]
  413.             then    outdir=`dirname "$newout"`
  414.                 new=`basename "$newout"`
  415.         fi
  416. fi
  417.  
  418. case $it in
  419. text)   # EXTRA stuff for TEXT to IMAGE
  420.     colorlist=`convert -list color | sed -n '/,/s/  .*//p' | sort -u`
  421.     colorlist=`echo " "$colorlist | sed 's/ / FALSE /g'`
  422.     if which kcolorchooser >/dev/null 2>&1
  423.         then    colorlist="FALSE GUI-\"kcolorchooser\" $colorlist"
  424.         else    colortext=`mytranslate "
  425.  
  426. Install the package \"kdegraphics\" to choose colors with a GUI"`
  427.     fi
  428.     colorlist="FALSE Key-in_RGB-value $colorlist"
  429.     # for OLD version of convert
  430.     # fontlist=`convert -list font | sed -n '/0$/s/ .*//p' | sort -u`
  431.     # for NEW version of convert
  432.     fontlist=`convert -list font | grep Font | sed 's/^.*://' | \
  433.         sed -e 's/^ //g' -e 's/ $//' | sort -u`
  434.     fontlist=`echo " "$fontlist | sed 's/ / FALSE /g'`
  435.     sizelist=" 8 10 12 15 20 25 30 35 40 50 60 80 100 120 150 200 250 300"
  436.     sizelist=`echo "$sizelist" | sed 's/ / FALSE /g'`
  437.  
  438.     bg=`mytranslate "Background"`
  439.     title="$bg"
  440.     text=`mytranslate "Choose Background Color
  441.  
  442. NOTE: If you use GUI chooser, the RGB values
  443. will be shown in subsequent windows
  444. for your reference."`
  445.     text="$text $colortext\n\n"
  446.     text=`mytranslate "ALSO NOTE: for some unknown reason
  447. there is a long delay when GUI closes."`
  448.     c1=`mytranslate "Choose color"`
  449.     back=`zenity --list --height=500 --title="$title" --text="$text" --radiolist --column "$s1" --column "$c1" $colorlist` || exit 0
  450.     case $back in
  451.         GUI-*)  back=`kcolorchooser --print` ;;
  452.         _GUI*)  text=`mytranslate "To choose colors using a GUI
  453. interface, install the"`
  454.             text="$text \"kdegraphics\""
  455.             text="$text "`mytranslate "package"`
  456.             zenity --info --title="kcolorchooser" --text="$text"
  457.             exit 0
  458.             ;;
  459.         Key-*)  title=`mytranslate "Key in color"`
  460.             text=`mytranslate "Enter the RGB color as"`
  461.             back=`zenity --entry --title="$title" --text="$text  #RGB  #RRGGBB  #RRRGGGBBB  #RRRRGGGGBBBB  rgb(rrr,ggg,bbb)"` || exit 0
  462.             ;;
  463.     esac
  464.     fg=`mytranslate "Foreground"`
  465.     title="$fg"
  466.     text=`mytranslate "Choose Foreground Color"`
  467.     fill=`zenity --list --height=500 --title="$title" --text="[$bg = $back]\n\n$text" --radiolist --column "$s1" --column "$c1" $colorlist` || exit 0
  468.     case $fill in
  469.         GUI-*)  fill=`kcolorchooser --print` ;;
  470.         _GUI*)  kdeg=`mytranslate "To choose colors with a GUI
  471. interface, install the \"kdegraphics\" package"`
  472.             zenity --info --title="kcolorchooser" --text="$kdeg"; exit 0 ;;
  473.         Key-*)  title=`mytranslate "Key in color"`
  474.                         text=`mytranslate "Enter the RGB color as"`
  475.             fill=`zenity --entry --title="$title" --text="$text:  #RGB  #RRGGBB  #RRRGGGBBB  #RRRRGGGGBBBB  rgb(rrr,ggg,bbb)"` || exit 0 ;;
  476.     esac
  477.     title=`mytranslate "Border Width"`
  478.     text=`mytranslate "Choose Border Width"`
  479.     borw=`zenity --list --height=340 --title="$title" --text="[$bg = $back]\n[$fg = $fill]\n\n$text" --radiolist --column "$s1" --column "$text" FALSE 0 FALSE 5 FALSE 10 FALSE 20 FALSE 30 FALSE 50` || exit 0
  480.     case $borw in
  481.         0)  borc=`mytranslate "transparent"` ;;
  482.         *)  title=`mytranslate "Border Color"`
  483.             text=`mytranslate "Border Width"`
  484.             borc=`zenity --list --height=500 --title="$title" --text="[$bg = $back]\n[$fg = $fill]\n[$text = $borw]\n\n$title" --radiolist --column "$s1" --column "$title" $colorlist` || exit 0 ;;
  485.     esac
  486.     case $borc in
  487.         GUI-*)  borc=`kcolorchooser --print` ;;
  488.         _GUI*)  zenity --info --title="kcolorchooser" --text="$kdeg"; exit 0 ;;
  489.         Key-*)  title=`mytranslate "Key in color"`
  490.                         text=`mytranslate "Enter the RGB color as"`
  491.             borc=`zenity --entry --title="$title" --text="$text:  #RGB  #RRGGBB  #RRRGGGBBB  #RRRRGGGGBBBB  rgb(rrr,ggg,bbb)"` || exit 0 ;;
  492.     esac
  493.     title=`mytranslate "Font"`
  494.     text=`mytranslate "Choose text font and style"`
  495.     font=`zenity --list --height=500 --title="$title" --text="[$bg = $back]\n[$fg = $fill]\n[Border width is $borw]\n[Border color is $borc]\n\n$text" --radiolist --column "$s1" --column "$title" $fontlist` || exit 0
  496.     title=`mytranslate "Font Size"`
  497.     text=`mytranslate "Choose text font size"`
  498.     size=`zenity --list --height=500 --title="$title" --text="[$bg = $back]\n[$fg = $fill]\n[Border width is $borw]\n[Border color is $borc]\n[Font style is $font]\n\n$text" --radiolist --column "$s1" --column "$title" $sizelist` || exit 0
  499.     textoptions="-background $back -fill $fill -border $borw -bordercolor $borc -font $font -pointsize $size label:@"
  500.     longestline=`cat "$target" | awk 'BEGIN {max[i]=0}
  501.                        (length >= max[i]) {max[i] = length}
  502.                        END {printf "%s\n", max[i]}'`
  503.     # horizrez=`echo -e "scale=0\n$rez * 8 / 11\nquit\n" | bc`
  504.     horizrez=$rez
  505.     maxpossible=`echo -e "scale=0\n$horizrez / $size\nquit\n" | bc`
  506.     # splitpieces=`echo -e "scale=0\n$longestline / $maxpossible\nquit\n" | bc`
  507.     newlength=`echo -e "scale=2\n$maxpossible * .9\nquit\n" | bc`
  508.     newlength=`echo "$newlength" | sed 's/\..*//'`
  509.     if [ "$longestline" -ge "$maxpossible" ]
  510.         then    newtarget=`echo "$target" | sed "s/$ext$/wrap$newlength/"`
  511.             title=`mytranslate "Adjusting input file"`
  512.             text=`mytranslate "The source file \"1\" contains long lines.
  513.  
  514. The longest line is 2 but the max possible with an
  515. image width of 3 and a font size of 4 is 5.
  516.  
  517. Creating an intermediate text file with a word wrap at
  518. the first space after character position 6.
  519.  
  520. This will get you APPROXIMATELY the
  521. character size you asked for.
  522. The intermediate file will contain the full text of 7.
  523.  
  524. Intermediate file name is: 8"`
  525.             text=`echo "$text" | \
  526.             sed -e "s/1/$target/" -e "s/2/$longestline/" \
  527.             -e "s/3/$horizrez/" -e "s/4/$size/" \
  528.             -e "s/5/$maxpossible/" -e "s/6/$newlength/" \
  529.             -e "s/7/$target/" -e "s/8/$newtarget/"`
  530.            
  531.             zenity --info --title="$title" --text="$text"
  532.         sed "s/.\{$newlength\} /&\n/g" < "$target" > "$newtarget"
  533.             target="$newtarget"
  534.     fi
  535.  
  536.     linecount=`wc $target | awk '{print $1}'`
  537.     maxpossible=`echo -e "scale=2\n$rez / $size\nquit\n" | bc`
  538.     maxpossible=`echo -e "scale=2\n$maxpossible * 2 / 3\nquit\n" | bc`
  539.     maxpossible=`echo "$maxpossible" | sed 's/\..*//'`
  540.  
  541.     if [ "$linecount" -gt "$maxpossible" ]
  542.         then    shorttarget=`echo $target | sed 's/$ext$//'`
  543.             title=`mytranslate "Adjusting input file"`
  544.             text=`mytranslate "The source file \"1\" contains too many lines.
  545. Adjusting for a more normal aspect ratio.
  546.  
  547. There are 2 lines, but the max possible
  548. with a length of 3 and a size of 4 is 5.
  549.  
  550. Splitting 6 into a set of files with 7
  551. lines in each.  The resulting files will be processed.
  552.  
  553. This will get you APPROXIMATELY the
  554. character size you asked for.
  555.  
  556. Intermediate files will be named 8"aa.txt", 8"ab.txt", 8"ac.txt", and so on."`
  557.             text=`echo "$text" | \
  558.             sed -e "s/1/$target.txt/" -e "s/2/$lincount/" \
  559.             -e "s/3/$rez/" -e "s/4/$size/" \
  560.             -e "s/5/$maxpossible/" -e "s/6/$target.txt/" \
  561.             -e "s/7/$maxpossible/" -e "s/8/$shorttarget/g"`
  562.             zenity --info --title="$title" --text="$text"
  563.             cat "$target" | split -$maxpossible - "$shorttarget"
  564.             targets=`ls $shorttarget??`
  565.             multiple=y
  566.             for each in `ls $shorttarget??`
  567.             do
  568.                 mv "$each" "$each.txt"
  569.             done
  570.     fi
  571.     ;;
  572. esac
  573. #End of extra stuff for text to image
  574.  
  575. # Now, do the actual conversion
  576.  
  577. # usually just one target
  578. targets="$target"
  579.  
  580. # in a text->image that needed splitting will be multiple $targets
  581. case $multiple in
  582.     y)  count=1
  583.         targets=`ls $shorttarget??*`
  584.         basenew="$new" ;;
  585. esac
  586.  
  587. echo "$targets" | \
  588. while read target
  589. do
  590. case $multiple in
  591.     y)  case $count in
  592.             ?)  count="00"$count ;;
  593.             ??) count="0"$count ;;
  594.         esac
  595.         new=`echo "$basenew" | sed "s/\....$/.$count&/"`
  596.         ;;
  597. esac
  598.  
  599. # some special handling of pdf files
  600. tpdf=n; npdf=n
  601. echo "$target" | grep "pdf$" >/dev/null && tpdf=y
  602. echo "$new" | grep "pdf$" >/dev/null && npdf=y
  603.  
  604. # if source=pdf and dest=image - set density for reasonable quality
  605. [ "$tpdf$npdf" = "yn" ] && density="-density 600"
  606. # case $tpdf$npdf in
  607.     # yn)   density="-density 600" ;;
  608. # esac
  609.  
  610.  
  611. # if source is multi-page, find how many
  612. # but ONLY if target is NOT gif or pdf
  613. echo "$target" | egrep -y "pdf|gif|" && tmult=y
  614. echo "$new" | egrep -y "pdf|gif|" && nmult=y
  615. case $tmult$nmult in
  616.     yy) ;;
  617.     *)
  618.     capext=`echo $ext | tr '[a-z]' '[A-Z]'`
  619.     multipage=`echo "$ident" | grep ".$ext\[[0-9].* $capext" | \
  620.         sed -e "s/ $capext.*//"`
  621.     case "$multipage" in
  622.         *[0-9]\])   multipage=`echo "$multipage" | \
  623.                     sed -e "s/^.*$ext.//" -e 's/.$//g'`
  624.                 export multipage each
  625.                
  626.                 ;;
  627.     esac
  628.     ;;
  629. esac
  630.  
  631. text=`mytranslate "Working on"`
  632. oldsize=`du -a -b "$target" | awk '{print $1}'`
  633. avwatcher "$outdir/$new" "$prog" "$oldsize" $$ &
  634. convert -verbose $density $geom $quality $normeq $textoptions"$target" "$outdir/$new" >/tmp/convert.$$.$count 2>&1
  635. case $overwrite in
  636.     YES)    rename=`echo "$target" | sed "s/$ext$/$each/"`
  637.         mv "$outdir/$new" "$rename"
  638.         rm "$target" ;;
  639. esac
  640. # ImagMagick/convert may call ufraw for camera raw images
  641. # there is a new version of ufraw with slightly different options
  642. # convert is using old options, causing a "deprecated" complaint
  643. # but the results are good as old options are still accepted.
  644. cleanup=`egrep -v "deprecated|ppm16" /tmp/convert.$$.$count` >/tmp/$$
  645. mv /tmp/$$ /tmp/convert.$$.$count
  646. if test -s /tmp/convert.$$.$count
  647.     then    message=`cat /tmp/convert.$$.$count`
  648.         text=`mytranslate "An error occured during the conversion
  649.  
  650. The message was: "`
  651.         zenity --info --text="$text $message" &
  652.     else    rm -f /tmp/convert.$$.$count
  653. fi
  654.  
  655. case $multiple in
  656.     y)  count=`expr $count + 1` ;;
  657. esac
  658. # end of multiple $targets loop
  659. done
  660.  
  661. # end of inside loop for $rez
  662. done
  663. # end of outside loop for $dims
  664. done
  665.  
  666. # convert -verbose $geom $quality $textoptions"$target" "$outdir/$new" 2>/tmp/convert.$$.$count | zenity --progress --text="Working on $new" --auto-close --auto-kill --pulsate
  667.  
  668. case $autoloop in
  669.     y)  echo "
  670.             choice=\"$choice\"
  671.             dim=\"$dim\"
  672.             dims=\"$dims\"
  673.             geom=\"$geom\"
  674.             qual=\"$qual\"
  675.             textoptions=\"$textoptions\"
  676.             normeq=\"$normeq\"
  677.                 " > /tmp/avconvert.env
  678.             chmod 666 /tmp/avconvert.env ;;
  679. esac
  680. }
  681. # end of avconvert
  682.  
  683.  
  684.  
  685. #####
  686. # ffmpeg
  687. #####
  688. myavffmpeg() {
  689. #
  690. # processing audio and video
  691.  
  692. # set -x
  693. # this is for looping after the first item, using the env for settings
  694. looping=n
  695. if test -s /tmp/avffmpeg.env
  696.     then    . /tmp/avffmpeg.env
  697.         looping=y
  698. fi
  699.  
  700. # set up output extensions
  701. case $av in
  702.     video)  out=" ogg ogv 3gp asf avi flv mkv mp4 mov mpg rm wmv flac_audio_only m4a_audio_only mp3_audio_only wav_audio_only wma_audio_only jpeg_frames OTHER"
  703.         height=550 ;;
  704.     audio)  out=" ogg flac m4a mp3 wav wma OTHER"
  705.         height=360 ;;
  706. esac
  707.  
  708. out=`echo "$out" | sed -e 's/ $ext //' -e "s/ / FALSE /g"`
  709.  
  710. # loop for output conversion type
  711. if [ "$looping" = "n" ]
  712.     then
  713. while true
  714. do
  715. title=`mytranslate "Convert"`
  716. text1=`mytranslate "Source file information"`
  717. text2=`mytranslate "Choose OTHER for an output type not listed"`
  718. choice=`zenity --list --height=$height --title="$title $target" --text="$text1 $show\n$ffident\n\n$text2" --radiolist --column "$s1" --column "$title" $out` || exit 0
  719.  
  720. if [ -n "$choice" ]
  721.         then      break
  722. fi
  723. done
  724. fi
  725.  
  726. # was it OTHER?
  727. case $choice in
  728.         OTHER)  title=`mytranslate "Output type"`
  729.         text=`mytranslate "Supply a valid file extension, example "`
  730.         choice=`zenity --entry --title="$title" --text="$text ogg / vob"` || exit 0 ;;
  731. esac
  732.  
  733.  
  734. case $choice in
  735.     *_audio*)   av=audio; choice=`echo $choice | sed 's/_.*//'` ;;
  736.     *_frames)   av=frame; choice=`echo $choice | sed 's/_.*//'`
  737.             outf=`echo $choice | awk -F_ '{print $1}'`
  738.             title=`mytranslate "Frame capture"`
  739.             text=`mytranslate "Number of frames to capture per second.
  740. The source file FPS is shown above."`
  741.             step=`zenity --scale --title="$title" --text="$show\n$ffident\n\n$text" --value=1 --min-value=0 --max-value=60 --step=1` || exit 0
  742.             # step="1/$step"
  743.             ;;
  744. esac
  745.  
  746. # we now have the output type
  747.  
  748. # ffmpeg options from options files
  749. ## SEED THE FILES IF NOT PRESENT
  750. # recreate the audio and video files, create custom if not present
  751.  
  752. cat <<!audio > ~/.config/avconvert/avconvert.ffopts.audio
  753. # ~/.config/avconvert/avconvert.ffopts.audio
  754. #
  755. # Do *NOT* put your own options here - they will be overwritten!
  756. #
  757. # Use ~/.config/avconvert/avconvert.ffopts.custom instead
  758. #
  759. TRUE AUDIO-CODEC-DEFAULT#based_on_conversion
  760. FALSE acodec-copy#use_codec_in_source
  761. FALSE acodec-ac3
  762. FALSE acodec-mp2
  763. FALSE acodec-libmp3lame
  764. TRUE AUDIO-SAMPLE-RATE-Default
  765. FALSE ar-44100
  766. FALSE ar-22050
  767. TRUE AUDIO-BITRATE-Default=64k
  768. FALSE ab-128k
  769. FALSE ab-256k
  770. TRUE VOLUME-Default=256
  771. FALSE vol-512
  772. !audio
  773.  
  774. cat <<!video > ~/.config/avconvert/avconvert.ffopts.video
  775. # ~/.config/avconvert/avconvert.ffopts.video
  776. #
  777. # Do *NOT* put your own options here - they will be overwritten!
  778. #
  779. # Use ~/.config/avconvert/avconvert.ffopts.custom instead
  780. #
  781. TRUE VIDEO-SIZE-UNCHANGED#or_choose_width_below,height_calculated_for_you
  782. FALSE w-128
  783. FALSE w-320
  784. FALSE w-640
  785. TRUE NO-ROTATION#rotation_requires_VLC_(using_KEY-IN,'vert'/'vlip'/'hflip'_too)
  786. FALSE rotate-right
  787. FALSE rotate-left
  788. TRUE DO-NOT-LETTERBOX#choose_aspect_of_your_player_to_avoid_stretching
  789. FALSE d-1.3333#4:3
  790. FALSE d-1.5#Apple_iPhone
  791. FALSE d-1.7778#16:9
  792. TRUE VIDEO-CODEC-UNCHANGED#based_on_conversion
  793. FALSE vcodec-copy#use_codec_in_source
  794. FALSE vcodec-libx264
  795. FALSE vcodec-mjpeg
  796. FALSE vcodec-mpeg2video
  797. !video
  798.  
  799. [ ! -f ~/.config/avconvert/avconvert.ffopts.custom ] && \
  800. cat <<!custom > ~/.config/avconvert/avconvert.ffopts.custom
  801. # ~/.config/avconvert/avconvert.ffopts.custom
  802. #
  803. # The purpose of THIS file is that you can create option-sets that
  804. # you use often, and apply them with a single click in avconvert
  805. #
  806. # This file will NOT be overwritten by updates, but if you remove it then
  807. # a new one will be created with all new options from the latest release
  808. #  
  809. # There MUST be only two "words" per line, separated by a space
  810. # TRUE or FALSE followed by an option.  The option has the "-" moved
  811. #   so that "-option parameter" becomes "option-paramater".
  812. # These can be combined by separating them with "~", as in
  813. #   option1-parameter~option2-paramater
  814. # A comment may be added by following options with "#comment" as in
  815. #   option1-parameter~option2-parameter#My-Comment-here
  816. #   s-640x480~vcodec-mpeg2video#my_custom_options
  817. # But there must be ONLY ONE space in each line
  818. #
  819. # An option for SIZING (-w) is NOT actually an ffmpeg option.  The real
  820. # size option is "-s WWWxHHH" where WWW and HHH are width and height and
  821. # would be written in an ffopts file as "s-WWWxHHH".  However, both here
  822. # and in ffopts.custom and ffopts.submissions, this script will use the
  823. # "-w WWW" (w-WWW) to trigger automatic resizing of the height to preserve
  824. # the original aspect ratio.  The supplied and calculated values will also
  825. # be rounded down to the nearst multiple of 2 pixels
  826. #
  827. # The DISPLAY option (-d) is ALSO not a true ffmpeg option.  It will be
  828. # used to determine if the video needs to be letterboxed so that aspect
  829. # ration is preserved when played on your device.  So regardless of SIZE,
  830. # if the original aspect does not match your specified DISPLAY, the math
  831. # will be done for you and black bands added to make the video fit.
  832. # Again, this form may also be used in .custom and .submissions files.
  833.  
  834. TRUE NO-CUSTOM-OPTIONS_these.are.created.using.KEY-IN
  835. FALSE s-640x480~vcodec-mpeg2video~acodec-mp2#Sample_custom_command
  836. !custom
  837.  
  838. grep tsi-inc ~/.config/avconvert/avconvert.ffopts.custom >/dev/null || cat <<!message >> ~/.config/avconvert/avconvert.ffopts.custom
  839. #
  840. # One more thing:  if you come up with some handy (or necessary) option
  841. # sets for specific conversions and submit them to me, I will start
  842. # maintaining a ~/.config/avconvert/avconvert.ffopts.submissions file and
  843. # regularly post it for download.
  844. # Just email me your option line in the format below.
  845. # Include a comment section, and describe the circumstances that require
  846. # it.  For example "when the input file is *.xxx and the output file is
  847. # *.yyy and ffmpeg identifies the audio stream as zzz.
  848. # Send submissions to:  tsi-inc@comcast.net
  849. # and include "avconvert submission" in the subject line.
  850. !message
  851. cat <<!ffmpeghelp > ~/.config/avconvert/avconvert.ffmpeg-help
  852. NOTE: an option like "-ss 60 -t 30" (seek in 60 seconds and record 30 seconds)
  853. would be written in ~/.config/avconvert/avconvert.ffopts* as:
  854.     ss-60~t-30#seek-60_record-30
  855. but here, use standard command-line syntax, like this:
  856.     -ss 60 -t 30
  857.  
  858. -t time     "time" is in seconds OR HH:MM:SS.xxx            stop recording after "time"
  859. -fs NNN     "filesize" in bytes                         stop recording at this "size"
  860. -f FFF      format as in "rawvideo" "oss"
  861. -ss time        "time is in seconds OR HH:MM:SS             start recording at "time"
  862. -target     eg. "vcd" "dvd" "ntsc" sets all appropriate options for this type
  863. -b rate     video bitrate in bit/s                      default "200 kb/s" (200000)
  864. -r fps      video frames per second in HZ                   default "25" fps
  865. -s size     frame size HHHxVVV (default is retain original) HORIZONTALxVERTICAL size
  866. -aspect     4:3  16:9  1.3333  1.7777
  867. -croptop        NN crop NN pixels from the top              produces a band
  868. -cropbottom -cropleft   -cropright                      (see croptop)
  869. -padtop     NN  pad NNN pixels on the top               produces a band
  870. -padbottom  -padleft   -padright                            (see padtop)
  871. -padcolor   H 6-digit HEX value (eg 000000) for padded bands
  872. -vcodec     CC  CC is codec name as in "libxvid" "mpeg4"
  873. -pass N     N = 1 or 2                                              1-pass or 2-pass vid encoding
  874. -ilme       (MPEG-2/4) if input interlaced, retain it for higher quality
  875. -ar         NNN     audio sample frequency 44100/22050/11025 default 44100
  876. -ab         NNN     audio birate in bit/s                            default 64k
  877. -ac         N       number of audio channels                         default 1
  878. -an         disable audio recording
  879. -acodec CCC audio codec name as in "ac3" "mp2"
  880. -tvstdc SSS set TV standard to NTSC PAL SECAM
  881. -shortest       finish recording when shortest input stream ends
  882.  
  883. Not OFFICIALLY ffmpeg options, but handled by avconvert:
  884.  
  885. -w          WWW specify a new video width, height is calculated for you
  886. -d          A.AAAA  specify the HxW ratio of a display device for letterboxing to preserve aspect ratio
  887. !ffmpeghelp
  888.  
  889. ## END OF SEEDING THE FILES
  890. # THE WORK STARTS HERE...
  891.  
  892. # If we are NOT in a loop, ask for options...
  893. if [ "$looping" = "n" ]
  894.     then
  895. case $av in
  896.     audio)  storedopts=`cat ~/.config/avconvert/avconvert.ffopts.audio ~/.config/avconvert/avconvert.ffopts.custom ~/.config/avconvert/avconvert.ffopts.submissions 2>/dev/null` ;;
  897.     video)  storedopts=`cat ~/.config/avconvert/avconvert.ffopts.audio ~/.config/avconvert/avconvert.ffopts.video ~/.config/avconvert/avconvert.ffopts.custom ~/.config/avconvert/avconvert.ffopts.submissions 2>/dev/null` ;;
  898. esac
  899. storedopts=`echo "$storedopts" | sed 's/^#.*//'`
  900.  
  901. title=`mytranslate "More options"`
  902. text1=`mytranslate "Source file:"`
  903. text2=`mytranslate "Select a combination of options, or leave any UNCHANGED.
  904. You may choose options here AND click KEY-IN-OPTIONS.  The selected and keyed options will both be used.
  905. There is a help page for key-in options.
  906.  
  907. Note that leaving an option UNCHANGED might actually result in some default actions, so check your results!
  908.  
  909. These options are stored in ~/.config/avconvert/avconvert.ffopts*
  910. You can modify the avconvert.ffopts.custom file, which will not be overwritten.
  911. Use your favorite text editor and follow instructions contained in that file."`
  912. c1=`mytranslate "Choose ONLY ONE from each category"`
  913.  
  914. ffopts=`zenity --list --height=600 --title="$title" --text="$text1 $target\n$ffident\n\n$text2" --checklist --column "$s1" --column "$c1" $storedopts FALSE KEY-IN-OPTIONS` || exit 0
  915.  
  916. if echo "$ffopts" | grep KEY-IN-OPTIONS >/dev/null
  917.     then    title=`mytranslate "Quick list of ffmpeg options"`
  918.         zenity --info --no-wrap --title="FFMPEG:  $title" --text="CURRENT SOURCE FILE:\n\n$ffident\n\n`cat ~/.config/avconvert/avconvert.ffmpeg-help`" 2>/dev/null &
  919.         sleep .5
  920.         zpid=`ps -ef | grep -v grep | grep -e "--title=FFMPEG:" | \
  921.             awk '{print $2}'`
  922.         title=`mytranslate "Key in your options"`
  923.         text=`mytranslate "Examples (leave blank for none)
  924. "`
  925.         text2=`mytranslate "
  926.  
  927. If you add a comment, then your options will be saved for future use
  928. in ~/.config/avconvert.ffopts.custom.  Example:   "`
  929.         text3=`mytranslate "My favorite"`
  930. dopts=`echo "$ffopts" | tr '|' '\012' | sed 's/^[A-Z].*//' | grep -v '^$'`
  931. dopts=`echo "$dopts" | sed -e 's/^/ /' -e 's/#.*//'`
  932. dopts=`echo "$dopts" | sed -e 's/~/ /g' -e 's/-/~/g' -e 's/ / -/g' -e 's/~/ /g'`
  933. case "$dopts" in
  934.     " -")   dopts="" ;;
  935. esac
  936.         keyin=`zenity --entry --title="$title" --text="$text     -acodec mpeg3 -vcodec libx264 -ab 320k$text2 -acodec mpeg2 -vcodec libx264 # $text3
  937.  
  938. Current ffmpeg options:
  939. $dopts"`
  940.         kill -15 $zpid >/dev/null 2>&1
  941.         if echo "$keyin" | grep "#" >/dev/null
  942.             then
  943.             opt=`echo "$keyin" | sed -e 's/  */ /g' -e 's/^ //'`
  944.             com=`echo "$opt" | sed -e 's/^.*#//' \
  945.                 -e 's/^ //' -e 's/ $//' -e 's/ /_/g'`
  946.             opt=`echo "$opt" | sed 's/#.*//'`
  947.             for word in $opt
  948.             do
  949.                 case $word in
  950.                 -*) line=$line`echo $word | sed 's/-//'` ;;
  951.                 *)  line=$line-$word~ ;;
  952.                 esac
  953.             done
  954.             line=`echo "$line#$com" | sed 's/~#/#/'`
  955.             echo "FALSE $line" >> ~/.config/avconvert/avconvert.ffopts.custom
  956.             keyin=`echo "$keyin" | sed 's/#.*//'`
  957.         fi
  958. fi
  959.  
  960. # manage the SPECIAL options for resizing and letterboxing
  961. size=`echo "$ffident" | grep Video | awk -F, '{print $3}'`  #current size per ffmpeg
  962. size=`echo $size | sed 's/,//'`
  963. curw=`echo $size | sed 's/x.*$//'`          #current width
  964. oldw=$curw
  965. curh=`echo $size | sed 's/^.*x//'`          #current height
  966. cura=`echo -e "scale=2\n$curw / $curh\nquit" | bc`  #current aspect
  967. wopt=`echo "$ffopts" | grep "w-" | sed 's/^.*w-//' | \
  968.     sed -e 's/|.*//' -e 's/#.*//'`          #new width
  969. neww=$wopt
  970. disp=`echo "$ffopts" | grep "d-" | sed 's/^.*d-//' | \
  971.     sed -e 's/|.*//' -e 's/#.*//'`           #disp asp
  972. if [ ! -z "$neww" ]
  973.     then    neww=`expr $neww / 2 \* 2`      #round down
  974.         scale=`echo -e "scale=5\n$curw / $neww\nquit" | bc` #height chg
  975.         newh=`echo -e "scale=5\n$curh / $scale\nquit" | bc` # new HHH
  976.         newh=`echo $newh | sed 's/\..*//'`  #integer
  977.         newh=`expr $newh / 2 \* 2`      #round down
  978.         curh=$newh              #new HHH
  979.         oldw=$curw
  980.         curw=$neww
  981.         nopt="$neww"x"$newh"            #WWWxHHH string
  982.         ffopts=`echo "$ffopts" | sed "s/w-$wopt/s-$nopt/"` #substitute
  983. fi
  984.  
  985. if [ ! -z "$disp" ]
  986. then    # *10000 and remove decimal, so "test" can treat as integer
  987.     # which aspect is wider? to calculate padding
  988.     curam=`echo -e "scale=5\n$cura * 10000\nquit" | bc`
  989.     dispm=`echo -e "scale=5\n$disp * 10000\nquit" | bc`
  990.     curam=`echo $curam | sed 's/\..*//'`
  991.     dispm=`echo $dispm | sed 's/\..*//'`
  992.     if [ "$cura" != "$disp" ]
  993.     then    if [ "$curam" -ge "$dispm" ]
  994.             then    # current aspect is wider than target aspect
  995.                 # pad top and bottom
  996.         xtra=`echo -e "scale=5\n$cura / $disp * $curh\nquit" | bc` #dots
  997.             pad=tb
  998.             else    # target aspect is wider than current aspect
  999.                 # pad left and right
  1000.         xtra=`echo -e "scale=5\n$disp / $cura * $curw\nquit" | bc` #dots
  1001.             pad=lr
  1002.         fi
  1003.         # $xtra is the total dots HHH or WWW in display aspect
  1004.         xtra=`echo $xtra | sed 's/\..*//'`  #integer
  1005.         # ACTUAL pad is $xtra - soze-of-image
  1006.         case $pad in
  1007.             tb) xtra=`expr $xtra - $curh + 1` ;;  #subtract HHH
  1008.             lr) xtra=`expr $xtra - $curw + 1` ;;  #subtract WWW
  1009.         esac
  1010.         # the padding should be half/half either L/R or T/B
  1011.         xtra=`expr $xtra / 2`           #top/bottom
  1012.         xtra=`expr $xtra / 2 \* 2`      #round down
  1013.         # create command option
  1014.         case $pad in
  1015.             tb) padding="padtop-$xtra|padbottom-$xtra" ;;
  1016.             lr) padding="padleft-$xtra|padright-$xtra" ;;
  1017.         esac
  1018.         ffopts=`echo "$ffopts" | \
  1019.             sed "s/d-$disp/$padding|aspect-$disp/"`
  1020.     fi
  1021. fi
  1022. ## end of aspect and padding section
  1023.  
  1024. ffopts=`echo "$ffopts" | tr '|' '\012' | sed 's/^[A-Z].*//' | grep -v '^$'`
  1025. if echo "$ffopts" | grep rotate >/dev/null
  1026.     then    rotate=`echo "$ffopts" | grep rotate`
  1027.         ffopts=`echo "$ffopts" | grep -v rotate`
  1028.         case $rotate in
  1029.             *right) rotate=90 ;;
  1030.             *vert)  rotate=180 ;;
  1031.             *left)  rotate=270 ;;
  1032.             *vflip) rotate=vflip ;;
  1033.             *hflip) rotate=hflip ;;
  1034.         esac
  1035. fi
  1036. ffopts=`echo "$ffopts" | sed -e 's/^/ /' -e 's/#.*//'`
  1037. ffopts=`echo "$ffopts" | sed -e 's/~/ /g' -e 's/-/~/g' -e 's/ / -/g' -e 's/~/ /g'`
  1038. case "$ffopts" in
  1039.     " -")   ffopts=" " ;;
  1040. esac
  1041.  
  1042. ffopts="$ffopts $keyin"
  1043. # echo -e "\n\nThe final ffmpeg options look like this:\n"
  1044. # echo $ffopts
  1045.  
  1046. # END OF OPTION-SETTING
  1047. fi
  1048.  
  1049. new=`echo $target | sed "s/$ext$/$dot$choice/"`
  1050.  
  1051. case $av in
  1052.     frame)  new="$target.frame-%04d.$outf"
  1053.         specialopts="-f image2 -r $step" ;;
  1054. esac
  1055.  
  1056. case $new in
  1057.     *3gp)   x264=`ls /usr/lib/*x264* | sed 's/\..*//' | head -1`
  1058.         x264=`basename $x264`
  1059.         specialopts="-vcodec $x264 -acodec libfaac" ;;
  1060.     *flv)   case "$ffopts" in
  1061.             *-ar*)  ;;
  1062.             *)  ar=`echo "$ffident" | grep Audio | \
  1063.                 awk -F, '{print $2}' |
  1064.                 sed -e 's/Hz//' -e 's/ //g'`
  1065.                 if [ "$ar" -gt "11025" ]
  1066.                     then    nr=11025
  1067.                 fi
  1068.                 if [ "$ar" -gt "22050" ]
  1069.                     then    nr=22050
  1070.                 fi
  1071.                 if [ "$ar" -gt "44100" ]
  1072.                     then    nr=44100
  1073.                 fi
  1074.                 ffopts="$ffopts -ar $nr"
  1075.                 ;;
  1076.         esac
  1077. esac
  1078.  
  1079. case "$outdir/$new" in
  1080.     "$dir/$target") title=`mytranslate "Destination file has the same name as source!!  Choose a new name"`
  1081.             newout=`zenity --file-selection --filename="$outdir/$new" --save --confirm-overwrite --title="$title"` || exit 0
  1082.             outdir=`dirname "$newout"`
  1083.             new=`basename "$newout"`
  1084.             ;;
  1085. esac
  1086.  
  1087. if test -s "$outdir/$new"
  1088.     then    text=`mytranslate "exists - Overwrite?
  1089.  
  1090. (OK to overwrite, Cancel to supply a new name)"`
  1091.         zenity --question --text="$outdir/$new $text" || newout=`zenity --file-selection --filename="$outdir/$new" --save --confirm-overwrite --title="Choose a new file/folder"` || exit 0
  1092.         if [ ! -z "$newout" ]
  1093.             then    outdir=`dirname "$newout"`
  1094.                 new=`basename "$newout"`
  1095.         fi
  1096. fi
  1097.  
  1098. oldsize=`du -a -b "$target" | awk '{print $1}'`
  1099. case $rotate in
  1100.     ?*) tempfile=`echo "$target" | sed 's/^/rotate-/'`
  1101.         avwatcher "$outdir/$tempfile" "mencoder" "$oldsize" $$ &
  1102.         # first attempt:
  1103.         # mencoder -vf rotate=$rotate -o "$outdir/$tempfile" -oac copy -ovc lavc "$target"
  1104.         # better quality:
  1105. mident=`midentify "$target"`
  1106. arate=`echo "$mident" | grep "AUDIO_BITRATE" | tail -1 | awk -F= '{print $2}'`
  1107. vrate=`echo "$mident" | grep "VIDEO_BITRATE" | tail -1 | awk -F= '{print $2}'`
  1108. rpass1=`mencoder "$target" -ovc lavc -lavcopts vbitrate=$vrate:vpass=1:turbo:threads=2 -oac copy -passlogfile $outdir/2pass.log -o "/tmp/mencoder-pass1-$tempfile" 2>&1`
  1109. rpass2=`mencoder "$target" -vf rotate=$rotate -ovc lavc -lavcopts vbitrate=$vrate:vpass=2:vhq=4:threads=2:abitrate=$arate -oac copy -passlogfile $outdir/2pass.log -o "$outdir/$tempfile" 2>&1`
  1110. echo -e "\tPASS 1\n\n$rpass1" > /tmp/mencoder-rotate.$$
  1111. echo "\n\n\tPASS 2\n\n$rpass2" >> /tmp/mencoder-rotate.$$
  1112. rm -f $outdir/2pass.log
  1113. midentr=`midentify "$outdir/$tempfile"`
  1114. arater=`echo "$midentr" | grep "AUDIO_BITRATE" | tail -1 | awk -F= '{print $2}'`
  1115. vrater=`echo "$midentr" | grep "VIDEO_BITRATE" | tail -1 | awk -F= '{print $2}'`
  1116. sizeof=`wc "$outdir/$tempfile" | awk '{print $3}'`
  1117. if [ "$arater"="0" -o "$vrater"="0" -o "$sizeof"="0" ]
  1118.     then    zenity --warning --title="Rotate Failed" --text="Rotation Failed!  :-(\n\nMencoder does not work well with all codecs.\nTry converting your file first (perhaps to AVI) and rotate the result"; exit 0
  1119. fi
  1120.         #   NEW WAY with VLC, which apparently doesn't work "yet"
  1121.         #   BECAUSE vlc will NOT write 'transforms' to a file!
  1122.         #   so the IMPROVED MENCODER code above is being used
  1123.         # avwatcher "$outdir/$tempfile" "vlc" "$oldsize" $$ &
  1124.         # vlc -I dummy "$target" -vvv --vout-filter=transform --transform-type=$rotate --sout '#transcode{deinterlace,audio-sync}:standard{dst="$tempfile",access=file}' vlc://quit > /tmp/vlc.$$ 2>&1
  1125.         target="$outdir/$tempfile"
  1126.         oldsize=`du -a -b "$target" | awk '{print $1}'`
  1127.         ;;
  1128. esac
  1129.  
  1130. avwatcher "$outdir/$new" "$prog" "$oldsize" $$ &
  1131.  
  1132. echo -e "ffident:\n$ffident\n" >/tmp/ffmpeg.$$
  1133. echo "ffmpeg command as executed:" >>/tmp/ffmpeg.$$
  1134. echo ffmpeg -y -i "$target" $vol $specialopts $ffopts "$outdir/$new" >>/tmp/ffmpeg.$$
  1135. echo >>/tmp/ffmpeg.$$
  1136. # set -x
  1137. ffmpeg -y -i "$target" $vol $specialopts $ffopts "$outdir/$new" 2>&1 | tr '=' '\012' >> /tmp/ffmpeg.$$
  1138.  
  1139. case $rotate in
  1140.     ?)  echo "mencoder rotated original file to $outdir/$tempfile" >>/tmp/ffmpeg.$$
  1141.         if test -s "/tmp/$tempfile"
  1142.             then    : it worked
  1143.             else    echo "but that operation failed" >>/tmp/ffmpeg.$$
  1144.         fi ;;
  1145. esac
  1146.  
  1147. result=`tail -5 /tmp/$prog.$$`
  1148. result="$result
  1149.  
  1150. The full text output from this conversion is in /tmp/$prog.$$
  1151. "
  1152.  
  1153. grep "lame: output buffer too small" /tmp/ffmpeg.$$ && note=`mytranslate "
  1154. The message about libmp3lame buffer is meaningless.
  1155. If that is the only complaint message, the conversion did work."`
  1156.  
  1157. grep "bitrate tolerance too small" /tmp/ffmpeg.$$ && note=`mytranslate "
  1158. The message about bitrate tolerance too small is due
  1159. to the way ffmpeg works.  Seconds between frames can not be
  1160. larger than about 2/3 of the frames per second in the video.
  1161.  
  1162. POSSIBLE SOLUTION:  Specify a smaller seconds between frames"`
  1163.  
  1164. grep "does not support" /tmp/ffmpeg.$$ && note=`mytranslate "
  1165. ffmpeg requires more/different options for this conversion
  1166.     OR
  1167. your source file is unusual and requires special options
  1168.  
  1169. POSSIBLE SOLUTIONS:
  1170.     specify a video or audio codec
  1171.     specify a sample rate
  1172.     specify a different output size"`
  1173.  
  1174. newsize=`du -a -b "$new" | sed 's/   .*//'`
  1175. # show results summary
  1176. case $quiet in
  1177.     y)  notify="--notification --window-icon=/usr/share/zenity/zenity-progress.png"
  1178.         cp /usr/share/zenity/zenity.glade.progress-under /usr/share/zenity/zenity.glade 2>/dev/null ;;
  1179.     n)  notify="--info"
  1180.         cp /usr/share/zenity/zenity.glade.progress-over /usr/share/zenity/zenity.glade 2>/dev/null ;;
  1181. esac
  1182.  
  1183. title=`mytranslate "Conversion results"`
  1184. text=`mytranslate "Last message in"`
  1185. text2=`mytranslate "File size:"`
  1186. zenity $notify --title="$title" --text="$target --> $new
  1187. $text2 $newsize
  1188. $note
  1189.  
  1190. $prog $text /tmp/$prog.$$:
  1191. $result" &
  1192. sleep .25
  1193. zpid=`ps -ef | grep -v grep | grep -e "--notification" | awk '{print $2}'`
  1194. case "$zpid" in
  1195.     '') ;;
  1196.     *)  (sleep 60; kill -9 $zpid >/dev/null 2>&1) & ;;
  1197. esac
  1198.  
  1199. case $autoloop in
  1200.     y)  echo -e "choice=\"$choice\"\nbits=\"$bits\"" > /tmp/avffmpeg.env
  1201.         chmod 666 /tmp/avffmpeg.env ;;
  1202. esac
  1203. }
  1204. # end of ffmpeg
  1205.  
  1206.  
  1207. #####
  1208. # ISO conversions
  1209. #####
  1210. myaviso() {
  1211.  
  1212. case $it in
  1213.     bchunk)
  1214.         which bchunk >/dev/null 2>&1 || exec zenity --warning --text="Must install \"bchunk\" package first!"
  1215.         tracks=`grep TRACK $base.cue | awk '{print $2}'`
  1216.         zenity --question --text="BIN/CUE to ISO conversion\nCUE shows tracks $tracks\nProgress will be idicated for first track only\nwhich will be named "$base"01.iso" || exit 0
  1217.         new="$outdir/$base"
  1218.         if test -s "$new"01.iso
  1219.             then    zenity --question --text="$new"01.iso" exists - Overwrite?" || new=`zenity --file-selection --filename="$outdir/$new" --save --confirm-overwrite --title="Choose a new file/folder"` || exit 0
  1220.         fi
  1221.         prog=bchunk
  1222.         oldsize=`du -a -b "$base.bin" | awk '{print $1}'`
  1223.         avwatcher "$new"01.iso "$prog" "$oldsize" $$ &
  1224.         bchunk "$base.bin" "$base.cue" "$new" >/tmp/bchunk.$$ 2>&1
  1225.         ;;
  1226.         daa*|nrg*|b?i*|cdi*|mdf*|pdi*|ccd*|uif*)
  1227.         which $it >/dev/null 2>&1 || exec zenity --warning --text="No program \"$it\" found for processing \"$target\"!\n\nFor ISO conversions, install one or more of these packages...\n\nAcetoneISO for:  b5i cdi mdf nrg pdi\nnrg2iso for:     nrg\ndaa2iso for:     daa\nbchunk for:      bin/cue\nccd2iso for:     cdd\nuif2iso for:      uif\n\ncdd2iso is available at: sourceforge.net/projects/ccd2iso\nuif2iso is available at: aluigi.altervista.org/mytoolz.htm\n\nthe rest are in the repositories."
  1228.         base=`echo $target | sed 's/....$//'`
  1229.                 zenity --question --text="Source file is $target\n$ext to ISO conversion\nwhich will be named $base.iso" || exit 0
  1230.                 new="$outdir/$base".iso
  1231.                 if test -s "$new".iso
  1232.                         then     zenity --question --text="$new".iso" exists - Overwrite?" || new=`zenity --file-selection --filename="$outdir/$new" --save --confirm-overwrite --title="Choose a new file/folder"` || exit 0
  1233.                 new="$new".iso
  1234.                 fi
  1235.         prog=$ext"2iso"
  1236.         oldsize=`du -a -b "$target" | awk '{print $1}'`
  1237.         avwatcher "$new" "$prog" "$oldsize" $$ &
  1238.         case $ext in
  1239.             b?i|pdi)    new="" ;;
  1240.         esac
  1241.         $prog "$target" "$new" >/tmp/$prog.$$ 2>&1
  1242.         ;;
  1243. esac
  1244.  
  1245. }
  1246. # end of iso conversions
  1247. #####
  1248. # Text conversions
  1249. #####
  1250. myavtext() {
  1251. #
  1252. # processing text between formats and into other things
  1253.  
  1254. # set -x
  1255.  
  1256. case $it in
  1257.     text)   # image->avconvert sound->espeak html->txt2tags
  1258.         out=" image sound html"; height=300 ;;
  1259.     doc)    # text->antiword
  1260.         out=" text"; height=300 ;;
  1261.     rtf)    # text->unrtf html->unrtf
  1262.         out=" text html"; height=300 ;;
  1263.     odt)    # text->odt2txt xml->odt2txt
  1264.         out=" text xml"; height=300 ;;
  1265. esac
  1266.  
  1267. out=`echo "$out" | sed -e 's/ $ext //' -e "s/ / FALSE /g"`
  1268.  
  1269. # loop for output conversion type
  1270. while true
  1271. do
  1272. title=`mytranslate "Convert"`
  1273. text=`mytranslate "Source file format"`
  1274. text2=`mytranslate "Convert to format"`
  1275. choice=`zenity --list --height=$height --title="$title $target" --text="$text $show\n$ffident\n" --radiolist --column "$s1" --column "$text2" $out` || exit 0
  1276.  
  1277. if [ -n "$choice" ]
  1278.         then      break
  1279. fi
  1280. done
  1281.  
  1282. # pre-check for installed application
  1283. case $it$choice in
  1284.     textsound)  if ! which espeak >/dev/null 2>&1
  1285.             then    zenity --warning --title=HELP --text="\"espeak\" package needs to be installed"
  1286.                 exit 0
  1287.             fi ;;
  1288.     texthtml)   if ! which txt2tags >/dev/null 2>&1
  1289.             then    zenity --warning --title=HELP --text="\"txt2tags\" package needs to be installed"
  1290.                 exit 0
  1291.             fi ;;
  1292.     doctext)    if ! which antiword >/dev/null 2>&1
  1293.             then    zenity --warning --title=HELP --text="\"antiword\" package needs to be installed"
  1294.                 exit 0
  1295.             fi ;;
  1296. esac
  1297.  
  1298. case $it$choice in
  1299.     textimage)  touch /tmp/avconvert.debug
  1300.             chmod 666 /tmp/avconvert.debug
  1301.             prog=convert; myavconvert 2>/tmp/avconvert.debug ;;
  1302.     textsound)  title=`mytranslate "Language"`
  1303.             text=`mytranslate "Choose a language for pronunciation"`
  1304.             pronun=`zenity --list --height=500 --title="$title" --text="$text" --radiolist --column "$s1" --column "$title" FALSE af_afrikaans FALSE bs_bosnian FALSE cs_czech FALSE cy_welsh FALSE de_german FALSE el_greek TRUE en_english FALSE es_spanish FALSE fi_finnish FALSE fr_french FALSE hi_hindi FALSE hr_croatian FALSE hu_hungarian FALSE is_icelandic FALSE it_italian FALSE la_latin FALSE mk_macedonian FALSE nl_dutch FALSE no_norwegian FALSE pl_polish FALSE pt_brazil FALSE ro_romanian FALSE ru_russian FALSE sk_slovak FALSE sr_serbian FALSE sv_swedish FALSE sw_swahihi FALSE vi_vietnam FALSE zh_Mandarin` || exit 0
  1305.             pronun=`echo $pronun | sed 's/_.*//'`
  1306.             title=`mytranslate "Voice"`
  1307.             text=`mytranslate "Choose voice to use"`
  1308.             voice=`zenity --list --height=400 --title="$title" --text="$text" --radiolist --column "$s1" --column "$title" FALSE m1_male-1 FALSE m2_male-2 FALSE m3_male-3 FALSE m4_male-4 FALSE f1_female-1 TRUE f2_female-2 FALSE f3_female-3 FALSE f4_female-4 FALSE croak FALSE whisper` || exit 0
  1309.             voice=`echo $voice | sed 's/_.*//'`
  1310.             text=`mytranslate "Pitch"`
  1311.             pitch=`zenity --scale --text="$text" --min-value=0 --max-value=99 --value=50` || exit 0
  1312.             text=`mytranslate "Speed"`
  1313.             speed=`zenity --scale --text="$text" --min-value=0 --max-value=99 --value=43` || exit 0
  1314.             speed=`expr $speed \* 4`
  1315.             new=`echo "$target" | \
  1316.                 sed -e 's/.txt$//' -e 's/$/.wav/'`
  1317.             oldsize=`du -a -b "$target" | awk '{print $1}'`
  1318.             avwatcher "$outdir/$new" "$prog" "$oldsize" $$ &
  1319.             cat "$target" | espeak --stdin -v$pronun+$voice -p $pitch -s $speed -w "$outdir/$new" >/tmp/espeak.$$ 2>&1
  1320.             ;;
  1321.     texthtml)   new=`echo "$target" | \
  1322.                 sed -e 's/.txt$//' -e 's/$/.html/'`
  1323.             oldsize=`du -a -b "$target" | awk '{print $1}'`
  1324.             avwatcher "$outdir/$new" "$prog" "$oldsize" $$ &
  1325.             txt2tags --infile="$target" --outfile="$outdir/$new" -t xhtml >> /tmp/txt2tags.$$ 2>&1
  1326.             if test -s /tmp/txt2tags.$$
  1327.             then    title=`mytranslate "program output"`
  1328.                 zenity --info --title="txt2tags $title" --text=`cat /tmp/txt2tags.$$`
  1329.             fi
  1330.             rm -f /tmp/txt2tags.$$
  1331.             ;;
  1332.     doctext)    new=`echo "$target" | \
  1333.                 sed -e 's/.doc$//' -e 's/$/.txt/'`
  1334.             title=`mytranslate "Options"`
  1335.             text=`mytranslate "Include either of these?
  1336.  
  1337. r = \"revisioning text\"
  1338. s = \"hidden text\""`
  1339.             text2=`mytranslate "Embedded text"`
  1340.             incl=`zenity --list --height=300 --title="$title" --text="$text" --checklist --column "$s1" --column "$text2" FALSE r FALSE s ` || exit 0
  1341.             case $incl in
  1342.                 '') ;;
  1343.                 *)  opts=`echo "$incl" | sed 's/|/ /g' | \
  1344.                     sed 's/^/ /' | sed 's/ / -/g'` ;;
  1345.             esac
  1346.             oldsize=`du -a -b "$target" | awk '{print $1}'`
  1347.             avwatcher "$outdir/$new" "$prog" "$oldsize" $$ &
  1348.             antiword $opts "$target" > "$outdir/$new" 2>>/tmp/antiword.$$
  1349.             if test -s /tmp/antiword.$$
  1350.             then    zenity --info --title="antiword output" --text=`cat /tmp/antiword.$$`
  1351.             fi
  1352.             rm -f /tmp/antiword.$$
  1353.             ;;
  1354.     *)  text=`mytranslate "Not ready yet"`
  1355.         zenity --info --title="OOPS!" --text="$text"; exit 0 ;;
  1356.  
  1357. esac
  1358. }
  1359. # end of text
  1360.  
  1361. #####
  1362. # Initial script
  1363. #####
  1364. PATH=$PATH:.:~/.gnome2/nautilus-scripts; export PATH
  1365.  
  1366. # check some stuff first...
  1367. # have we been renamed?
  1368. case `basename $0` in
  1369.     avconvert)  ;;
  1370.     *)      exec xterm -hold -geometry 70x2 -bg red -T "Please rename the script back to 'avconvert'" -e true ;;
  1371. esac
  1372. # if zenity is not installed, use xterm to display a warning
  1373. which zenity >/dev/null 2>&1 || exec xterm -hold -geometry 50x2 -bg red -T "OOPS! Must install zenity first!" -e true
  1374. # if imagemagick is not installed, display warning
  1375. which convert >/dev/null 2>&1 || im="\n\t\"ImageMagick\""
  1376. # if ffmpeg is not installed, display warning
  1377. which ffmpeg >/dev/null 2>&1 || ffmpeg="\n\t\"ffmpeg\""
  1378. # etc, etc
  1379. which wget >/dev/null 2>&1 || wget="\n\t\"wget\""
  1380. which curl >/dev/null 2>&1 || curl="\n\t\"curl\""
  1381. which gawk >/dev/null 2>&1 || gawk="\n\t\"gawk\""
  1382. which links >/dev/null 2>&1 || which elinks >/dev/null 2>&1 || links="\n\t\"links\" or \"elinks\""
  1383. which bc >/dev/null 2>&1 || bc="\n\t\"bc\""
  1384.  
  1385. # special handling for translate
  1386. translate="\n\t\"libtranslate\" or \"libtranslate-bin\" or \"translate-shell\"\n\t  (whichever your distro offers)"
  1387. which translate-bin >/dev/null 2>&1 && info translate-bin | grep -q "translate a text" && trbin=/usr/bin/translate-bin
  1388. which translate >/dev/null 2>&1 && info translate | grep -q "translate a text" && trbin=/usr/bin/translate
  1389. which trans >/dev/null 2>&1 && info trans | grep -q "Command-line translator" && trbin="/usr/bin/trans -b"
  1390. case $trbin in
  1391.         /*)     translate="" ;;
  1392. esac
  1393.  
  1394. case $im$ffmpeg$wget$curl$gawk$links$bc$translate in
  1395.     ?*) exec zenity --warning --text "Please install the package$im$ffmpeg$wget$curl$gawk$links$bc$translate\nand start the conversion again!" ;;
  1396. esac
  1397.  
  1398. # the config files are now moved....
  1399. mkdir -p ~/.config/avconvert
  1400. if test -f ~/.avconvert
  1401.     then    for each in ~/.avconvert*
  1402.         do
  1403.             new=`echo "$each" | sed 's/^.*\/.//'`
  1404.             mv $each ~/.config/avconvert/$new
  1405.         done
  1406. fi
  1407.  
  1408. # translation
  1409.  
  1410. langlist=`$trbin -f en -l | \
  1411.     sed -e 's/.*-> //' -e 's/ /=/' -e 's/:.*//' | \
  1412.     tr -d '()\040'`
  1413.  
  1414. dlang=`echo "$LANG" | sed 's/_.*//'`
  1415. langlist=`echo -e "en=English\n$langlist" | sort | uniq | \
  1416.     sed -e 's/^/FALSE /' -e "/ $dlang=/s/FALSE/TRUE/"`
  1417.  
  1418. online=n
  1419. #echo "$langlist" | grep English >/dev/null && online=y
  1420. if ping -c 1 google.com >/dev/null 2>&1
  1421.     then    online=y
  1422. fi
  1423.  
  1424. # if language preference file is out of date, remove it
  1425. if grep "lang=" ~/.config/avconvert/avconvert.lang >/dev/null 2>&1
  1426.     then    :
  1427.     else    rm -f ~/.config/avconvert/avconvert.lang
  1428. fi
  1429.  
  1430. # requirements for translations
  1431. if which curl >/dev/null 2>&1
  1432.     then    curlok=y
  1433.     else    curlok=n; text="$text\n\tcurl"
  1434. fi
  1435. if which elinks >/dev/null 2>&1
  1436.     then    linksok=y; links=elinks
  1437.     else    if which links >/dev/null 2>&1
  1438.             then    linksok=y; links=links
  1439.             else    linksok=n; text="$text\n\telinks  -OR-  links"
  1440.         fi
  1441. fi
  1442.  
  1443. # if we are not online, do not ask about language
  1444. if [ "$online" = "n" ]
  1445.     then    lang=en
  1446.     else
  1447. # check language
  1448. if test -s ~/.config/avconvert/avconvert.lang
  1449.     then    . ~/.config/avconvert/avconvert.lang
  1450.         case $curlok$linksok in
  1451.             *n*)    if [ "$lang" = "en" ]
  1452.                     then    : ok
  1453.                     else    zenity --info --title="Required for translation" --text="Please install$text\nfor translation"
  1454.                         lang="en"
  1455.                 fi
  1456.                 ;;
  1457.         esac
  1458.         export lang service
  1459.     else    lang=`zenity --list --height=520 --title="Language" --text="Choose language for all text" --radiolist --column="Select" --column="Language" $langlist` || exit 0
  1460.         lang=`echo $lang | sed 's/=.*//'`
  1461.         echo "lang=$lang" > ~/.config/avconvert/avconvert.lang
  1462.         export lang
  1463.         if [ "$lang" = "en" ]
  1464.             then    online=n
  1465.             else    text="Please install the package(s):\n"
  1466.                 case $curlok$linksok in
  1467.                     *n*)    zenity --info --title="Packages needed" --text "$text\n\nfor the translation feature.\nProceeding in English until installed."
  1468.                         online=n ;;
  1469.                 esac
  1470.  
  1471.                 #service=`zenity --list --title="Service" --text="Which service should be used" --radiolist --column="Select" --column="Language" TRUE google FALSE babel` || exit 0
  1472.                 #echo "service=$service" >> ~/.config/avconvert/avconvert.lang
  1473.                 #export lang service
  1474.                 export lang
  1475.         fi
  1476.         title=`mytranslate "Language Preference"`
  1477.         text=`mytranslate "When the internet is available, this script will
  1478. send text through the translation service.
  1479.  
  1480. If there is no internet connection, everything will be in English.
  1481. If the internet is slow, translations may not work well.  Please
  1482. tell me if there are problems so that I can improve this.
  1483.  
  1484. Your language is stored in ~/.config/avconvert/avconvert.lang
  1485. Remove or edit that file to change language."`
  1486.         [ "$lang" = "en" ] && text="You have chosen to display text in English.
  1487.  
  1488. You can change the language by editing or removing
  1489. ~/.config/avconvert/avconvert.lang"
  1490.         zenity --info --title="$title" --text="$text"
  1491. fi
  1492. fi
  1493.  
  1494. # if language is EN, we do not need to translate
  1495. case $lang in
  1496.     en) online=n ;;
  1497. esac
  1498.  
  1499. if grep "trash=" ~/.config/avconvert/avconvert >/dev/null 2>&1
  1500.     then    : UP TO DATE
  1501.     else    rm -f ~/.config/avconvert/[Aa]vconvert
  1502. fi
  1503.  
  1504. if test -s ~/.config/avconvert/avconvert
  1505.     then    : defaults already set
  1506.     else    echo -e "destdir=n\nautoloop=n\nzenityfix=n\nquiet=n\ntrash=n" > ~/.config/avconvert/avconvert
  1507.         title=`mytranslate "Set Defaults"`
  1508.         text=`mytranslate "This is your first run of avconvert.
  1509.  
  1510. The file ~/.config/avconvert/avconvert can save some default behaviors.  You can create one now by selecting from the options below,
  1511. and you can change it at any time by editing it directly or by removing it, which will trigger this dialogue to reappear.
  1512.  
  1513. Read each option below and choose your prefrence followed by OK, or click Cancel to defer this until later."`
  1514.         p1=`mytranslate "Always pop up to ask for a destination into which to save converted files"`
  1515.         p2=`mytranslate "When multiple files are selected, process each one in the same way"`
  1516.         p3=`mytranslate "Check zenity defaults and offer to fix them so popups are OVER rather than UNDER"`
  1517.         p4=`mytranslate "Quiet mode - use 'notification' instead of 'info' to show some conversion results"`
  1518.         p5=`mytranslate "VERY Quiet mode - NEVER show progress indicators, work silently"`
  1519.         p6=`mytranslate "After conversions, offer to move originals to ~/Desktop/avconvert-Trash"`
  1520.         behavior=`zenity --list --height=400 --title="$title" --text="$text" --checklist --column="" --column="Description" TRUE "$p1 (destdir=y)" TRUE "$p2 (autoloop=y)" TRUE "$p3 (zenityfix=y)" TRUE "$p4 (quiet=y)" FALSE "$p5 (veryquiet=y)" FALSE "$p6 (trash=n)"` || exit 0
  1521. echo "$behavior" | tr '|' '\012' | sed -e 's/^.*(//' -e 's/.$//' | grep "=" >> ~/.config/avconvert/avconvert
  1522.  
  1523.         title=`mytranslate "Set image default"`
  1524.         text=`mytranslate "When converting images, the system can set a default for the type of file to be converted to.
  1525.  
  1526. choose one of the following"`
  1527.         col=`mytranslate "Default image extension"`
  1528.         i1=`mytranslate "No default should be pre-selected"`
  1529.         i2=`mytranslate "Create an image of the same type as the source"`
  1530.         i3=`mytranslate "Default image type should be"`
  1531.         imageext=none
  1532.         imageext=`zenity --list --height=370 --title="$title" --text="$text" --radiolist --column="" --column="$col" TRUE "$i1 (imageext=none)" FALSE "$i2 (imageext=same)" FALSE "$i3 gif (imageext=gif)" FALSE "$i3 jpg (imageext=jpg)" FALSE "$i3 ico (imageext=ico)" FALSE "$i3 pdf (imageext=pdf)" FALSE "$i3 png (imageext=png)" FALSE "$i3 tif (imageext=tif)"` || exit 0
  1533.  
  1534. echo "$imageext" | tr '|' '\012' | sed -e 's/^.*(//' -e 's/.$//' >> ~/.config/avconvert/avconvert
  1535. . ~/.config/avconvert/avconvert
  1536.  
  1537. echo "# Defaults file for avconvert, created `date`
  1538. destdir=$destdir
  1539. autoloop=$autoloop
  1540. zenityfix=$zenityfix
  1541. quiet=$quiet
  1542. veryquiet=$veryquiet
  1543. trash=$trash
  1544. imageext=$imageext
  1545. " > ~/.config/avconvert/avconvert
  1546. fi
  1547.  
  1548. # set everything to 'defaults' regardless of preferences file
  1549. # in case there's no file, or some are missing
  1550. destdir=y; autoloop=x; zenityfix=y; imageext=none; quiet=n trash=n
  1551.  
  1552. # now "dot" the file if it is there
  1553. # due to google translator, be sure it is linked to Avconvert as well
  1554. if test -s ~/.config/avconvert/avconvert
  1555.     then    . ~/.config/avconvert/avconvert
  1556.     if cmp ~/.config/avconvert/avconvert ~/.config/avconvert/Avconvert >/dev/null 2>&1
  1557.         then    : alldone
  1558.         else    rm -f ~/.config/avconvert/Avconvert
  1559.             ln ~/.config/avconvert/avconvert ~/.config/avconvert/Avconvert
  1560.     fi
  1561. fi
  1562.  
  1563. # here we go.....
  1564.  
  1565. # let's get some common translated phrases in advance first:
  1566. # export them all!!
  1567. s1=`mytranslate "Select"`; export s1
  1568.  
  1569.  
  1570. while true
  1571. do
  1572.     case $1 in
  1573.         --no-destdir)   destdir=n; shift ;;
  1574.         --no-loop)  autoloop=n; shift ;;
  1575.         --no-zenityfix) zenityfix=n; shift ;;
  1576.         *)      break ;;
  1577.     esac
  1578. done
  1579.  
  1580. case $1 in
  1581.     '') which avconvert >/dev/null 2>&1 || exec zenity --warning --text="Attention needed:\n\nNo source files were selected.  That is OK,\nbut for this to work properly, the avconvert program must be copied or linked\nto a location that is in your PATH.\nOr you may change your PATH to include the location of this script."
  1582.         title=`mytranslate "Select source files. They should all of same type (all images, or all videos, etc)"`
  1583.         files=`zenity --title="$title" --file-selection --multiple --separator="|"` || exit 0
  1584.         files=`echo "$files" | sed -e 's/^/"/' -e 's/$/"/' -e 's/|/" "/g'`
  1585.         echo "avconvert $* $files" > /tmp/avconvert.rerun
  1586.         sh /tmp/avconvert.rerun &
  1587.         rm -f /tmp/avconvert.rerun
  1588.         exit 0
  1589.         ;;
  1590. esac
  1591.  
  1592. # if this is not a "fixable" zenity, do not try to fix it
  1593. # (at least) ubuntu 9.10 lacks the configuration file
  1594. if test -s /usr/share/zenity/zenity.glade
  1595.     then    : good
  1596.     else    zenityfix=n
  1597. fi
  1598. case $zenityfix in
  1599.     n)  ;;
  1600.     *)
  1601. if test -s /usr/share/zenity/zenity.glade.orig
  1602.     then    : already done
  1603.     else    cat << ZenityFix > /tmp/zenity.fix
  1604. # zenity.fix
  1605. # written by
  1606. # marc brumlik, tailored softare inc
  1607. # distributed with
  1608. # avconvert, posted on gnome-look, to correct the behavior of zenity
  1609. # The driectory containing zenity.glade is:
  1610. DIR=/usr/share/zenity
  1611.  
  1612. if test -s \$DIR/zenity.glade
  1613.     then    cd \$DIR
  1614.     else    echo -e "The file /usr/share/zenity/zenity.glade\ncan not be found!\n\nPlease modify the script /tmp/zenity.fix with the correct\nlocation of the file 'zenity.glade'.\nThen execute it directly from the command line.\n\nExiting now...\n\nPress Return \c"; read fred; exit 0
  1615. fi
  1616. echo -e "\nModifying the file \$DIR/zenity.glade\n\nA safe copy of your file is being saved as zenity.glade.orig"
  1617. if test -s zenity.glade.orig
  1618.     then    echo "It appears that this has been run before."
  1619.         echo "Doing it again..."
  1620.     else    cp zenity.glade zenity.glade.orig
  1621. fi
  1622. cat zenity.glade.orig | sed '/focus_on_map/s/False/True/' > zenity.glade
  1623. cp zenity.glade zenity.glade.progress-over
  1624. progline=\`grep -n "zenity_progress_dialog" zenity.glade | sed 's/:.*//'\`
  1625. focusline=\`sed -n "\$progline,\$"p < zenity.glade | grep -n focus_on_map | \
  1626.     head -1 | sed 's/:.*//'\`
  1627. targetline=\`echo -e "\$progline + \$focusline - 1 \n quit" | bc\`
  1628. sed "\$targetline s/True/False/" < zenity.glade.progress-over > zenity.glade.progress-under
  1629. chmod 666 /usr/share/zenity/zenity.glade
  1630. echo -e "/tmp/zenity.fix now exiting\n\nPress Return \c"; read fred
  1631. ZenityFix
  1632.         chmod 755 /tmp/zenity.fix
  1633.         if which xterm >/dev/null 2>&1
  1634.             then    message=`mytranslate "Click OK if you would like this to be run for you right now, or if you prefer, click Cancel to prevent avconvert from doing this.  If you Cancel, avconvert will create a script /tmp/zenity.fix which you can run as \'root\' manually.  This will modify your Zenity defauts so its windows appear on top of any others."`
  1635.             else    message=`mytranslate "This fix can not be auto-run for you because you do not have \'xterm\' installed.  Click Cancel and execute /tmp/zenity.fix as \'root\'. This will modify Zenity defaults so its windows appear on top of any others."`
  1636.         fi
  1637.         title=`mytranslate "Zenity defaults"`
  1638.         text1=`mytranslate "The Linux utility that avconvert uses to pop up dialogs is called Zenity.  In some releases, these dialogs pop up underneath existing windows.  This makes reading the output and responding to prompts from avconvert very inconvenient.
  1639.  
  1640. Your installed Zenity version is one which behaves this way.  For your convenience, a small program named 'zenity.fix' has just been written in your /tmp directory."`
  1641.         text2=`mytranslate "NOTE: this window and some others can be permanently silenced either with command line options or by creating a ~/.config/avconvert/avconvert config file.  Read the end of the avconvert script for details.
  1642. Also, this window will not reappear once the zenity defaults are changed OR as long as the /tmp/zenity.fix file exists."`
  1643.         zenity --question --title="$title" --text="$text1\n\n$message\n\n$text2" && xterm -e ' echo -e "To fix zenity requires Root \c"; su -c /tmp/zenity.fix '
  1644. fi
  1645.     ;;
  1646. esac
  1647.  
  1648. case $quiet in
  1649.     y)  cp /usr/share/zenity/zenity.glade.progress-under /usr/share/zenity/zenity.glade 2>/dev/null ;;
  1650.     n)  cp /usr/share/zenity/zenity.glade.progress-over /usr/share/zenity/zenity.glade 2>/dev/null ;;
  1651. esac
  1652.  
  1653. rm -f /tmp/avconvert.env /tmp/avffmpeg.env
  1654.  
  1655. for each in "$@"
  1656. do
  1657.     allargs=`echo -e "$allargs\n$each"`
  1658. done
  1659. allargs=`echo -e "$allargs" | grep -v "^$"`
  1660. chkavi=`echo -e "$allargs" | sed 's/^.*\.//' | egrep "avi$|AVI$" | sort | \
  1661.     uniq | wc | awk '{print $1}'`
  1662. case $chkavi in
  1663.     1)  qtyavi=`echo -e "$allargs" | wc | awk '{print $1}'`
  1664.         case $qtyavi in
  1665.             [01])   ;;
  1666.             *)  multiavi=y ;;
  1667.         esac ;;
  1668. esac
  1669. case $multiavi in
  1670.     y)  title=`mytranslate "Merge AVI Files"`
  1671.         text1=`mytranslate "You have selected multiple AVI files."`
  1672.         text2=`mytranslate "Press CANCEL to proceed with normal conversions."`
  1673.         text3=`mytranslate "If these files all have the same audio and video settings"`
  1674.         text4=`mytranslate "you may click OK to combine them to a single AVI file."`
  1675.         text5=`mytranslate "The final file will be named MergedAVI.avi"`
  1676.         if zenity --question --title="$title" --text="$text1\n\n$text2\n\n$text3\n$text4\n\n$text5"
  1677.             then    if ! which avimerge >/dev/null 2>&1
  1678.                     then    zenity --warning --title=HELP --text="The program \"avimerge\" is needed but was was not found.\nThis should be provided by the \"transcode\" package.\nPlease install that and try again."
  1679.                     exit 0
  1680.                 fi
  1681.  
  1682.                 dest=`echo -e "$allargs"| head -1`
  1683.                 dest=`dirname "$dest"`
  1684.                 oldsize=`du -b -c "$allargs" | grep total | awk '{print $1}'`
  1685.                 avwatcher "$dest/MergedAVI.avi" "avimerge" "$oldsize" $$ &
  1686.                 avimerge -c -o "$dest/MergedAVI.avi" -i "$@" >/tmp/avimerge.$$ 2>&1
  1687.                 exit 0
  1688.         fi ;;
  1689. esac
  1690.  
  1691. chkvob=`echo -e "$allargs" | sed 's/^.*\.//' | egrep "vob$|VOB$" | sort | \
  1692.     uniq | wc | awk '{print $1}'`
  1693. case $chkvob in
  1694.     1)  qtyvob=`echo -e "$allargs" | wc | awk '{print $1}'`
  1695.         case $qtyvob in
  1696.             [0])    ;;
  1697.             *)  multivob=y ;;
  1698.         esac ;;
  1699. esac
  1700. case $multivob in
  1701.     y)  title=`mytranslate "Merge VOB Files"`
  1702.         text1=`mytranslate "You have selected one or more VOB files."`
  1703.         text2=`mytranslate "Press CANCEL to proceed with normal conversions."`
  1704.         text3=`mytranslate "You may click OK to combine them to a single AVI file"`
  1705.         text4=`mytranslate "with all options set to rip from a DVD."`
  1706.         text5=`mytranslate "The final file will be named $HOME/Desktop/MergedVOB.avi"`
  1707.         text6=`mytranslate "This process will first create each VOB to an AVI and"`
  1708.         text7=`mytranslate "then merge them to a single AVI. Each converted VOB will"`
  1709.         text8=`mytranslate "also be on the Desktop, for reference and debugging"`
  1710.         if zenity --question --title="$title" --text="$text1\n\n$text2\n\n$text3\n$text4\n\n$text5\n\n$text6\n$text7\n$text8"
  1711.             then   
  1712.                 dest=`echo -e "$allargs"| head -1`
  1713.                 destdir=`dirname "$dest"`
  1714.                 ffident=`ffmpeg -i "$dest" 2>&1 | \
  1715.                     egrep "duration:|Input #|Stream #"`
  1716.                 ab=`echo "$ffident" | grep Audio | tr ',' '\012' | grep "kb/s" | awk '{print $1}'`
  1717.                 count=1
  1718.                 until [ -z "$1" ]
  1719.                 do
  1720.                     count=`echo $count | awk '{printf "%03.0f\n", $1}'`
  1721.                     oldsize=`du -b -c "$1" | grep total | awk '{print $1}'`
  1722.                     avwatcher "$HOME/Desktop/vob2avi.$$.$count.avi" "ffmpeg" "$oldsize" $$ &
  1723.                     echo -e "$ffident" > /tmp/vob2avi.$$.$count.log
  1724.                     echo -e "\nEXECUTING:\nffmpeg -i "$1" -f avi -vcodec mpeg4 -b 4096k -g 300 -bf 2 -ac 2 -acodec libmp3lame -ab $ab"k" $HOME/Desktop/vob2avi.$$.$count.avi\n\n" > /tmp/vob2avi.$$.$count.log
  1725.                     ### ffmpeg -i "$1" -f avi -vcodec mpeg4 -b 800k -g 300 -bf 2 -ac 2 -acodec libmp3lame -ab $ab"k" -ar 44100 $HOME/Desktop/vob2avi.$$.$count.avi >> /tmp/vob2avi.$$.$count.log 2>&1
  1726.                     ffmpeg -i "$1" -f avi -vcodec mpeg4 -b 4096k -r 29.97 -g 300 -bf 2 -ac 2 -acodec libmp3lame -ab $ab"k" -ar 44100 $HOME/Desktop/vob2avi.$$.$count.avi >> /tmp/vob2avi.$$.$count.log 2>&1
  1727.                     # ffmpeg sometimes gets the duration or speed wrong on a VOB file
  1728.                     # AND sometimes that misinformation gets into the converted AVI file
  1729.                     # but it seems that copying the AVI fixes this, generating an index to use is even better
  1730.                     ffmpeg -i $HOME/Desktop/vob2avi.$$.$count.avi -acodec copy -vcodec copy $HOME/Desktop/vob2avi.$$.$count.copy.avi > /tmp/vob2avi.$$.$count.copy.log 2>&1
  1731.                     aviindex -o $HOME/Desktop/vob2avi.$$.$count.copy.idx -i $HOME/Desktop/vob2avi.$$.$count.copy.avi -x > /tmp/vob2avi.$$.$count.idx.log 2>&1
  1732.                     avimerge -c -o $HOME/Desktop/vob2avi.$$.$count.FIXED.avi -i $HOME/Desktop/vob2avi.$$.$count.copy.avi -x $HOME/Desktop/vob2avi.$$.$count.copy.idx > /tmp/vob2avi.$$.$count.fix.log 2>&1
  1733.                     count=`expr $count + 1`
  1734.                     shift
  1735.                 done
  1736.                 oldsize=`du -b -c "$HOME/Desktop/vob2avi.$$."*".avi" | grep total | awk '{print $1}'`
  1737.                 avwatcher "$HOME/Desktop/MergedAVI.$$.avi" "avimerge" "$oldsize" $$ &
  1738.                 echo -e "EXECUTING:\navimerge -c -o "$HOME/Desktop/MergedAVI.$$.avi" -i $HOME/Desktop/vob2avi.$$.*.FIXED.avi\n\n" > /tmp/MergedAVI.$$.log 2>&1
  1739.                 avimerge -c -o "$HOME/Desktop/MergedAVI.$$.avi" -i $HOME/Desktop/vob2avi.$$.*.FIXED.avi >> /tmp/MergedAVI.$$.log 2>&1
  1740.                 ffmpeg -i $HOME/Desktop/MergedAVI.$$.avi -acodec copy -vcodec copy $HOME/Desktop/MergedAVI.FINAL.avi > /tmp/MergedAVI.$$.FINAL.log 2>&1
  1741.  
  1742.             exit 0
  1743.         fi ;;
  1744. esac
  1745.  
  1746. until [ -z "$1" ]
  1747. do
  1748.  
  1749. fileargs="$fileargs
  1750. $1"
  1751.  
  1752. echo "$1" > /tmp/files
  1753.  
  1754. targetpath=$1; shift
  1755. target=`basename "$targetpath"`
  1756. # had used this due to an earlier bug, but dirname is better
  1757. # dir=`echo "$targetpath" | sed "s/\/\$target//"`
  1758. dir=`dirname "$targetpath"`
  1759.  
  1760. case "$dir" in
  1761.     */*)    ;;
  1762.     *)  dir=`pwd` ;;
  1763. esac
  1764.  
  1765. test -d "$dir" && cd "$dir"
  1766.  
  1767. dir=`pwd`
  1768.  
  1769. if test -f "$target"
  1770.     then    if test -s "$target"
  1771.             then    : OK
  1772.             else    text=`mytranslate "Source file is EMPTY!"`
  1773.                 exec zenity --warning --text="$target\n\n$text"
  1774.         fi
  1775.     else    text=`mytranslate "Source file NOT FOUND!"`
  1776.         exec zenity --warning --text="$target\n\n$text"
  1777. fi
  1778.  
  1779. # echo destdir: $destdir
  1780. # echo target: $target
  1781. # echo outdir: $outdir
  1782.  
  1783. case $outdir in
  1784.     '')
  1785.         case $destdir in
  1786.             n)  outdir=`pwd` ;;
  1787.         esac
  1788.  
  1789.         case "$outdir" in
  1790.             '') outdir=`dirname "$target"` ;;
  1791.         esac
  1792.  
  1793.         case $destdir in
  1794.             y)  outdir="" ;;
  1795.         esac
  1796.  
  1797.         if test -z "$outdir"
  1798.             then    cwd=`pwd`
  1799.         title=`mytranslate "Destination folder (CANCEL will write to SOURCE DIRECTORY)"`
  1800.                 outdir=`zenity --file-selection --directory="$dir" --title="$title"`
  1801.                 if test -z "$outdir"
  1802.                     then    outdir=$cwd
  1803.                 fi
  1804.         fi
  1805.     ;;
  1806. esac
  1807.  
  1808. case "$target" in
  1809.     *.*)    ;;
  1810.     *)  zenity --question --title="What to do?" --text="It would be nice if \"$target\" had an extension...\nRenaming this file is recommended!\n\nTo have avconvert proceed anyway, press OK.\nTo exit now so you can rename the file, press Cancel" || exit 0 ;;
  1811. esac
  1812.  
  1813. type=`file "$target" | tr '[A-Z]' '[a-z]' | sed 's/^.*://'`
  1814. mtype=`file -i "$target" | tr '[A-Z]' '[a-z]' | sed 's/^.*://'`
  1815. show=`file "$target" | awk -F: '{print $2}'`
  1816. case "$target" in
  1817.     *.rm|*.rmvb|*.???|*.????)   ext=`echo "$target" | sed 's/^.*\.//'` ;;
  1818.     *)      ext="" ;;
  1819. esac
  1820. lcext=`echo "$ext" | tr '[A-Z]' '[a-z]'`
  1821. # imagemagick identify does not properly execute mencoder on *avi files
  1822. # and instead generated a png image for every frame.
  1823. # until fixed, this like should be commented out.
  1824. ## ident=`identify "$target" 2>/dev/null | sed "s/^.*$ext //"`
  1825. # ffmpeg provides lots of info anyway
  1826. ffident=`ffmpeg -i "$target" 2>&1 | egrep "Duration:|Input #|Stream #"`
  1827. if echo "$ffident" | grep Unable >/dev/null
  1828.     then    ffident=""
  1829. fi
  1830. echo "$ffident" | grep audio >/dev/null && fftype=audio
  1831. echo "$ffident" | grep video >/dev/null && fftype=video
  1832. [ "$fftype" = "video" ] && echo "$ffident" | grep "bitrate: N/A" && fftype=image
  1833. [ "$fftype" = "image" ] && imident=`echo "$ffident" | grep Video | \
  1834.     awk -F, '{print $1, $2, $3, $4}' | sed 's/^.*://'`
  1835.  
  1836. #   based on source file, what might the destinations be?
  1837. #   what application will we use to create them?
  1838. #   the case wildcards are things we know how to convert FROM
  1839. #   "prog" is the actual conversion program
  1840. #   "out" is a list of formats we can choose to convert TO
  1841. it=""; av=""    # for "image/text" and "audio/video"
  1842. # strings from file, and extensions, which imply input file type
  1843.  
  1844. #echo DETECTED: "$fftype$type$mtype$lcext"
  1845. caseme=`echo "$fftype$type$mtype$lcext" | sed 's/;//g'`
  1846. #echo "CASEME: $caseme"
  1847. case "$caseme" in
  1848.     *bin)
  1849.         base=`echo "$target" | sed 's/.bin$//'`
  1850.         if test -s "$base".cue
  1851.             then    prog=iso; it=bchunk
  1852.         fi ;;
  1853.     *cue)
  1854.         base=`echo "$target" | sed 's/.cue$//'`
  1855.         if test -s "$base".bin
  1856.             then    prog=iso; it=bchunk
  1857.         fi ;;
  1858.     *daa)
  1859.         prog=iso; it=daa2iso ;;
  1860.     *nrg)
  1861.         prog=iso; it=nrg2iso ;;
  1862.     *b?i)
  1863.         prog=iso; it=b5i2iso ;;
  1864.     *cdi)
  1865.         prog=iso; it=csi2osi ;;
  1866.     *mdf)
  1867.         prog=iso; it=mdf2iso ;;
  1868.     *pdi)
  1869.         prog=iso; it=pdi2iso ;;
  1870.     *ccd)
  1871.         prog=iso; it=ccd2iso ;;
  1872.     *uif)
  1873.         prog=iso; it=uif2iso ;;
  1874.     *text/plain*|*ascii*)
  1875.         # recognized plain text formats, output as text OR image
  1876.         prog="text"; it="text" ;;
  1877.     *SVG*Scalable*|*svg)
  1878.         # separating this one out - might offer "autotrace"
  1879.         prog="convert"; it="image" ;;
  1880.     *image*|*pdf|*ico|*orf)
  1881.         # recognized image formats, output will also be an image
  1882.         prog="convert"; it="image" ;;
  1883.     *video*|*3g2|*3gp|*ogv|*mkv|*mpg|*wmv|*asf|*mp4|*ogg|*rm|*rmvb)
  1884.         # recognized multimedia formats, output to multimedia OR audio
  1885.         prog="ffmpeg"; av="video" ;;
  1886.     *audio*|*mp3|*wav|*m4a|*wma)
  1887.         # recognized audio-only formats, output to audio
  1888.         prog="ffmpeg"; av="audio" ;;
  1889.     *msword*)
  1890.         # micro$oft office word document, v2 / v6 or newer
  1891.         prog=text; it="doc" ;;
  1892.     text/html)
  1893.         prog=text; it="html" ;;
  1894.     *text/rtf*)
  1895.         prog=text; it="rtf" ;;
  1896.     *Unicode*)
  1897.         prog=text; it="uni" ;;
  1898.     *opendocument*|*openoffice*)
  1899.         prog=text; it="odt" ;;
  1900.     *openoffice*)
  1901.         prog=text; it="oo" ;;
  1902.     *)  prog=`zenity --list --height=520 --title="Unknown!" --text="Not sure what to do with this file!\n\nTarget file: \"$target\"\n\nInformation found:\n$type\n$mtype\n$ffident$ident\n\n\nBut, it is likely that though this file type was not\nexplicitly recognized by avconvert, it can be handled\nby convert (Images) or ffmpeg (Audio/Video).\n\nIf you would like to TRY, choose the utility you feel\nwould be appropriate and avconvert will do its best.\n\n" --radiolist --column "Select" --column "Utility to use" FALSE convert FALSE ffmpeg` || exit 0
  1903.         case $prog in
  1904.             ffmpeg)     av=`zenity --list --title="Select" --text="Tell ffmpeg whether this file is:" --radiolist --column "Select" --column "Content type" FALSE audio FALSE video` || exit 0 ;;
  1905.             convert)    it=image ;;
  1906.         esac
  1907.         ;;
  1908.        
  1909. esac
  1910.  
  1911. # export everything we know so far and launch the appropriate function quality
  1912.  
  1913. case $prog in
  1914.     ffmpeg|convert) audiovideo=y ;;
  1915.     *)      audiovideo=n ;;
  1916. esac
  1917. case "$1$audiovideo$autoloop" in
  1918.     ?*yx)   autoloop=n
  1919.         zenity --question --title="Auto-Loop though Source Files??" --text="You have selected multiple source files.  Avconvert will loop through them and convert them all.\n\n*IF* you know that all source files are of the same type\n   (images, audio, etc)\nand *IF* you want them all of them converted the same way\n   (images->1024/jpg, audio->128bit/mp3, etc)\n*THEN* click OK\n\n     But...\n\n*IF* they the source files are of different types\n   (not ALL images or not ALL videos)\nor *IF* you want per-file control of how each source is handled\n*THEN* click Cancel\n\nOK will auto-loop, Cancel will treat each file separately." && autoloop=y
  1920.         ;;
  1921. esac
  1922.  
  1923. today=`date +%Y%m%d`
  1924. if grep "$today" ~/.config/avconvert/versioncheck >/dev/null 2>&1
  1925.     then    : already checked
  1926.     else    echo "$today" > ~/.config/avconvert/versioncheck
  1927.         versioncheck=`wget -q --timeout=3 --tries=1 \
  1928.         http://www.gtk-apps.org/CONTENT/content-files/92533-avconvert.tar.gz -O - | \
  1929.         gunzip 2>/dev/null | grep -a version= | head -1 | \
  1930.         sed -e 's/"$//' -e 's/^.*"//'`
  1931.         case "$versioncheck" in
  1932.             [0-9].[0-9][0-9]*)
  1933.                 case "$versioncheck" in
  1934.                     "$version") ;;
  1935.                     *)  zenity --info --height=500 --title="A newer version 'avconvert' is available for download" --text="You are running version\n\t$version\n\nAvailable for download\n\t$versioncheck"
  1936.                         ;;
  1937.                 esac
  1938.         esac
  1939. fi
  1940.  
  1941. export dir target ext type mtype show ident ffident it av prog autoloop base
  1942. myav$prog
  1943.  
  1944. # this 'done' is for looping through multiple selected input files
  1945. done
  1946.  
  1947. case $overwrite in
  1948.     YES)    ;;
  1949.     *)  case $trash in
  1950.             y)  totrash=`zenity --list --title="Send to Trash?" --height=210 --text="You can move the source file(s) to\n$HOME/Desktop/avconvert-Trash" --radiolist --column "Select" --column "Trashcan the source files?" TRUE NO FALSE YES` || exit 0
  1951.             case $totrash in
  1952.                 NO) ;;
  1953.                 YES)    mkdir -p ~/Desktop/avconvert-Trash/$$ 2>&1
  1954.                     echo "$fileargs" | while read each
  1955.                     do mv "$each" ~/Desktop/avconvert-Trash/$$; done
  1956.                     ;;
  1957.             esac
  1958.             ;;
  1959.         esac
  1960.         ;;
  1961. esac
  1962.  
  1963. exit 0
  1964.  
  1965. #####
  1966. # READ ME
  1967. #####
  1968. avconvert
  1969.  
  1970. marc brumlik, tailored software inc, Thu Nov 13 18:50:35 CST 2008
  1971. tsi-inc@comcast.net
  1972.  
  1973. #######
  1974. # IF YOU READ NO FURTHER THAN THIS, please note that when using
  1975. # "kcolorchooser" in text-to-image, there is a delay after choosing your
  1976. # color before the next window from avconvert comes up.  I have no idea why
  1977. # kcolorchooser is behaving this way.
  1978. #######
  1979.  
  1980. This project started out because I needed to create jpg files of numerous
  1981. resolutions from a number of source files and wanted an automatic way to
  1982. generate them more quickly.  Then my daughter asked for a way to convert
  1983. "unplugged" YouTube files into formats her friends could view.
  1984. Combining these two short scripts is where avconvert began.
  1985.  
  1986. Upon completion of each use of avconvert, a file for debugging purposes
  1987. is written to, with lots of output showing exactly what the script
  1988. has done.  This can help a lot if you are getting unexpected results.
  1989. The file name is derived from the conversion, followed by "dot" PID.  In the
  1990. case of text->image and if intermediary files were generated, there will be
  1991. another "dot" and a sequence number.  These files will be named:
  1992.     /tmp/convert.$$
  1993.     /tmp/convert.$$.$count
  1994.     /tmp/ffmpeg.$$
  1995.     /tmp/avconvert.debug
  1996.     /tmp/txt2tags.$$
  1997.     /tmp/antiword.$$
  1998. and more may be added in the future.
  1999.  
  2000. You can create a startup file ~/.config/avconvert/avconvert to contain preferences.
  2001. Currently there are three items that can be specified there.
  2002. A ~/.config/avconvert/avconvert which specifies the defaults for these would contain this:
  2003.     destdir=y
  2004.     autoloop=y
  2005.     zenityfix=y
  2006. The line "destdir" specifies whether or not there should be a popup prompting
  2007. for a destination directory for converted files.  The line "autoloop" determines
  2008. whether, if multiple files are selected, you will be prompted and asked if you
  2009. want to treat them all the same way.  Y means prompt, N means always treat the
  2010. files individually.  THe line "zenityfix" has to do with the prompt which offers
  2011. to fix zenity popups so they appear above (instead of below) other items on the
  2012. desktop.  Y means detect "popunder" and offer to fix it, N means ignore current
  2013. zentiy configuration.
  2014.  
  2015. Things you might want to customize (starting from the top of the script):
  2016.  
  2017. Too much verbage that gives information about source files?
  2018.     comment out the "ident=" and "ffident=" lines
  2019.  
  2020. Too much window activity and want to eliminate "progress" window?
  2021.     uncomment the "exit 0" at the top of "myavconvert-watch"
  2022.  
  2023. To add more extensions to recognized file types for image/video/audio:
  2024.     the script uses two different forms of output from the
  2025.     "file" command plus the source file extension, to decide
  2026.     what type of file it has to work with.  "file" isn not
  2027.     always useful, which is why extensions are needed at all.
  2028.     find the comment "# strings from file".  Below that are
  2029.     four cases.  Each contains first something that may have
  2030.     been output by "file", followed by a set of extensions.
  2031.     Just follow the example to add more extensions.
  2032.  
  2033. To add more extensions to the default list of output file types:
  2034.     Find the comment line beginning "Show appropriate output".
  2035.     Below that are four entries - two for "convert" and two
  2036.     for "ffmpeg".  These are each divided into two more.
  2037.     Just add any extension you like into that space-separated
  2038.     list (do NOT remove the leading space in the list).
  2039.  
  2040. To prevent a "default" output type for images:
  2041.     Currently the default output for images is the same as
  2042.     the source file type.  To prevent this, find the comment
  2043.     "This line sets default" and, on the line for "convert",
  2044.     and comment out the "out=" line below that.
  2045.  
  2046. To change the list of image resolutions presented:
  2047.     Find the comment "# set resolution here".  Add or
  2048.     remove any resolutions from the list.  Also, setting
  2049.     one or more from FALSE to TRUE will cause them to be
  2050.     checkboxed when the window appears.
  2051.  
  2052. To change the list of jpg quality settings:
  2053.     Find the comment "# if this is a jpeg".  Add or remove.
  2054.     You may also set the default by changing a FALSE to TRUE
  2055.  
  2056. To change the list of font sizes:
  2057.     Modify the line beginning "sizelist=".
  2058.  
  2059. To change bitrate choices for ffmpeg audio conversion:
  2060.     Find the comment line "# audio bitrate".
  2061.     To set a default bitrate, change one of the FALSE to TRUE.
  2062.  
  2063. To prevent the final window showing summary after completion:
  2064.     (Not recommeded, since this is ALSO where you might see
  2065.     diagnostic output after a failure)
  2066.     Find the comment " Show results summary" and comment out the
  2067.     "zenity" line that follows it.
  2068.  
  2069.  
  2070. COMMENTS WELCOME!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement