Converting video to ASCII.






ASCII images and video with AAlib.




Please accept cookies (at the bottom) to watch this video.
Please accept cookies (at the bottom) to watch this video.


  • Preparation.

    vid="Small_marble_machine.flv" ;

    Name of the input video is set to "Small_marble_machine.flv".

    Old files are cleaned up ("rm ...") and the name of the video is parsed by "sed".

  • Images are extracted from the video.

    ffmpeg -i $vid -r 30 -s vga -f image2 %7d.jpg

    Image names are seven digits long.

    Images are extracted from the video with ffmpeg. Ffmpeg is complaining about a program "image2", but it works for now.

  • Images are processed in the for loop.

    for line in $( ls *.jpg ) do ...

  • Asciiview is called with default parameters.

    asciiview $ascii.jpg &

    AAlib programs asciiview and aview work out the same way. But asciiview can process various kinds of input images, for example. jpg in this case.

    New X terminal window with ASCII image is opened. The settings are default, without the parameters. Asciiview is running in the background (&), so that the shell script continues.

  • Wmctrl program changes the size and position of X window.

    wmctrl -r "aa for X" -e 0,0,0,1024,768 ;

    Wmctrl can manipulate position and size of a X window.

    String "aa for X" is name of the X terminal window, written on its top. X window is resized to appropritate size.

  • Window with the name "aa for x" is found by using xwininfo.

    afx=$(xwininfo -tree -root | grep "aa for X" | sed '1,$ s/\( *\)\(.\)x\(.*\) "\(.*\)$/\2x\3/' ) ;

    Result is parsed using 'sed' stream editor to get an ID of the window. ID is saved into the variable "afx".

    The variable "afx" is needed in next step, when image is grabbed out of a X window.

  • Finaly! X terminal window is captured into a .xwd image.

    xwd -id $afx -out $ascii.xwd ;

    The X window is identified by its ID (-id).

  • Conversion of .xwd format to more general .png .

    convert $( echo $ascii".xwd" ) $( echo $ascii".png" ) ;

    The "convert" program is part of the ImageMagick tools.

  • Closing of a X window.

    kill -9 $( ps -C aview -o pid= ) ;

  • PNG image is copied to next 9 frames of a video.

    ... while (( $i < 10 )) ...

    "While" loop copies image nine times. So the FPS is changed from 25 or 30 fps to 2.5 or 3 fps. No more ASCII signs flashing!

  • Grande Finale: encoding images to a video!

    mencoder ...

    Mencoder stacks images and sound to an ASCII video!

  • Script usage.

    Script is to be saved to a text file. File must be executable, so do next:

    chmod +x script-file-name

    Type:

    ksh script-file-name

    And wait ...

  • And here is the code.

    Copy/paste it to a text file.


  • Script - version 1.

    5 or 6 fps. Simplified encoding without cropping and audio.

    Should work out of the box!


  # This script is free software, and can be used, modified and
  # distributed under the terms of the GPL License: http://www.gnu.org/licenses/gpl.html .

  vid="ascii-train.mp4" ;

  aud=$( echo $vid | sed '1,$ s/^\(.*\)\.\(.*\)$/\1/' ) ;
  rm *.tga *.svg *.JPG *.html *.jpg *.tga *.log *.jpeg *.xwd *.png ;

  ffmpeg -i $vid -r 25 -f image2 %7d.jpg ;

  for line in $( ls *.jpg )
  do
          #echo $line ;
          AaLib=$( echo $line | sed '1,$ s/\.jpg//' ) ;

          nipe=$( echo $AaLib | sed '1,$ s/......\(.\)/\1/' ) ;
          nipe1=$( echo $AaLib | sed '1,$ s/\(......\)./\1/' ) ;

          if (( $AaLib == "0000001" ))
          then
                  echo -n $line ;
                  AaLib=$( echo $line | sed '1,$ s/\.jpg//' ) ;
                  asciiview $AaLib.jpg &
                  sleep 1 ;
                  wmctrl -r "aa for X" -e 0,0,0,1664,936 ;
                  sleep 1 ;
                  afx="" ;
                  afx=$(xwininfo -tree -root | grep "aa for X" | sed '1,$ s/\( *\)\(.\)x\(.*\) "\(.*\)$/\2x\3/' ) ;
                  xwd -id $afx -out $AaLib.xwd ;
                  convert $( echo $AaLib".xwd" ) $( echo $AaLib".JPG" ) ;
                  kill -9 $( ps -C aview -o pid= ) > /dev/null 2>&1 ;

                  i=2 ;
                  while (( i < 5 ))
                  do
                          echo -n " "$i ;
                          cp $AaLib.JPG $nipe1$i.JPG ;
                          echo -n " ... $nipe1$i" ;
                          i=$(( $i + 1 )) ;
                  done
          fi

          if (( $nipe == "0" ))
          then
                  echo -n $line ;
                  AaLib=$( echo $line | sed '1,$ s/\.jpg//' ) ;
                  asciiview $AaLib.jpg &
                  sleep 1 ;
                  wmctrl -r "aa for X" -e 0,0,0,1664,936 ;
                  sleep 1 ;
                  afx="" ;
                  afx=$(xwininfo -tree -root | grep "aa for X" | sed '1,$ s/\( *\)\(.\)x\(.*\) "\(.*\)$/\2x\3/' ) ;
                  xwd -id $afx -out $AaLib.xwd ;
                  convert $( echo $AaLib".xwd" ) $( echo $AaLib".JPG" ) ;
                  kill -9 $( ps -C aview -o pid= ) > /dev/null 2>&1 ;

                  i=1 ;
                  while (( $i < 5 )) ;
                  do
                          echo -n " "$i ;
                          cp $AaLib.JPG $nipe1$i.JPG ;
                          echo -n " ... $nipe1$i" ;
                          i=$(( $i + 1 )) ;
                  done
                  echo "" ;
          fi
          
          if (( $nipe == "5" ))
          then
                  echo -n $line ;
                  AaLib=$( echo $line | sed '1,$ s/\.jpg//' ) ;
                  asciiview $AaLib.jpg &
                  sleep 1 ;
                  wmctrl -r "aa for X" -e 0,0,0,1664,936 ;
                  sleep 1 ;
                  afx="" ;
                  afx=$(xwininfo -tree -root | grep "aa for X" | sed '1,$ s/\( *\)\(.\)x\(.*\) "\(.*\)$/\2x\3/' ) ;
                  xwd -id $afx -out $AaLib.xwd ;
                  convert $( echo $AaLib".xwd" ) $( echo $AaLib".JPG" ) ;
                  kill -9 $( ps -C aview -o pid= ) > /dev/null 2>&1 ;

                  i=6 ;
                  while (( $i < 10 )) ;
                  do
                          echo -n " "$i ;
                          cp $AaLib.JPG $nipe1$i.JPG ;
                          echo -n " ... $nipe1$i" ;
                          i=$(( $i + 1 )) ;
                  done
                  echo "" ;
          fi
          
  done
    
  mencoder mf://*.JPG -mf fps=25:type=JPG -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:mv0:trell:v4mv:cbp:last_pred=3:predia=2:dia=2:vmax_b_frames=2:vb_strategy=1:precmp=2:cmp=2:subcmp=2:preme=2:qns=2 -oac copy -o "$aud".avi  

          


  • Script - version 2.

    2.5 or 3 fps. Encoding with cropping and audio.

    Final encoding will not work out of the box!

    Please adjust parameter '-vf crop=1024:768:8:13' and prepare .mp3 audio (must have name as video file)!


  # This script is free software, and can be used, modified and
  # distributed under the terms of the GPL License: http://www.gnu.org/licenses/gpl.html .

  vid="Small_marble_machine.flv" ;

  aud=$( echo $vid | sed '1,$ s/^\(.*\)\.\(.*\)$/\1/' ) ;
  rm *.tga *.svg *.png *.html *.jpg *.tga *.log *.jpeg *.xwd ;

  ffmpeg -i $vid -r 30 -s vga -f image2 %7d.jpg ;

  for line in $( ls *.jpg )
  do
          #echo $line ;
          ascii=$( echo $line | sed '1,$ s/\.jpg//' ) ;

          nipe=$( echo $ascii | sed '1,$ s/......\(.\)/\1/' ) ;
          nipe1=$( echo $ascii | sed '1,$ s/\(......\)./\1/' ) ;

          if (( $ascii == "0000001" ))
          then
                  echo -n $line ;
                  asciiview $ascii.jpg &
                  sleep 1 ;
                  wmctrl -r "aa for X" -e 0,0,0,1024,768 ;
                  sleep 1 ;
                  afx="" ;
                  afx=$(xwininfo -tree -root | grep "aa for X" | sed '1,$ s/\( *\)\(.\)x\(.*\) "\(.*\)$/\2x\3/' ) ;
                  xwd -id $afx -out $ascii.xwd ;
                  convert $( echo $ascii".xwd" ) $( echo $ascii".png" ) ;
                  kill -9 $( ps -C aview -o pid= ) ;

                  i=2 ;
                  while (( i < 10 ))
                  do
                          cp $ascii.png $nipe1$i.png ;
                          echo -n " ... $nipe1$i" ;
                          i=$(( $i + 1 )) ;
                  done
          fi

          if (( $nipe == "0" ))
          then
                  echo -n $line ;
                  asciiview $ascii.jpg &
                  sleep 1 ;
                  wmctrl -r "aa for X" -e 0,0,0,1024,768 ;
                  sleep 1 ;
                  afx="" ;
                  afx=$(xwininfo -tree -root | grep "aa for X" | sed '1,$ s/\( *\)\(.\)x\(.*\) "\(.*\)$/\2x\3/' ) ;
                  xwd -id $afx -out $ascii.xwd ;
                  convert $( echo $ascii".xwd" ) $( echo $ascii".png" ) ;
                  kill -9 $( ps -C aview -o pid= ) ;

                  i=1 ;
                  while (( $i < 10 )) ;
                  do
                          cp $ascii.png $nipe1$i.png ;
                          echo -n " ... $nipe1$i" ;
                          i=$(( $i + 1 )) ;
                  done
                  echo "" ;
          fi
  done

  killall aview ;

  mencoder -audiofile "$aud".mp3 mf://*.png -mf w=1024:h=768:fps=30:type=xvd -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:mv0:trell:v4mv:cbp:last_pred=3:predia=2:dia=2:vmax_b_frames=2:vb_strategy=1:precmp=2:cmp=2:subcmp=2:preme=2:qns=2 -oac copy -o "$aud".avi