aMule Forum

Please login or register.

Login with username, password and session length
Advanced search  

News:

We're back! (IN POG FORM)

Author Topic: bash script to get the real extension of video file types  (Read 1484 times)

orair

  • Newbie
  • Karma: 0
  • Offline Offline
  • Posts: 2
bash script to get the real extension of video file types
« on: May 24, 2008, 05:52:48 PM »

I have wrote a bash script for myself that uses file to get the real extension of the video file types and changes the extension properly.
Furthermore the script uses the ffmpeg2theora to convert avi and mpeg files to ogg to reduces memmory space.
It could be put as an example on the wiki... ;)

#!/bin/bash
 #
 # get_real_content_and_try_to_compress.sh - converts the extension to reflect the real content and try to convert the videos to ogg to save memmory space
 # Used in conjuction with aMule's Event feature
 #
 # Call like this: getRealExtensionAndVideoCompressor.sh "%NAME" "%FILE"
 #
 #
 ShortFilename=$1
 FullPathFilename=$2
 {
 echo aMule completed this download:
 echo ------------------------------
 echo
 echo File: "$FullPathFilename"
 echo --------------------------------------------------------------------
 }

ASF_FILE_DESCRIPTION="Microsoft ASF"
AVI_FILE_DESCRIPTION="RIFF (little-endian) data, AVI"
MPEG_FILE_DESCRIPTION="MPEG sequence"
OGG_FILE_DESCRIPTION="Ogg data, Theora"

ASF=`file "$FullPathFilename" | grep -i -n $ASF_FILE_DESCRIPTION | wc -l`
AVI=`file "$FullPathFilename" | grep -i -n $AVI_FILE_DESCRIPTION | wc -l`
OGG=`file "$FullPathFilename" | grep -i -n $OGG_FILE_DESCRIPTION | wc -l`
MPEG=`file "$FullPathFilename" | grep -i -n MPEG_FILE_DESCRIPTION | wc -l`

if [ "$ASF" -gt "0" ]; then
   TYPE="asf"
   #Replaces the old extension by the real found
   NEW_FILENAME=`echo "$ShortFilename" | sed "s/\(\.\)\?\(ogg\|avi\|wmv\|mpg\|mpeg\|asf\)\?$/.$TYPE/i"`
   if [ "$ShortFilename" != "$NEW_FILENAME" ]; then
   mv -v "$ShortFilename" "$NEW_FILENAME"
   ShortFilename=$NEW_FILENAME
fi

if [ "$MPEG" -gt "0" ]; then
   TYPE="mpeg"
   #Replaces the old extension by the real found
   NEW_FILENAME=`echo "$ShortFilename" | sed "s/\(\.\)\?\(ogg\|avi\|wmv\|mpg\|mpeg\|asf\)\?$/.$TYPE/i"`
   if [ "$ShortFilename" != "$NEW_FILENAME" ]; then
   mv -v "$ShortFilename" "$NEW_FILENAME"
   ShortFilename=$NEW_FILENAME
fi
if [ "$AVI" -gt "0" ]; then
   TYPE="avi"
   #Replaces the old extension by the real found
   NEW_FILENAME=`echo "$ShortFilename" | sed "s/\(\.\)\?\(ogg\|avi\|wmv\|mpg\|mpeg\|asf\)\?$/.$TYPE/i"`
   if [ "$ShortFilename" != "$NEW_FILENAME" ]; then
   mv -v "$ShortFilename" "$NEW_FILENAME"
   ShortFilename=$NEW_FILENAME
fi
if [ "$OGG" -gt "0" ]; then
   TYPE="ogg"
   #Replaces the old extension by the real found
   NEW_FILENAME=`echo "$ShortFilename" | sed "s/\(\.\)\?\(ogg\|avi\|wmv\|mpg\|mpeg\|asf\)\?$/.$TYPE/i"`
   if [ "$ShortFilename" != "$NEW_FILENAME" ]; then
   mv -v "$ShortFilename" "$NEW_FILENAME"
   ShortFilename=$NEW_FILENAME
fi

#Trying to convert to ogg to save space. We will try to convert just the mpeg and avi files
if [ "$MPEG" -gt "0" -o "$AVI" -gt "0" ]; then
   TYPE="ogg"
   NEW_FILENAME=`echo "$ShortFilename" | sed "s/\(\.\)\?\(ogg\|avi\|wmv\|mpg\|mpeg\|asf\)\?$/.$TYPE/i"`
   CURRENT_SIZE=`ls -s "$ShortFilename"| cut -f1 --delim=" "`
   if [ -f "$NEW_FILENAME" ];
   then
      echo "Ogg correspondig file \'$NEW_FILENAME\'already exists... Then we will not try to convert to ogg..."
   else
      # Converting the video to ogg format
      echo "ffmpeg2theora $ShortFilename"
      ffmpeg2theora "$ShortFilename"
   fi
   OGG_FILE_SIZE=`ls -s "$NEW_FILENAME" | cut -f1 --delim=" "`
   DECREASED_SIZE=$((CURRENT_SIZE - OGG_FILE_SIZE))
   # The factor to decrease the size of the file to consider a significant reduce
   DECREASE_FACTOR=2
   SIGNIFICANT_REDUCE=`echo "$CURRENT_SIZE > ${DECREASE_FACTOR} * $OGG_FILE_SIZE" | bc -l`
   echo "OLD SIZE: $CURRENT_SIZE NEW SIZE: $OGG_FILE_SIZE DIFFERENCE SIZE: $DECREASED_SIZE SIGNIFICANT REDUCE? $SIGNIFICANT_REDUCE"
   if [ "$SIGNIFICANT_REDUCE" -eq "0" ];
   then
      echo "$NEW_FILENAME" >> to_exclude_files.txt
   else
      echo "$ShortFilename" >> to_exclude_files.txt
   fi
fi

Logged

Stu Redman

  • Administrator
  • Hero Member
  • *****
  • Karma: 214
  • Offline Offline
  • Posts: 3739
  • Engines screaming
Re: bash script to get the real extension of video file types
« Reply #1 on: May 25, 2008, 02:21:02 PM »

Now why did you post this as follow up in a 4 month old thread about KAD not working ?  ???
Logged
The image of mother goddess, lying dormant in the eyes of the dead, the sheaf of the corn is broken, end the harvest, throw the dead on the pyre -- Iron Maiden, Isle of Avalon