Jump to content


Photo

videntify 1.0 BASH script for Mac OS X


  • Please log in to reply
31 replies to this topic

#1 callmenames

callmenames
  • Members
  • 1,426 posts

Posted 27 November 2005 - 06:41 PM

Just thought I would share this for anyone who wants to use it or take it farther. I wanted a quick, simple way of identifying the video types of tons of downloaded movies (yes, all porn) to sort out which would play in OS X and which would require Classic or Windows. I couldn't find anything that did what I wanted so I wrote this script as I went along identifying and sorting files...

It is currently functional and suits my own minimal needs although it could certainly be improved and enhanced. Run the script from the command line and supply the path to a folder of video files and it will rename the files based on their actual file type and video encoding. See the notes included as comments in the script for more info.

And Thank You to everyone who shares code, information and answers in the forum. :)

CODE
#!/bin/bash

# videntify 1.0 2005-11-20
# Released into the public domain by the author.
# Feel free to modify or swipe any part of this, no credit required.
# Please share your changes and additions too.

# WARNING
# This script -will- rename the video files, prepending file format and video type if known and may
# change the filename extension to reflect the actual file format (for instance an avi file titled
# video.divx with Indeo Video 5 compression would be changed to avi_iv50_video.avi)

# DESCRIPTION
# A Mac OS X BASH script to help identify the file format and video type of video files.
# Intended to be used on quantities of downloaded videos to quickly identify those that will and won't
# play using various OS X media players.

# USAGE
# videntify <path to folder of video files>

# RECOMMENDATIONS
#
# Use the freeware IconCleaner (gui) to strip custom icons from the files before running DupSeek (see next)
# http://www.ziksw.com/software/iconcleaner.html
#
# Use the freeware DupSeek (cli) to eliminate duplicate files (based on their content) before running this script
# http://www.beautylabs.net/software/dupseek.html
#
# Download mPlayer OSX, VLC (VideoLAN Client), Windows Media Player and RealPlayer and the DIVX and 3VIX CoDecs.
# They can be found using macupdate.com or the slow and annoying versiontracker.com
#
# Set the application for each video filename extension to open with for simplified double-clicking
# EXAMPLE: select a file ending in .asf in the Finder, Get Info on the file, in the "Open With" section
# select the program to open the file with such as "mPlayer OSX"


# NOTES
#
# Whenever possible, avoid using QuickTime Player, Windows Media Player or RealPlayer as they shamelessly
# allow the media files to open browser windows, display things that interfere with the video etc.
#
# Windows Media Player is needed for asf / WMV3 non-DRM playback
#
# asf/wmv files that use DRM (Digital Rights Management) will only playback in Windows - VirtualPC works (slowly),
# install Windows Media Player 10 under Windows within VirtualPC.
#
# Many of the RealVideo formats will play in VLC or mPlayer but not all.
#
# Many QuickTime formats will play in VLC or mPlayer, but again, not all.
#
# AVI, MOV, RM, ASF/WMV are file formats - they may contain data encoded with any of thousands of CoDecs
# and many will not play under OS X.
#
# Indeo Video 3 files play poorly in mPlayer, Indeo Video 3, 4, and 5 play well in the Classic QuickTime
# Player with the Indeo CoDecs installed. They also play in VirtualPC.
#
# The ToVid package includes a cli utility "IdVid" which calls the players themselves (mPlayer for instance) to
# report far more information about video files, it also takes much longer.
# http://tovid.sourceforge.net/


# IDEAS FOR ENHANCEMENTS TO THIS SCRIPT (please share your changes)
# Allow the script to work on a single file, list of files or a directory instead of just a directory
# If no files or folders are specified, prompt user
# Allow "move to" location(s) to be specified, perhaps into folders by file format and then subfolders by video CoDec
# Basic checks such as for write access
# If the "new" filename exists, add numbers counting up, then append the extension (at the moment it just skips the file)
# Optionally set permissions, owner / group and flags for each file processed
# When an uncompressed QuickTime movie is found to have nsav set to 00 00 00 01, unlock by changing to 00 00 00 00
# Decompress QuickTime Movie Resource data to check for nsav in compressed movies and unlock as well
# Option to disable QuickTime movie Flash, Sprite, Event tracks
# Option to split-out joined mpeg files (they play fine in mPlayer but in QT Player some only play the first clip within the file)
# Identify audio CoDecs as well




######## Functions (Subroutines)

strip_leading() {
A="${A:40}" ### stips 40 characters from the front of A, eliminates one entire line at a time from hexdump output
[ "${A:0:39}" = "0000 0000 0000 0000 0000 0000 0000 0000" ] && strip_leading
[ "${A:0:39}" = "ffff ffff ffff ffff ffff ffff ffff ffff" ] && strip_leading
}

declare_variables_for_this_file() {
# reset these variables for each item / file
filetype="none"
videotype="none"
avivids=""
avivids2=""
avividsascii=""
avivids2ascii=""
filename=`basename "${item}"`
filename_extension="${filename##*.}"
extension="${filename_extension}"
just_the_filename="${filename%.*}"
# squeeze duplicates of some chrs & cut some chrs out of filename
just_the_filename=`echo "${just_the_filename}" | tr -s " -_.()[]{}" | tr -d "#:!"`
filepath=`dirname "${item}"`
target=""
kind=""
playback_alert=""
nsav=""
A="" # Output from hexdump
wmvtype=""
}

remove_previous_leader_from_filename() {
# remove asf wmv mpg mov etc from front of just_the_filename in case the file has already been renamed
# current formats are
# avi_div3_
# avi_divxdx50_
just_the_filename="${just_the_filename/#asf_????_DRM_/}"
just_the_filename="${just_the_filename/#asf_????_/}"
just_the_filename="${just_the_filename/#avi_????????_/}"
just_the_filename="${just_the_filename/#avi_???????_/}"
just_the_filename="${just_the_filename/#avi_??????_/}"
just_the_filename="${just_the_filename/#avi_????_/}"
just_the_filename="${just_the_filename/#avi_???_/}"
just_the_filename="${just_the_filename/#mov_????_/}"
just_the_filename="${just_the_filename/#mov_???_/}"
just_the_filename="${just_the_filename/#asf_/}"
just_the_filename="${just_the_filename/#avi_/}"
just_the_filename="${just_the_filename/#mov_/}"
just_the_filename="${just_the_filename/#mpeg1_/}"
just_the_filename="${just_the_filename/#mpeg-ps_/}"
### Strip certain leading characters from the front of the filename
just_the_filename="${just_the_filename/#\ /}"
just_the_filename="${just_the_filename/#\_/}"
just_the_filename="${just_the_filename/#\!/}"
just_the_filename="${just_the_filename/#\./}"
}

check_video_for_avi() {
if [ "${A:0:9}" = "5249 4646" ]; then ### RIFF
filetype="avi"
extension="avi"

avivids="${A:280:9}"
avivids2="${A:470:9}"

if [ "${avivids2}" = "f000 0000" ]; then
 avivids2="${A:478:9}"
fi

avividsascii=`echo -e "\\x${avivids:0:2}\\x${avivids:2:2}\\x${avivids:5:2}\\x${avivids:7:2}"`
avivids2ascii=`echo -e "\\x${avivids2:0:2}\\x${avivids2:2:2}\\x${avivids2:5:2}\\x${avivids2:7:2}"`


avivids2ascii=`echo "${avivids2ascii}" | tr "[:upper:]" "[:lower:]"`
avividsascii=`echo "${avividsascii}" | tr "[:upper:]" "[:lower:]"`

videotype="${avivids2ascii}"
[ "${avividsascii}" != "${avivids2ascii}" ] && videotype="${avividsascii}${avivids2ascii}"
videotype=`echo "${videotype}" | tr -c -d "[:alnum:]"`

[ "${videotype:0:3}" = "iv5" ] && playback_alert="Use QuickTime Player Classic version & Indeo Video 5 CoDec"
[ "${videotype:0:3}" = "iv4" ] && playback_alert="Use QuickTime Player Classic version & Indeo Video 4 CoDec"
[ "${videotype:0:3}" = "iv3" ] && playback_alert="Use mPlayer or QuickTime Player Classic version & Indeo Video 3 CoDec"

fi
}

check_video_for_asf_or_wmv() {
if [ "${filetype}" = "none" ]; then ### Don't check it if it was already identified as an AVI
if [ "${A:0:39}" = "3026 b275 8e66 cf11 a6d9 00aa 0062 ce6c" ]; then
 filetype="asf"
 extension="wmv"
 wmvtype=`strings "${item}" | grep "WMV1\|WMV2\|WMV3\|MP43"`
 videotype=`echo "${wmvtype:0:4}" | head -n 1`

 [ "${A:475:9}" = "4452
4d00" ] && just_the_filename="DRM_${just_the_filename}" && playback_alert="REQUIRES Microsoft Windows OS, VirtualPC works."

 if [ -n "${playback_alert}" ]; then
  drm=`strings -32 "${item}" | grep "http://www.microsoft.com/isapi/redir.dll\|http://66103.vidlock.com/v1.aspx"`

  if [ "${drm:0:40}" = "http://www.microsoft.com/isapi/redir.dll" ]; then
   just_the_filename="DRM_${just_the_filename}"
   playback_alert="REQUIRES Microsoft Windows OS, VirtualPC works."
  fi

  if [ "${drm:0:32}" = "http://66103.vidlock.com/v1.aspx" ]; then
   just_the_filename="DRM_${just_the_filename}"
   playback_alert="REQUIRES Microsoft Windows OS, VirtualPC works."
  fi
 fi

 case "MP43" in
  "${wmvtype}" ) videotype="MP43";;
 esac

 case "WMV1" in
  "${wmvtype}" ) videotype="WMV1"; [ "${playback_alert}" = "" ] && playback_alert="Use Windows Media Player, VLC or mPlayer.";;
 esac

 case "WMV2" in
  "${wmvtype}" ) videotype="WMV2"; [ "${playback_alert}" = "" ] && playback_alert="Use Windows Media Player, VLC or mPlayer.";;
 esac

 case "WMV3" in
  "${wmvtype}" ) videotype="WMV3"; [ "${playback_alert}" = "" ] && playback_alert="Use Windows Media Player.";;
 esac

fi
fi
}


check_video_for_RealVideo() {
#
# Currently only identifies the file type, not whether the encoding is RV10, RV20, RV30 etc.
#
if [ "${filetype}" = "none" ]; then ### Don't check it if it was already identified as an AVI or ASF/WMV
if [ "${A:0:9}" = "2e52 4d46" ]; then
 filetype="RealVideo"
 extension="rm"
 playback_alert="Use RealPlayer / RealONE Player."
fi
fi
}


check_video_for_mpeg() {
if [ "${filetype}" = "none" ]; then ### Don't check it if it was already identified as an AVI or ASF/WMV or RealVideo
if [ "${A:0:9}" = "0000 01ba" ]; then
 filetype="mpeg-ps"
 extension="mpg"
 videotype="mpeg-ps"
fi

if [ "${A:0:9}" = "0000 01b3" ]; then
 filetype="mpeg1"
 extension="mpg"
 videotype="mpeg1"
fi
fi
}


check_video_for_QuickTime_moov() {
#
# The QuickTime identifier needs lots of work, currently identifies the file format and whether the movie is compressed but not
# the CoDecs used etc.
#
if [ "${filetype}" = "none" ]; then ### Don't check it if it was already identified as an AVI or ASF/WMV or RealVideo or MPEG
if [ "${A:0:79}" = "0000 0020 6674 7970 7174 2020 2005 0300
7174 2020 0000 0000 0000 0000 0000 0000" ]; then
 filetype="mov"
 extension="mov" ### Sorenson Video 3
fi

if [ "${A:90:9}" = "6d6f 6f76" ]; then ### moov
 filetype="mov"
 extension="mov"
fi

if [ "${A:160:9}" = "636d 6f76" ]; then
 videotype="cmov"
fi

if [ "${A:110:9}" = "636d 6f76" ]; then
 videotype="cmov"
fi

if [ "${A:10:9}" = "6d6f 6f76" ]; then
 filetype="mov"
 extension="mov"
 if [ "${A:30:9}" = "636d 6f76" ]; then
  videotype="cmov"
 fi
fi

if [ "${A:10:19}" = "6674 7970 7174 2020" ]; then
 filetype="mov"
 extension="mov"
 if [ "${A:30:9}" = "636d 6f76" ]; then
  videotype="cmov"
 fi
fi

if [ "${A:0:29}" = "0000 001c 6674 7970 4d53 4e56" ]; then
 filetype="mov"
 extension="mov"
 videotype="mpeg4"
fi

if [ "${A:10:9}" = "6d64 6174" ]; then ### mdat
 filetype="mov"
 extension="mov"
 videotype="cvid"
fi

# This routine is very slow, disabled for now
#if [ "${filetype}" = "mov" ]; then
# if [ "${videotype}" != "cmov" ]; then
#  nsav=`strings -4 "${item}" | grep "nsav"`
#  case "nsav" in
#   "${nsav}" ) [ "${playback_alert}" = "" ] && playback_alert="No-Save (nsav) flag may be set, the movie appears to be uncompressed.";;
#  esac
#fi
#fi

fi
}

display_file_info() {
echo " Filename: ${filename}"
#[ -n "${filepath}" ] && echo "  Dirname: ${filepath}"
echo " Filetype: ${filetype}   Videotype: ${videotype}   Extension: ${filename_extension}"
[ -n "${playback_alert}" ] && echo " Playback: ${playback_alert}"
echo "-----------------------------------------------------------------------------"
}

generate_output_filename() {
[ "${filetype}" = "${videotype}" ] && filetype="none"

if [ "${filetype}" = "none" ] && [ "${videotype}" != "none" ]; then
target="${videotype}_${just_the_filename}.${extension}"
fi

if [ "${videotype}" = "none" ] && [ "${filetype}" != "none" ]; then
target="${filetype}_${just_the_filename}.${extension}"
fi

if [ "${videotype}" = "none" ] && [ "${filetype}" = "none" ]; then
target="${just_the_filename}.${extension}"
fi

if [ "${videotype}" != "none" ] && [ "${filetype}" != "none" ]; then
target="${filetype}_${videotype}_${just_the_filename}.${extension}"
fi

target="${filepath}/${target}"
}


######## End of Functions (Subroutines)


### Variables
filetype="none"
videotype="none"
avivids=""
avivids2=""
avividsascii=""
avivids2ascii=""
filename=`basename "${item}"`
filename_extension="${filename##*.}"
extension="${filename_extension}"
just_the_filename="${filename%.*}"
# squeeze duplicates of some chrs & cut some chrs out of filename
just_the_filename=`echo "${just_the_filename}" | tr -s " -_.()[]{}" | tr -d "#:!"`
filepath=`dirname "${item}"`
target=""
kind=""
playback_alert=""
nsav=""
A="" # Output from hexdump
wmvtype=""

clear

[ ! -d "${1}" ] && echo "Folder NOT FOUND" && exit 1 ### Die here if the specified directory is not a directory or not in existance
######### If target is a directory
cd "${1}" ### Put us in the specified directory thus making it the current directory
find "${1}" -type f | while read item; do ### Find all the files within the current directory
if [ "${item}" != "." ] && [ "${item}" != ".." ] && [ "${item}" != ".DS_Store" ]; then ### ignore . .. and .DS_Store files
declare_variables_for_this_file ### call the function to set variables for this item
filename_extension=`echo "${filename_extension}" | tr "[:upper:]" "[:lower:]"`
case "${filename_extension}" in
 "mpg" | "mpeg" | "mp4" | "mpe" | "avi" | "asf" | "wmv" | "mov" | "rm" | "divx" ) kind="movie";;
esac ### End of case extension indicates a movie
if [ "${kind}" = "movie" ]; then
 remove_previous_leader_from_filename ### call the function to remove previous video type info from front of filename
 A=`hexdump -vn 1000 "${item}" | colrm 1 8`
 [ "${A:0:39}" = "0000 0000 0000 0000 0000 0000 0000 0000" ] && strip_leading ### call the function to strip leading if zeros exist
 [ "${A:0:39}" = "ffff ffff ffff ffff ffff ffff ffff ffff" ] && strip_leading ### call the function to strip leading if ffs exist
 check_video_for_avi ### call the function to check if this video is avi
 check_video_for_asf_or_wmv ### call the function to check if this video is asf / wmv
 check_video_for_RealVideo ### call the function to check if this video is RealVideo
 check_video_for_mpeg ### call the function to check if this video is mpeg
 check_video_for_QuickTime_moov ### call the function to check if this video is a QuickTime movie
 display_file_info ### call the function to give some feedback to the user
 generate_output_filename
 # Actually renaming the file may fail if we do not have write access
 [ ! -r "${target}" ] && mv "${item}" "${target}" ### Only rename the file if the new name does NOT already exists
 fi ### end of if kind is movie
fi ### end of if item is not . or ..
done ### end of reading filenames from folder
######### End If target is a directory
exit 0


#2 Siph0n

Siph0n
  • V.I.P.
  • 2,870 posts

Posted 28 November 2005 - 09:55 AM

Very nice...haha. I'd test it out but I havent' had any time to put any video on my computer.

Is a rose bush wrong in its thorny self-adornment? Is fire truly bad when it engulfs mere material in beautiful flame? So it is with Siph0n...


Click: Thar? Yes, right thar.

#3 callmenames

callmenames
  • Members
  • 1,426 posts

Posted 03 January 2006 - 04:42 PM

Revised version with new features :)
- Identifies lots of video files, codecs and some graphics files (I kept running into them misnamed as videos)
- Corrects the filename extension for different file / content types
- Provides more info via ffmpeg -i
- Unlocks QuickTime Movies with uncompressed movie data atom
- Disables autoplay and extra tracks (like Flash tracks) in unlocked QuickTime movies
- Disables URLs in QuickTime mov files - whether they are unlocked or not
- Disables URLs in Windows Media Player asf/wmv files (no more web pages opening!)
- Checks for steganography in jpegs with stegdetect
- Cleans up filenames


Separate downloads of bsed, ffmpeg and stegdetect are needed for some of the routines. They are all free downloads. The ffmpeg path is set as a variable which will likely need to be changed for your ffmpeg location. The others are assumed to be in either the same folder as the script or in a directory within your PATH.

CODE
#!/bin/bash
#
#vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
# Information
#
# videntify 1.1 2006-01-03
# Released into the public domain by the author.
# Feel free to modify or swipe any part of this, no credit required.
# Please share your changes and additions too.

# WARNING
#
# Uncompressed QuickTime Movies will open on the screen (to disabled extra tracks and autoplay)
#
# This script -will- rename the video files, prepending file format and video type if known and may
# change the filename extension to reflect the actual file format (for instance an avi file titled
# video.divx with Indeo Video 5 compression would be changed to avi_iv50_video.avi)
#
# Some routines output files to a new file and then rename the orignal name which effectively removes Finder color labels

# DEPENDANCIES
#
# bsed - used to disable the nsav (no-save) flag in uncompressed QuickTime movie data
#        also used to disable web-page addresses in RealMedia, QuickTime and Windows Media files (changes http:// to null://)
# http://www1.bell-labs.com/cgi-user/wwexptools/gensnapshot?bsed
#
# stegdetect - used to detect steganography within jpeg images
# http://www.outguess.org/detection.php
#
# ffmpeg - used to get more info on video files. The pre-compiled binary in the ffmpegX application package works.
# http://homepage.mac.com/major4/
#
# QuickTime Player - used to disabled auto-play and extra tracks in QuickTime mov files.
# http://www.apple.com/quicktime/

# DESCRIPTION
# A Mac OS X BASH script to help identify the file format and video type of video files.
# Intended to be used on quantities of downloaded videos to quickly identify those that will and won't
# play using various OS X media players.

# USAGE
# videntify <path to folder of video files>

# RECOMMENDATIONS
#
# Use the freeware IconCleaner (gui) to strip custom icons from the files before running DupSeek (see next)
# http://www.ziksw.com/software/iconcleaner.html
#
# Use the freeware DupSeek (cli) to eliminate duplicate files (based on their content) before running this script
# http://www.beautylabs.net/software/dupseek.html
#
# Download mPlayer OSX, VLC (VideoLAN Client), Windows Media Player and RealPlayer and the DIVX and 3IVX CoDecs.
# They can be found using macupdate.com or the slow and annoying versiontracker.com
#
# Set the application for each video filename extension to open with for simplified double-clicking
# EXAMPLE: select a file ending in .asf in the Finder, Get Info on the file, in the "Open With" section
# select the program to open the file with such as "mPlayer OSX" and click the "Change All" button.
# Set .wmv files to open in Windows Media Player (this script uses the wmv extension for WMV3 videos)
#
# "MPEG Streamclip" (separate download) can "Fix Timecode Breaks" and then "Convert to MPEG" to save the fixed copy of problem mpeg videos.

# NOTES
#
# Whenever possible, avoid using QuickTime Player, Windows Media Player or RealPlayer as they shamelessly
# allow the media files to open browser windows, display things that interfere with the video etc.
#
# Windows Media Player is needed for asf / WMV3 non-DRM playback
#
# asf/wmv files that use DRM (Digital Rights Management) will only playback in Windows - VirtualPC works (slowly),
# install Windows Media Player 10 under Windows within VirtualPC. You must have the license key for the DRM file.
#
# Many of the RealVideo formats will play in VLC or mPlayer but not all.
#
# Many QuickTime formats will play in VLC or mPlayer, but again, not all.
#
# AVI, MOV, RM, ASF/WMV are file formats - they may contain data encoded with any of thousands of CoDecs
# and many will not play under OS X.
#
# Indeo Video 3 files play poorly in mPlayer, Indeo Video 3, 4, and 5 play well in the Classic QuickTime
# Player with the Indeo CoDecs installed. They also play in VirtualPC.
#
# The ToVid package includes a cli utility "IdVid" which calls the players themselves (mPlayer for instance) to
# report far more information about video files, it also takes much longer.
# http://tovid.sourceforge.net/

# IDEAS FOR ENHANCEMENTS TO THIS SCRIPT (please share your changes)
# Allow the script to work on a single file, list of files or a directory instead of just a directory
# If no files or folders are specified, prompt user
# Allow "move to" location(s) to be specified, perhaps into folders by file format and then subfolders by video CoDec
# Basic checks such as for write access
# If the "new" filename exists, add numbers counting up, then append the extension (at the moment it just skips the file)
# Optionally set permissions, owner / group and flags for each file processed
# Decompress QuickTime Movie Resource data to check for nsav in compressed movies and unlock
# Option to disable QuickTime movie Flash, Sprite, Event tracks
# Option to split-out joined mpeg files (they play fine in mPlayer but in QT Player some only play the first clip within the file)
# Identify audio CoDecs as well
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# End of Information

#vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
# Functions (Subroutines)

####################################
strip_leading() {
A="${A:40}" ### stips 40 characters from the front of A, eliminates one entire line at a time from hexdump output
[ "${A:0:39}" = "0000 0000 0000 0000 0000 0000 0000 0000" ] && strip_leading
[ "${A:0:39}" = "ffff ffff ffff ffff ffff ffff ffff ffff" ] && strip_leading
}

####################################
declare_variables_for_this_file() {
# These should be reset for each file
filetype="none"
videotype="none"
videotype_lower=""
avivids=""
avivids2=""
avividsascii=""
avivids2ascii=""
filename=`basename "${item}"`
extension="${filename##*.}"
extension=`echo "${extension}" | tr "[:upper:]" "[:lower:]"`
just_the_filename="${filename%.*}"
[ "${extension}" = "${just_the_filename}" ] && extension="unknown"
# squeeze duplicates of some chrs & cut some chrs out of filename
just_the_filename=`echo "${just_the_filename}" | tr -s "\ \-\_\.\(\)\[\]\{\}" | tr -d "\#\:\!"`
filepath=`dirname "${item}"`
target=""
kind=""
playback_alert=""
nsav=""
A="" # Output from hexdump
wmvtype=""
wmv_drm="0"
http_status=""
file_size_before=""
file_size_after=""
file_size=""
ffmpeg_info=""
ffmpeg_fps=""
ffmpeg_pixels=""
ffmpeg_video=""
steg=""
}

####################################
remove_previous_leader_from_filename() {
# remove asf wmv mpg mov etc from front of just_the_filename in case the file has already been renamed
# current formats are
# avi_div3_
# avi_divxdx50_
just_the_filename="${just_the_filename/#*\fps-DRM_/}"
just_the_filename="${just_the_filename/#*\fps-/}"
just_the_filename="${just_the_filename/#mov_MPEG1-Muxed-/}"
just_the_filename="${just_the_filename/#mov_MPEG1-Muxed_/}"
just_the_filename="${just_the_filename/#asf_????_DRM_/}"
just_the_filename="${just_the_filename/#asf_????_/}"
just_the_filename="${just_the_filename/#avi_????????_/}"
just_the_filename="${just_the_filename/#avi_???????_/}"
just_the_filename="${just_the_filename/#avi_??????_/}"
just_the_filename="${just_the_filename/#avi_????_/}"
just_the_filename="${just_the_filename/#avi_???_/}"
just_the_filename="${just_the_filename/#mov_????_/}"
just_the_filename="${just_the_filename/#mov_???_/}"
just_the_filename="${just_the_filename/#asf_/}"
just_the_filename="${just_the_filename/#avi_/}"
just_the_filename="${just_the_filename/#mov-/}"
just_the_filename="${just_the_filename/#mov_/}"
just_the_filename="${just_the_filename/#mpeg1_/}"
just_the_filename="${just_the_filename/#mpeg-ps_/}"
just_the_filename="${just_the_filename/#jpeg__/}"
just_the_filename="${just_the_filename/#jpeg-/}"
just_the_filename="${just_the_filename/#jpeg_/}"
just_the_filename="${just_the_filename/#pdf_/}"
just_the_filename="${just_the_filename/#gif_/}"
just_the_filename="${just_the_filename/#p7_/}"
### Strip certain leading characters from the front of the filename
just_the_filename="${just_the_filename/#\ /}"
just_the_filename="${just_the_filename/#\_/}"
just_the_filename="${just_the_filename/#\!/}"
just_the_filename="${just_the_filename/#\./}"
### replace space-dash and dash-space with just a dash
just_the_filename="${just_the_filename/\ \-/-}"
just_the_filename="${just_the_filename/\-\ /-}"
}

####################################
check_video_for_avi() {
if [ "${filetype}" = "none" ]; then
if [ "${A:0:9}" = "5249 4646" ]; then ### RIFF
filetype="avi"
extension="avi"

avivids="${A:280:9}"
avivids2="${A:470:9}"

if [ "${avivids2}" = "f000 0000" ]; then
 avivids2="${A:478:9}"
fi

avividsascii=`echo -e "\\x${avivids:0:2}\\x${avivids:2:2}\\x${avivids:5:2}\\x${avivids:7:2}"`
avivids2ascii=`echo -e "\\x${avivids2:0:2}\\x${avivids2:2:2}\\x${avivids2:5:2}\\x${avivids2:7:2}"`

avivids2ascii=`echo "${avivids2ascii}" | tr "[:upper:]" "[:lower:]"`
avividsascii=`echo "${avividsascii}" | tr "[:upper:]" "[:lower:]"`

videotype="${avivids2ascii}"
[ "${avividsascii}" != "${avivids2ascii}" ] && videotype="${avividsascii}${avivids2ascii}"
videotype=`echo "${videotype}" | tr -c -d "[:alnum:]"`

[ "${videotype:0:3}" = "iv5" ] && playback_alert="Use QuickTime Player Classic version & Indeo Video 5 CoDec"
[ "${videotype:0:3}" = "iv4" ] && playback_alert="Use QuickTime Player Classic version & Indeo Video 4 CoDec"
[ "${videotype:0:3}" = "iv3" ] && playback_alert="Use mPlayer or QuickTime Player Classic version & Indeo Video 3 CoDec"

if [ -z "${videotype}" ]; then
 [ "${A:20:9}" = "4344 5841" ] && videotype="CDXA" && playback_alert="Use mPlayer to play avi files with CDXA video."
fi

get_more_info

fi
fi
}

####################################
check_for_jpeg() {
if [ "${filetype}" = "none" ]; then ### Don't check it if it was already identified
if [ "${A:0:9}" = "ffd8 ffe1" -o "${A:0:9}" = "ffd8 ffe0" ]; then
 filetype="jpeg"
 extension="jpg"
 videotype="none"

 # check for steganography
 echo -n " Checking for steganography. False positives are common..."
 steg=`stegdetect -q "${item}"`
 if [ -n "${steg}" ]; then
  echo "${steg##*\ }"
 else
  echo
 fi

fi
fi
}

#################################### Not sure if this info is definitive for mp3 so this routine is unused right now
check_for_mp3() {
if [ "${filetype}" = "none" ]; then ### Don't check it if it was already identified
if [ "${A:0:7}" = "4944 3303" ]; then
 filetype="mp3"
 extension="mp3"
 videotype="none"
fi
fi
}

####################################
check_for_P7() {
if [ "${filetype}" = "none" ]; then ### Don't check it if it was already identified
if [ "${A:0:14}" = "5037 2033 3332" ]; then
 filetype="P7"
 extension="P7"
 videotype="none"
 playback_alert="XV Visual Schnauzer thumbnail file, not really worth keeping."
fi
fi
}

####################################
check_for_pdf() {
if [ "${filetype}" = "none" ]; then ### Don't check it if it was already identified
if [ "${A:0:9}" = "2550 4446" ]; then
 filetype="pdf"
 extension="pdf"
 videotype="none"
fi
fi
}

####################################
check_for_gif() {
if [ "${filetype}" = "none" ]; then ### Don't check it if it was already identified
if [ "${A:0:7}" = "4749 46" ]; then
 filetype="gif"
 extension="gif"
 videotype="none"
fi
fi
}

####################################
check_video_for_asf_or_wmv() {
if [ "${filetype}" = "none" ]; then ### Don't check it if it was already identified


if [ "${A:0:39}" = "3026 b275 8e66 cf11 a6d9 00aa 0062 ce6c" ]; then
 filetype="asf"
 extension="wmv"
 echo " Checking asf/wmv file for video type"
# Strings is slow on large files, change to bsed and set m-1-1 for min 1 max 1 matches (assuming bsed stops looking)
 wmvtype=`strings "${item}" | grep "WMV1\|WMV2\|WMV3\|MP43\|M4S2"`
 videotype=`echo "${wmvtype:0:4}" | head -n 1`

 if [ "${A:475:9}" = "4452
4d00" ]; then
  just_the_filename="DRM_${just_the_filename}"
  playback_alert="REQUIRES Microsoft Windows OS, VirtualPC works."
  wmv_drm="1"
 fi

 if [ -z "${playback_alert}" ]; then
  drm=`strings -32 "${item}" | grep "http://www.microsoft.com/isapi/redir.dll\|vidlock"`

  if [ "${drm:0:40}" = "http://www.microsoft.com/isapi/redir.dll" ] && [ -z "${playback_alert}" ]; then
   just_the_filename="DRM_${just_the_filename}"
   playback_alert="REQUIRES Microsoft Windows OS, VirtualPC works."
   wmv_drm="1"
  fi

  if [[ "${drm}" = *"vidlock"* ]] && [ -z "${playback_alert}" ]; then
   just_the_filename="DRM_${just_the_filename}"
   playback_alert="REQUIRES Microsoft Windows OS, VirtualPC works."
   wmv_drm="1"
  fi


 fi

 case "M4S2" in
  # Note that M4S2 asf/wmv files will play in mPlayer on OS X but NOT in Windows Media Player 9
  "${videotype}" ) videotype="M4S2"; extension="asf"; [ "${playback_alert}" = "" ] && playback_alert="Use mPlayer.";;
 esac

 case "MP43" in
  "${videotype}" ) videotype="MP43"; extension="asf";;
 esac

 case "WMV1" in
  "${videotype}" ) videotype="WMV1"; extension="asf"; [ "${playback_alert}" = "" ] && playback_alert="Use Windows Media Player, VLC or mPlayer.";;
 esac

 case "WMV2" in
  "${videotype}" ) videotype="WMV2"; extension="asf"; [ "${playback_alert}" = "" ] && playback_alert="Use Windows Media Player, VLC or mPlayer.";;
 esac

 case "WMV3" in
  "${videotype}" ) videotype="WMV3"; extension="wmv"; [ "${playback_alert}" = "" ] && playback_alert="Use Windows Media Player.";;
 esac

 if [ "${filetype}" = "asf" -o "${filetype}" = "wmv" ] && [ "${wmv_drm}" != "1" ]; then
  check_for_http
 fi

  [ "${filetype}" = "asf" -o "${filetype}" = "wmv" ] && get_more_info

fi
fi
}

####################################
check_video_for_RealVideo() {
#
if [ "${filetype}" = "none" ]; then ### Don't check it if it was already identified
if [ "${A:0:9}" = "2e52 4d46" ]; then
 filetype="RealVideo"
 extension="rm"
 playback_alert="Use RealPlayer / RealONE Player."
 get_more_info
 #check_for_http # hmmm, might cripple streams?
fi
fi
}

####################################
check_video_for_mpeg() {
if [ "${filetype}" = "none" ]; then ### Don't check it if it was already identified
if [ "${A:0:9}" = "0000 01ba" ]; then
 filetype="mpeg-ps"
 extension="mpg"
 videotype="mpeg-ps"
 get_more_info
fi

if [ "${A:0:9}" = "0000 01b3" ]; then
 filetype="mpeg1"
 extension="mpg"
 videotype="mpeg1"
 get_more_info
fi

fi
}

####################################
check_video_for_QuickTime_moov() {
if [ "${filetype}" = "none" ]; then ### Don't check it if it was already identified

if [ "${A:0:79}" = "0000 0020 6674 7970 7174 2020 2005 0300
7174 2020 0000 0000 0000 0000 0000 0000" ]; then
 # 20 6674 7970 7174 2020 20 = " ftypqt   "
 filetype="mov"
 extension="mov"
fi

if [ "${A:90:9}" = "6d6f 6f76" ]; then ### moov
 filetype="mov"
 extension="mov"
fi

if [ "${filetype}" = "mov" ] && [ "${A:160:9}" = "636d 6f76" ]; then
 videotype="cmov"
fi

if [ "${filetype}" = "mov" ] && [ "${A:110:9}" = "636d 6f76" ]; then
 videotype="cmov"
fi

if [ "${A:10:9}" = "6d6f 6f76" ]; then
 filetype="mov"
 extension="mov"
 if [ "${A:30:9}" = "636d 6f76" ]; then
  videotype="cmov"
 fi
fi

if [ "${A:10:19}" = "6674 7970 7174 2020" ]; then
 filetype="mov"
 extension="mov"
 if [ "${A:30:9}" = "636d 6f76" ]; then
  videotype="cmov"
 fi
fi

if [ "${A:0:29}" = "0000 001c 6674 7970 4d53 4e56" ]; then
 filetype="mov"
 extension="mov"
 videotype="mpeg4"
if [ "${A:0:39}" = "0000 001c 6674 7970 4d53 4e56 0100 002c" ]; then
 filetype="mpeg4"
 extension="mp4"
 playback_alert="Some MSNV MP4 files will only play in QuickTime if the filename extension is mp4."
fi
fi

if [ "${A:10:9}" = "6d64 6174" ]; then ### mdat
 filetype="mov"
 extension="mov"
 videotype="cvid"
fi

if [ "${A:0:39}" = "0000 63fc 6672 6565 0000 006c 6d76 6864" ]; then
 filetype="mov"
 extension="mov"
 videotype="svq3"
fi

if [ "${A:0:39}" = "0001 45ea 6672 6565 0000 006c 6d76 6864" ]; then
 filetype="mov"
 extension="mov"
 videotype="mpeg4"
fi


if [ "${A:0:39}" = "0000 051d 6d6f 6f76 0000 006c 6d76 6864" -o "${A:0:39}" = "0000 043e 6672 6565 0000 006c 6d76 6864" ]; then
 filetype="mov"
 extension="mov"
 videotype="MPEG1-Muxed"
 playback_alert="Use FileJuicer to extract the MPEG from this QuickTime mov wrapper file."
fi

if [ "${A:0:39}" = "0000 0483 6d6f 6f76 0000 006c 6d76 6864" -o "${A:0:39}" = "0000 052b 6d6f 6f76 0000 006c 6d76 6864" ]; then
 filetype="mov"
 extension="mov"
 videotype="MPEG1-Muxed"
 playback_alert="Use FileJuicer to extract the MPEG from this QuickTime mov wrapper file."
fi

if [ "${A:0:39}" = "0000 04e6 6672 6565 0000 006c 6d76 6864" -o "${A:0:39}" = "0000 051d 6672 6565 0000 006c 6d76 6864" ]; then
 filetype="mov"
 extension="mov"
 videotype="MPEG1-Muxed"
 playback_alert="Use FileJuicer to extract the MPEG from this QuickTime mov wrapper file."
fi


if [ "${A:0:39}" = "0000 073d 6672 6565 0000 006c 6d76 6864" -o "${A:0:39}" = "0000 052b 6d6f 6f76 0000 006c 6d76 6864" ]; then
 filetype="mov"
 extension="mov"
 videotype="MPEG1-Muxed"
 playback_alert="Use FileJuicer to extract the MPEG from this QuickTime mov wrapper file."
fi

# This routine is dependant on bsed and disables the nsav flag in uncompressed mov data
if [ "${filetype}" = "mov" ] && [ "${videotype}" != "cmov" ]; then
 echo -n " Checking for & unlocking nsav: "
  bsed "6E73617600000001=6E73617600000000" "${item}" "${item}_nsav_UNLOCKED.tmp"
 file_size_before=`ls -al "${item}" | tr -s "\ " | cut -d " " -f 5`
 file_size_after=`ls -al "${item}_nsav_UNLOCKED.tmp" | tr -s "\ " | cut -d " " -f 5`
  if [ "${file_size_before}" == "${file_size_after}" ]; then
   mv "${item}_nsav_UNLOCKED.tmp" "${item}"
  else
   echo " AFTER NULLIFYING THE URL THE FILE SIZE HAS CHANGED. It was ${file_size_before} and is ${file_size_after}"
   #rm "${item}_nsav_UNLOCKED.tmp"
  fi
fi

if [ "${filetype}" = "mov" ]; then
 check_for_http
 get_more_info
 [ "${videotype}" != "cmov" ] && set_mov_features
fi

fi
}

####################################
check_for_http() {
# This routine is dependant on bsed and changes the http:// string to "null://" to prevent opening web pages.
if [ "${filetype}" != "none" ]; then
echo -n " Checking for URLs: "
 bsed "http:\\"="null:\\" "${item}" "${item}_deHTTP.tmp"
 file_size_before=`ls -al "${item}" | tr -s "\ " | cut -d " " -f 5`
 file_size_after=`ls -al "${item}_deHTTP.tmp" | tr -s "\ " | cut -d " " -f 5`
 if [ "${file_size_before}" == "${file_size_after}" ]; then
  mv "${item}_deHTTP.tmp" "${item}"
 else
  echo " AFTER NULLIFYING URL THE FILE SIZE HAS CHANGED. It was ${file_size_before} and is ${file_size_after}"
  #rm "${item}_deHTTP.tmp"
 fi
fi
}

####################################
display_file_info() {
#[ -n "${filepath}" ] && echo "  Dirname: ${filepath}"
#echo " Filetype: ${filetype}   Videotype: ${videotype}   Extension: ${extension}"
[ -n "${playback_alert}" ] && echo " Playback: ${playback_alert}"
echo
echo "============================================================================
==="
}

####################################
clean_just_the_filename() {
# get rid of problematic characters and squeeze spaces, periods, hyphens and underscores
just_the_filename=`echo "${just_the_filename}" | tr -Cd "[:alnum:] _-.()" | tr -cd "[:print:]" | tr -s "\ \.\_\-"`
# since we are only working with the filename and there is no extension at this point, remove blank spaces at the end
just_the_filename=`echo "${just_the_filename}" | sed -e 's/[ ]*$//'`
}

####################################
generate_output_filename() {
[ "${filetype}" = "${videotype}" ] && filetype="none" # no sense naming the file mpeg1_mpeg1...

if [ "${just_the_filename:0:1}" = "-" -o "${just_the_filename:0:1}" = "_" ]; then
just_the_filename="${just_the_filename:1}"
fi

if [ "${filetype}" = "none" ] && [ "${videotype}" != "none" ]; then # if videotype is not none and filetype is none
[ -z "${ffmpeg_info}" ] && target="${videotype}-${just_the_filename}.${extension}"
[ -n "${ffmpeg_info}" ] && target="${videotype}_${just_the_filename}.${extension}"
fi

if [ "${videotype}" = "none" ] && [ "${filetype}" != "none" ]; then # if videotype is none and filetype is not none
 [ -z "${ffmpeg_info}" ] && target="${filetype}-${just_the_filename}.${extension}"
 [ -n "${ffmpeg_info}" ] && target="${filetype}_${just_the_filename}.${extension}"
fi

if [ "${videotype}" = "none" ] && [ "${filetype}" = "none" ]; then # if both videotype and filetype are none
target="${just_the_filename}.${extension}"
fi

if [ "${videotype}" != "none" ] && [ "${filetype}" != "none" ]; then # if both videotype and filetype are not none
 [ -z "${ffmpeg_info}" ] && target="${filetype}_${videotype}-${just_the_filename}.${extension}"
 [ -n "${ffmpeg_info}" ] && target="${filetype}_${videotype}_${just_the_filename}.${extension}"
fi

# If the new filename is longer than 255 characters then remove the extension, truncate to 250 which allows
# five character extensions such as ".mpeg" and then re-add the extension
if [ "${#target}" -gt 255 ]; then
target="${target%.*}"
target="${target:0:250}.${extension}"
fi

target="${filepath}/${target}"
}

####################################
set_mov_features() {
echo " Calling QuickTime Player to disable auto-play and extra tracks."
osascript -e 'tell application "QuickTime Player"' -e "set show welcome movie automatically to false" -e "close every movie" -e "set play movie from beginning when opened to true" -e "set ignore auto play to true" -e "set ignore auto present to true" -e "set only front movie plays sound to true" -e "set open movie in new player to true" -e "end tell"

open -a "QuickTime Player" "${item}"

osascript -e 'tell application "QuickTime Player"' -e "set current time of front movie to 0" -e "set the enabled of (every track of movie 1) to false" -e "set the enabled of (every track of movie 1 whose kind is \"Sound\") to true" -e "set the enabled of (every track of movie 1 whose kind is \"Sound 1\") to true" -e "set the enabled of (every track of movie 1 whose kind is \"Video\") to true" -e "set the enabled of (every track of movie 1 whose kind is \"Video 1\") to true" -e "set the enabled of (every track of movie 1 whose kind is \"Mpeg1 Muxed\") to true" -e "set the auto play of movie 1 to false" -e "set the auto present of movie 1 to false" -e "set the looping of movie 1 to false" -e "set the auto close when done of movie 1 to false" -e "set the auto quit when done of movie 1 to false" -e "set the controller type of movie 1 to standard" -e "set the saveable of (every movie) to true" -e "save (movie 1)" -e "close window 1" -e "end tell"
}

####################################
get_more_info() {
# redo this to get the info from ffmpeg only once and save as a string then extract from the string

if [ "${filetype}" != "none" ]; then

# ffmpeg -i "${item}" 2>&1 | grep "Duration: "
# result is   Duration: 00:02:13.4, start: 1.000000, bitrate: 1911 kb/s
# Note that duration is approximate only
# "${ffmpeg}" -i "${item}" 2>&1 | grep "Audio: "
# result is   Stream #0.1[0x1c0]: Audio: mp2, 44100 Hz, stereo, 192 kb/s

ffmpeg_video=`"${ffmpeg}" -i "${item}" 2>&1 | grep "Video: " | tr -s "\ " | cut -d " " -f 7` # mpeg1video,
ffmpeg_video="${ffmpeg_video/video/}"
ffmpeg_video=`echo "${ffmpeg_video}" | tr "[:upper:]" "[:lower:]"`
videotype_lower=`echo "${videotype}" | tr "[:upper:]" "[:lower:]"`
ffmpeg_video="${ffmpeg_video/\,/}"
[ "${ffmpeg_video}" = "${videotype_lower}" ] && ffmpeg_video=""

ffmpeg_pixels=`"${ffmpeg}" -i "${item}" 2>&1 | grep "Video: " | tr -s "\ " | cut -d " " -f 9` # 720x480,
[[ "${ffmpeg_pixels}" == "0x"* ]] && ffmpeg_pixels=""
ffmpeg_fps=`"${ffmpeg}" -i "${item}" 2>&1 | grep "Video: " | tr -s "\ " | cut -d " " -f 4` # 29.97
ffmpeg_fps="${ffmpeg_fps:0:2}"
ffmpeg_fps="${ffmpeg_fps%.*}"
#ffmpeg_audio=`"${ffmpeg}" -i "${item}" 2>&1 | grep "Audio: " | tr -s "\ " | cut -d " " -f 5` # mp2,
#ffmpeg_duration=`"${ffmpeg}" -i "${item}" 2>&1 | grep "Duration: " | cut -d "," -f 1 | tr -s "\ " | cut -d " " -f 3 | cut -d "." -f 1` # 00:02:13
#ffmpeg_duration="${ffmpeg_duration:0:2}h${ffmpeg_duration:2:2}m${ffmpeg_duration:4:2}s"

[ -n "${ffmpeg_video}" ]  && ffmpeg_info="${ffmpeg_video}"
[ -n "${ffmpeg_pixels}" ] && ffmpeg_info="${ffmpeg_info}_${ffmpeg_pixels}"
[ -n "${ffmpeg_fps}" ]    && ffmpeg_info="${ffmpeg_info}_${ffmpeg_fps}fps"
[ -n "${ffmpeg_info}" ]   && ffmpeg_info="${ffmpeg_info}-"

[ "${ffmpeg_info:0:1}" = "_" ] && ffmpeg_info="${ffmpeg_info:1}"
just_the_filename="${ffmpeg_info}-${just_the_filename}"
fi
}
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# End of Functions (Subroutines)


#vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
ffmpeg="/Applications/ffmpegX_0.0.9u/ffmpegX.app/Contents/Resources/ffmpeg"
item=""
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# End of variables


#vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
# Main
osascript -e 'tell application "Terminal" to set the size of the window frontmost to {1065, 450}'

clear

echo "============================================================================
==="

[ ! -d "${1}" ] && echo "Folder NOT FOUND" && exit 1 ### Die here if the specified directory is not a directory or not in existance
######### If target is a directory
cd "${1}" ### Put us in the specified directory thus making it the current directory
echo "DIRECTORY: "`pwd`
echo "============================================================================
==="
find "${1}" -type f \! -name ".DS_Store" \! -name "." \! -name ".." | while read item; do
declare_variables_for_this_file

case "${extension}" in
 "mpg" | "mpeg" | "mp4" | "mpe" | "avi" | "asf" | "wmv" | "mov" | "rm" | "divx" | "jpeg" | "jpg" | "pdf" | "p7" | "gif" ) kind="known";;
esac ### End of case extension indicates a movie
file_size=`ls -al "${item}" | tr -s "\ " | cut -d " " -f 5`
if [ "${kind}" = "known" ] && [ "${file_size}" -gt "1" ]; then

 remove_previous_leader_from_filename ### call the function to remove previous info leader from front of filename

 echo ">${filename}"
 A=`hexdump -vn 1000 "${item}" | colrm 1 8`
 [ "${A:0:39}" = "0000 0000 0000 0000 0000 0000 0000 0000" ] && strip_leading ### call the function to strip leading if zeros exist
 [ "${A:0:39}" = "ffff ffff ffff ffff ffff ffff ffff ffff" ] && strip_leading ### call the function to strip leading if ffs exist

 [ "${filetype}" = "none" ] && check_for_jpeg
 [ "${filetype}" = "none" ] && check_for_P7
 [ "${filetype}" = "none" ] && check_for_pdf
 [ "${filetype}" = "none" ] && check_for_gif
 [ "${filetype}" = "none" ] && check_video_for_avi
 [ "${filetype}" = "none" ] && check_video_for_asf_or_wmv
 [ "${filetype}" = "none" ] && check_video_for_RealVideo
 [ "${filetype}" = "none" ] && check_video_for_mpeg
 [ "${filetype}" = "none" ] && check_video_for_QuickTime_moov

 [ -n "${playback_alert}" ] && echo " Playback: ${playback_alert}" # since display_file_info is disabled above

 clean_just_the_filename

 generate_output_filename

 # Actually renaming the file may fail if we do not have write access
 [ ! -r "${target}" ] && mv "${item}" "${target}" ### Only rename the file if the new name does NOT already exist
 echo "<"`basename "${target}"`
 echo "--------------------------------------------------------------------------------"
fi ### end of if kind is known

done ### end of reading filenames from folder
######### End If target is a directory

echo "============================================================================
==="

exit 0


To use the script, save the script to a text file, chmod +x the file, open Terminal, drag the script into the terminal window (and type a space if needed) then drag a folder full of media files to the window. Or type the script path, type the folder path. Press return.

#4 callmenames

callmenames
  • Members
  • 1,426 posts

Posted 03 January 2006 - 05:05 PM

Apparently the posting process here doesn't like a few lines. Find and replace:

echo & #34;============================================================================
===="

with

echo double-quote-chr=============================================================================
=="

And remove the line breaks so that it is one line.

EDIT: By the way the ===='s are the default width of OS X's Terminal windows.

Edited by callmenames, 03 January 2006 - 05:06 PM.


#5 callmenames

callmenames
  • Members
  • 1,426 posts

Posted 03 January 2006 - 08:05 PM

Posting the script seems to have mangled the osascript commands as well... so here it is as a zipped text file, executable, with a few more additions (now double-clickable command file, asks for folder path if not supplied etc.)

http://rapidshare.de...ommand.zip.html

Edited by callmenames, 03 January 2006 - 08:06 PM.


#6 callmenames

callmenames
  • Members
  • 1,426 posts

Posted 04 January 2006 - 11:30 PM

Version 1.3

This one finds your bsed, ffmpeg and stegdetect executables via the locate command.

http://rapidshare.de...ommand.zip.html

#7 callmenames

callmenames
  • Members
  • 1,426 posts

Posted 08 January 2006 - 09:16 PM

Version 1.4 with binaries and info for bsed, ffmpeg and stegdetect (all together its 1.4 MegaBytes)

http://rapidshare.de...fy_1.4.zip.html

The following text is the source of the script itself for reference. It may get mangled in posting so you should download from the link above.

CODE
#!/bin/bash
#
#vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
# Information
#
# videntify 1.4 2006-01-07
# Released into the public domain by the author.
# Feel free to modify or swipe any part of this, no credit required.
# Please share your changes and additions too.


# USAGE
# ./videntify.command <path to folder of video files>
# or double click and either type a path or drag a folder to the terminal window


# DESCRIPTION
# A Mac OS X BASH script to help identify the file format and video type of video files.
# Intended to be used on quantities of downloaded videos to quickly identify those that will and won't
# play using various OS X media players.


# WARNING
# Uncompressed QuickTime Movies will open on the screen (to disable extra tracks and autoplay)
#
# This script -will- rename the video files, prepending file format and video type if known and may
# change the filename extension to reflect the actual file format (for instance an avi file titled
# video.divx with Indeo Video 5 compression would be changed to avi_iv50_video.avi)
#
# Some routines output files to a new file and then replace the orignal which effectively removes Finder color labels


# DEPENDANCIES
# bsed - used to disable the nsav (no-save) flag in uncompressed QuickTime movie data
#        also used to disable web-page addresses in RealMedia, QuickTime and Windows Media files (changes http:// to null://)
# http://www1.bell-labs.com/cgi-user/wwexptools/gensnapshot?bsed
#
# stegdetect - used to detect steganography within jpeg images
# http://www.outguess.org/detection.php
#
# ffmpeg - used to get more info on video files. The pre-compiled binary in the ffmpegX application package works.
# http://homepage.mac.com/major4/
#
# QuickTime Player - used to disabled auto-play and extra tracks in QuickTime mov files.
# http://www.apple.com/quicktime/
#
# Optional
# GetFileInfo and SetFile from Apple's free Developer's Tools or XCode which hopefully came with your Mac
# http://developer.apple.com/tools/


# RECOMMENDATIONS
# Use the freeware IconCleaner (gui) to strip custom icons from the files before running DupSeek (see next)
# http://www.ziksw.com/software/iconcleaner.html
#
# Use the freeware DupSeek (cli) to eliminate duplicate files (based on their content) before running this script
# http://www.beautylabs.net/software/dupseek.html
#
# Download MPlayer OSX, VLC (VideoLAN Client), Windows Media Player and RealPlayer and the DIVX and 3IVX CoDecs.
# They can be found using macupdate.com or versiontracker.com
#
# MPlayer OSX (the official MPlayer)
# This is version 1.0pre8 of MPlayer OSX which includes the binary MPlayer dev-CVS-050904-08:02-4.0.0
# http://mplayerhq.hu/homepage/design7/dload.html
#
# MPlayer OS X 2 (the buggy one)
# 2b8r5 requires Tiger, 2b8R4 requires 10.3+
# Version 2B8r4 includes the binary MPlayer dev-CVS-041212-12:40-3.3
# http://sourceforge.net/project/showfiles.php?group_id=62947
#
# djoPlayer (small player with few features)
# Version 3.5.1 includes the binary MPlayer dev-CVS-050123-19:55-3.3
# http://homepage.mac.com/djodjodesign/index2.html
#
# VLC (VideoLan Client)
# http://www.videolan.org/vlc/download-macosx.html
#
# Windows Media Player for Mac
# http://www.microsoft.com/mac/otherproducts/otherproducts.aspx?pid=windowsmedia
#
# MPEG Streamclip
# It can "Fix Timecode Breaks" and then "Convert to MPEG" to save a fixed copy of problem mpeg videos.
# http://www.alfanet.it/squared5/mpegstreamclip.html
#
# RealPlayer
# http://www.real.com/mac
#
# Set the application for each video filename extension to open with for simplified double-clicking
# EXAMPLE: select a file ending in .asf in the Finder, Get Info on the file, in the "Open With" section
# select the program with which to open the file such as "MPlayer OS X" and click the "Change All" button.
# Set .wmv files to open in Windows Media Player (this script uses the wmv extension for WMV3 videos)


# NOTES
# Whenever possible, avoid using QuickTime Player, Windows Media Player or RealPlayer as they shamelessly
# allow the media files to open browser windows, display things that interfere with the video etc.
#
# Windows Media Player is needed for asf / WMV3 non-DRM playback
#
# asf/wmv files that use DRM (Digital Rights Management) will only playback in Windows - VirtualPC works (slowly),
# install Windows Media Player 10 under Windows within VirtualPC. You must have the license key for the DRM file.
#
# Many of the RealVideo formats will play in VLC or MPlayer but not all.
#
# Many QuickTime formats will play in VLC or MPlayer, but again, not all.
#
# AVI, MOV, RM, ASF/WMV are file formats - they may contain data encoded with any of thousands of CoDecs
# and many will not play under OS X.
#
# Indeo Video 3 files play poorly in MPlayer, Indeo Video 3, 4, and 5 play well in the Classic QuickTime
# Player with the Indeo CoDecs installed. They also play in VirtualPC.
#
# The ToVid package includes a cli utility "IdVid" which calls the players themselves (MPlayer for instance) to
# report far more information about video files, it also takes much longer.
# http://tovid.sourceforge.net/


# IDEAS FOR ENHANCEMENTS TO THIS SCRIPT (please share your changes)
# Allow the script to work on a single file, list of files or directoris or a single directory instead of just a directory
# Allow "move to" location(s) to be specified, perhaps into folders by file format and then subfolders by video CoDec
# Basic checks such as for write access
# Optionally set permissions, owner / group and flags for each file processed (chflags nouchg already happens)
# Decompress QuickTime Movie Resource data to check for nsav in compressed movies and unlock
# Option to split-out joined mpeg files with timecode breaks using vcdgear
# Identify audio CoDecs as well
# Get file attributes and create date from the original file and preserve them on recreated files
# Handle dmgs
# Add firstframe routines

#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# End of Information



#vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
# Functions (Subroutines)

####################################
greeting() {
  clear # note that the === line below is the default width of OS X's Terminal window
  echo "============================================================================
==="
  echo "Script title : videntify"
  echo "Script origin: http://www.machacking.net/forums/index.php?showtopic=5339&hl="
  echo
  echo "  Attempts to identify media file format and set appropriate filename extension."
  echo "  Attempts to identify video compression used in the media files if any."
  echo "  Unlocks QuickTime Movies with uncompressed movie data atom."
  echo "  Disables autoplay and extra tracks in unlocked QuickTime movies."
  echo "  Disables URLs in QuickTime mov files and Windows Media Player asf/wmv files."
  echo "  Checks for steganography in jpegs with stegdetect."
  echo "  Cleans filenames for compatibility with some players."
  echo
  echo "  You will be asked to supply the path to the folder of files to inspect."
  echo
  echo "  You may either type in the path to the folder or you may drag-and-drop the"
  echo "  file into this window, then click back in this window with the mouse and"
  echo "  press the return key."
  echo
}

####################################
select_source_folder() {
  echo
  echo "  ========================== Select a source folder =========================="
  echo
  echo "  The script can be cancelled by pressing Control-C or Command-period"
  echo "  Please select the folder of media files for videntify to inspect."
  echo -n "  Folder: ";
  read -e source_folder

  if [ ! -e "${source_folder}" ]; then
     echo "  There is no folder with that path. Please try again." && select_source_folder
  fi

  if [ ! -d "${source_folder}" ]; then
     echo "  There is a file, not a folder at that path. Please try again." && select_source_folder
  fi
}


####################################
strip_leading() {
A="${A:40}" ### stips 40 characters from the front of A, eliminates one entire line at a time from hexdump output
[ "${A:0:39}" = "0000 0000 0000 0000 0000 0000 0000 0000" ] && strip_leading
[ "${A:0:39}" = "ffff ffff ffff ffff ffff ffff ffff ffff" ] && strip_leading
}

####################################
declare_variables_for_this_file() {
# These should be reset for each file
filetype="none"
videotype="none"
videotype_lower=""
avivids=""
avivids2=""
avividsascii=""
avivids2ascii=""
filename=`basename "${item}"`
extension="${filename##*.}"
extension=`echo "${extension}" | tr "[:upper:]" "[:lower:]"`
just_the_filename="${filename%.*}"
[ "${extension}" = "${just_the_filename}" ] && extension="unknown"
# squeeze duplicates of some chrs & cut some chrs out of filename
just_the_filename=`echo "${just_the_filename}" | tr -s "\ \-\_\.\(\)\[\]\{\}" | tr -d "\#\:\!"`
filepath=`dirname "${item}"`
target=""
kind=""
playback_alert=""
nsav=""
A="" # Output from hexdump
wmvtype=""
wmv_drm="0"
http_status=""
file_size_before=""
file_size_after=""
file_size=""
ffmpeg_info=""
ffmpeg_fps=""
ffmpeg_pixels=""
ffmpeg_video=""
steg=""
name_count="100" # This is to get the Finder to sort them correctly as opposed to "natural" sort order
item_created=""
}

####################################
remove_previous_leader_from_filename() {
# remove our previous leader from the filename
# This needs some cleanup to remove lines that reference leader formats that can't possibly occur anymore
just_the_filename="${just_the_filename/#*\RealVideo-/}"
just_the_filename="${just_the_filename/#*\RealAudio-/}"
just_the_filename="${just_the_filename/#*\RealMedia-/}"
just_the_filename="${just_the_filename/#*\fps_??h??m-/}"
just_the_filename="${just_the_filename/#*\fps_??h?m-/}"
just_the_filename="${just_the_filename/#*\fps_??h??m-DRM_/}"
just_the_filename="${just_the_filename/#*\fps_??h?m-DRM_/}"
just_the_filename="${just_the_filename/#*\fps-DRM_/}"
just_the_filename="${just_the_filename/#*\fps-/}"
just_the_filename="${just_the_filename/#mov_MPEG1-Muxed-/}"
just_the_filename="${just_the_filename/#mov_MPEG1-Muxed_/}"
just_the_filename="${just_the_filename/#asf_????_DRM_/}"
just_the_filename="${just_the_filename/#asf_????_/}"
just_the_filename="${just_the_filename/#avi_????????_/}"
just_the_filename="${just_the_filename/#avi_???????_/}"
just_the_filename="${just_the_filename/#avi_??????_/}"
just_the_filename="${just_the_filename/#avi_????-/}"
just_the_filename="${just_the_filename/#avi_????_/}"
just_the_filename="${just_the_filename/#avi_???_/}"
just_the_filename="${just_the_filename/#mov_????_/}"
just_the_filename="${just_the_filename/#mov_???_/}"
just_the_filename="${just_the_filename/#asf_/}"
just_the_filename="${just_the_filename/#avi_/}"
just_the_filename="${just_the_filename/#mov-/}"
just_the_filename="${just_the_filename/#mov_/}"
just_the_filename="${just_the_filename/#mpeg4-/}"
just_the_filename="${just_the_filename/#mpeg1_/}"
just_the_filename="${just_the_filename/#mpeg-ps_/}"
just_the_filename="${just_the_filename/#CDXA-/}"
just_the_filename="${just_the_filename/#jpeg__/}"
just_the_filename="${just_the_filename/#jpeg-/}"
just_the_filename="${just_the_filename/#jpeg_/}"
just_the_filename="${just_the_filename/#pdf_/}"
just_the_filename="${just_the_filename/#gif_/}"
just_the_filename="${just_the_filename/#p7_/}"
just_the_filename="${just_the_filename/#pdf-/}"
just_the_filename="${just_the_filename/#gif-/}"
just_the_filename="${just_the_filename/#p7-/}"
### Strip certain leading characters from the front of the filename
just_the_filename="${just_the_filename//#\ /}"
just_the_filename="${just_the_filename//#\_/}"
just_the_filename="${just_the_filename//#\!/}"
just_the_filename="${just_the_filename//#\./}"
### replace space-dash and dash-space with just a dash
just_the_filename="${just_the_filename//\ \-/-}"
just_the_filename="${just_the_filename//\-\ /-}"
}

####################################
check_video_for_avi() {
# file <filename> displays RIFF, Endian and AVI
if [ "${filetype}" = "none" ]; then
 if [ "${A:0:9}" = "5249 4646" -o "${A:0:9}" = "5249 4658" ] && [ "${A:20:7}" = "4156 49" -o "${A:20:9}" = "4344 5841" ]; then ### RIFF RIFX & AVI CDXA
  filetype="avi"
  extension="avi"

  avivids="${A:280:9}"
  avivids2="${A:470:9}"

  if [ "${avivids2}" = "f000 0000" ]; then
   avivids2="${A:478:9}"
  fi

  avividsascii=`echo -e "\\x${avivids:0:2}\\x${avivids:2:2}\\x${avivids:5:2}\\x${avivids:7:2}" | tr "[:upper:]" "[:lower:]"`
  avivids2ascii=`echo -e "\\x${avivids2:0:2}\\x${avivids2:2:2}\\x${avivids2:5:2}\\x${avivids2:7:2}" | tr "[:upper:]" "[:lower:]"`

  videotype="${avivids2ascii}"
  [ "${avividsascii}" != "${avivids2ascii}" ] && videotype="${avividsascii}.${avivids2ascii}"
  videotype=`echo "${videotype}" | tr -c -d "[:alnum:]"`

  if [ "${videotype:0:2}" = "iv" ]; then
   [ "${videotype:0:3}" = "iv5" ] && playback_alert="Use QuickTime Player Classic version & Indeo Video 5 CoDec"
   [ "${videotype:0:3}" = "iv4" ] && playback_alert="Use QuickTime Player Classic version & Indeo Video 4 CoDec"
   [ "${videotype:0:3}" = "iv3" ] && playback_alert="Use MPlayer OS X or QuickTime Player Classic version & Indeo Video 3 CoDec"
   # Note that the SetFile command is dependant on Developer Tools or XCode, it would be better to do with osascript if possible
   [ -n "${SetFile}" ] && "${SetFile}" -c "TVOD" -t "VfW " "${item}" # Remove the filename extension and it will open in QuickTime Player Classic
  fi

  if [ -z "${videotype}" ]; then
   # It appears that just changing the avi extension to mpg allows playback in QuickTime Player but its still a RIFF
   [ "${A:20:9}" = "4344 5841" ] && videotype="CDXA" && playback_alert="Use MPEG Streamclip to Convert to MPEG to extract from this avi wrapper file."
  fi

  [ -n "${ffmpeg}" ] && get_more_info

 fi
fi
}

####################################
check_for_rtf() {
if [ "${filetype}" = "none" ]; then ### Don't check it if it was already identified
 if [ "${A:0:12}" = "7B5C 7274 66" ]; then # {\rtf
  filetype="rtf"
  extension="rtf"
  videotype="none"
 fi
fi
}

####################################
check_for_jpeg() {
if [ "${filetype}" = "none" ]; then ### Don't check it if it was already identified
 if [ "${A:0:9}" = "ffd8 ffe1" -o "${A:0:9}" = "ffd8 ffe0" ]; then
  filetype="jpeg"
  extension="jpg"
  videotype="none"
  [ -n "${stegdetect}" ] && check_for_steganography
 fi
fi
}

#################################### Not sure if this info is definitive for mp3 so this routine is unused right now
check_for_mp3() {
if [ "${filetype}" = "none" ]; then ### Don't check it if it was already identified
 if [ "${A:0:7}" = "4944 3303" ]; then
  filetype="mp3" # creator hook (iTunes) type MPG3
  extension="mp3"
  videotype="none"
 fi
fi
}

####################################
check_for_P7() {
if [ "${filetype}" = "none" ]; then ### Don't check it if it was already identified
 if [ "${A:0:14}" = "5037 2033 3332" ]; then
  filetype="P7"
  extension="P7"
  videotype="none"
  playback_alert="XV Visual Schnauzer thumbnail file, not really worth keeping."
 fi
fi
}

####################################
check_for_pdf() {
if [ "${filetype}" = "none" ]; then ### Don't check it if it was already identified
 if [ "${A:0:9}" = "2550 4446" ]; then
  filetype="pdf" # creator CARO is Acrobat Reader
  extension="pdf"
  videotype="none"
 fi
fi
}

####################################
check_for_gif() {
if [ "${filetype}" = "none" ]; then ### Don't check it if it was already identified
 if [ "${A:0:9}" = "4749 4638" ]; then # GIF8
  filetype="gif"
  extension="gif"
  videotype="none"
 fi
fi
}

####################################
check_for_png() {
if [ "${filetype}" = "none" ]; then ### Don't check it if it was already identified
 if [ "${A:0:9}" = "8950 4e47" ]; then # PNG
  filetype="png"
  extension="png"
  videotype="none"
 fi
fi
}

####################################
check_for_bmp() {
if [ "${filetype}" = "none" ]; then ### Don't check it if it was already identified
 if [ "${A:0:4}" = "424d" ]; then # BM
  filetype="bmp"
  extension="bmp"
  videotype="none"
 fi
fi
}

####################################
check_for_Photoshop() {
if [ "${filetype}" = "none" ]; then ### Don't check it if it was already identified
 if [ "${A:0:4}" = "3842 5053" ]; then # 8BPS
  filetype="psd"
  extension="psd"
  videotype="none"
 fi
fi
}

####################################
check_video_for_asf_or_wmv() {
# file <filename> displays Microsoft ASF
if [ "${filetype}" = "none" ]; then ### Don't check it if it was already identified

 if [ "${A:0:39}" = "3026 b275 8e66 cf11 a6d9 00aa 0062 ce6c" ]; then # ASX matches this too - any way to differentiate?
  filetype="asf"
  extension="wmv"
  echo " Checking asf/wmv file for video type"
# Strings is slow on large files, change to bsed and set m-1-1 for min 1 max 1 matches (assuming bsed stops looking)
  wmvtype=`strings "${item}" | grep "WMV1\|WMV2\|WMV3\|MP43\|M4S2"`
  videotype=`echo "${wmvtype:0:4}" | head -n 1`

# Added the \ here
  if [ "${A:475:9}" = "4452\
4d00" ]; then
   just_the_filename="DRM_${just_the_filename}"
   playback_alert="REQUIRES Microsoft Windows OS and the license key to decrypt the content."
   wmv_drm="1"
  fi

  if [ -z "${playback_alert}" ]; then
   drm=`strings -32 "${item}" | grep "http://www.microsoft.com/isapi/redir.dll\|vidlock"`

   if [ "${drm:0:40}" = "http://www.microsoft.com/isapi/redir.dll" ] && [ -z "${playback_alert}" ]; then
    just_the_filename="DRM_${just_the_filename}"
    playback_alert="REQUIRES Microsoft Windows OS and the license key to decrypt the content."
    wmv_drm="1"
   fi

   if [[ "${drm}" = *"vidlock"* ]] && [ -z "${playback_alert}" ]; then
    just_the_filename="DRM_${just_the_filename}"
    playback_alert="REQUIRES Microsoft Windows OS and the license key to decrypt the content."
    wmv_drm="1"
   fi

  fi

  case "M4S2" in
   # Note that M4S2 asf/wmv files will play in MPlayer on OS X but NOT in Windows Media Player 9
   "${videotype}" ) videotype="M4S2"; extension="asf"; [ "${playback_alert}" = "" ] && playback_alert="Use MPlayer.";;
  esac

  case "MP43" in
   "${videotype}" ) videotype="MP43"; extension="asf";;
  esac

  case "WMV1" in
   "${videotype}" ) videotype="WMV1"; extension="asf"; [ "${playback_alert}" = "" ] && playback_alert="Use Windows Media Player, VLC or MPlayer.";;
  esac

  case "WMV2" in
   "${videotype}" ) videotype="WMV2"; extension="asf"; [ "${playback_alert}" = "" ] && playback_alert="Use Windows Media Player, VLC or MPlayer.";;
  esac

  case "WMV3" in
   "${videotype}" ) videotype="WMV3"; extension="wmv"; [ "${playback_alert}" = "" ] && playback_alert="Use Windows Media Player.";;
  esac

  if [ "${filetype}" = "asf" -o "${filetype}" = "wmv" ] && [ "${wmv_drm}" != "1" ]; then
   [ -n "${bsed}" ] && check_for_http
   # 68007400740070003A002F002F00 = h t t p : / / for asf/wmv files but these are the media addresses... would cripple streams
  fi

  [ "${filetype}" = "asf" -o "${filetype}" = "wmv" ] &&  [ -n "${ffmpeg}" ] && get_more_info

 fi
fi
}

####################################
check_video_for_RealMedia() {
if [ "${filetype}" = "none" ]; then ### Don't check it if it was already identified
 if [ "${A:0:9}" = "2e52 4d46" ]; then # .RMF
  filetype="RealMedia" # creator PNst
  extension="rm"
  playback_alert="Use RealPlayer / RealONE Player."
   [ -n "${ffmpeg}" ] && get_more_info
  #[ -n "${bsed}" ] && check_for_http # hmmm, might cripple streams?
 fi
fi
}

####################################
check_for_RealAudio() {
if [ "${filetype}" = "none" ]; then ### Don't check it if it was already identified
 if [ "${A:0:9}" = "2e72 61fd" ]; then # .ra.
  filetype="RealAudio" # creator PNst
  extension="ra"
  playback_alert="Use RealPlayer / RealONE Player."
   [ -n "${ffmpeg}" ] && get_more_info
  #[ -n "${bsed}" ] && check_for_http # hmmm, might cripple streams?
 fi
fi
}

####################################
check_video_for_mpeg() {
# file <filename> displays MPEG System Stream data for MPEG-PS and MPEG Video stream data for MPEG1
if [ "${filetype}" = "none" ]; then ### Don't check it if it was already identified
 if [ "${A:0:9}" = "0000 01ba" ]; then
  filetype="mpeg-ps" # MPEG program stream file
  extension="mpg"
  videotype="mpeg-ps"
   [ -n "${ffmpeg}" ] && get_more_info
 fi

 if [ "${A:0:9}" = "0000 01b3" ]; then
  filetype="mpeg1" # MPEG video stream
  extension="mpg"
  videotype="mpeg1"
  playback_alert="MPEG Streamclip can Convert to MPEG to enclose this inside an MPEG Program Stream file for maximum compatibility."
  [ -n "${ffmpeg}" ] && get_more_info
 fi

 if [ "${A:0:9}" = "4740 0010" ]; then
  filetype="mpeg-ts" # MPEG transport stream file
  extension="mpg"
  videotype="mpeg-ts"
  [ -n "${ffmpeg}" ] && get_more_info
 fi
fi
}

####################################
check_video_for_QuickTime_moov() {
if [ "${filetype}" = "none" ]; then ### Don't check it if it was already identified

# added the \ here
 if [ "${A:0:79}" = "0000 0020 6674 7970 7174 2020 2005 0300\
7174 2020 0000 0000 0000 0000 0000 0000" ]; then
  # 20 6674 7970 7174 2020 20 = " ftypqt   "
  filetype="mov"
  extension="mov"
 fi

 if [ "${A:0:29}" = "0000 001c 6674 7970 4d53 4e56" ]; then
  filetype="mov"
  extension="mov"
  videotype="mpeg4"
 fi

 if [ "${A:0:39}" = "0000 001c 6674 7970 4d53 4e56 0100 002c" ]; then # ftypMSNV 350 length 9 6d70 3476=mp4v
  filetype="mov"
  extension="mp4"
  [ "${A:350:9}" = "6d70 3476" ] && videotype="mp4v"
  playback_alert="Some MSNV MP4 files will only play in QuickTime if the filename extension is mp4."
  [ -n "${ffmpeg}" ] && get_more_info
  return 0 # now exit this function to avoid checking for nsav or urls since apparently this is not a normal mov
 fi

 if [ "${A:10:9}" = "6d64 6174" ]; then ### mdat
  filetype="mov"
  extension="mov"
  videotype="cvid"
 fi

 if [ "${A:0:39}" = "0000 63fc 6672 6565 0000 006c 6d76 6864" -o "${A:0:39}" = "0000 27fe 6672 6565 0000 006c 6d76 6864" ]; then
  filetype="mov"
  extension="mov"
  videotype="svq3" # Really?
 fi

 if [ "${A:0:39}" = "0001 45ea 6672 6565 0000 006c 6d76 6864" ]; then
  filetype="mov"
  extension="mov"
  videotype="mpeg4"
 fi

 case "${A:0:39}" in
 "0000 051d 6d6f 6f76 0000 006c 6d76 6864" | "0000 043e 6672 6565 0000 006c 6d76 6864" | "0000 0483 6d6f 6f76 0000 006c 6d76 6864" | "0000 052b 6d6f 6f76 0000 006c 6d76 6864" | "0000 04e6 6672 6565 0000 006c 6d76 6864" | "0000 051d 6672 6565 0000 006c 6d76 6864" | "0000 073d 6672 6565 0000 006c 6d76 6864" | "0000 052b 6d6f 6f76 0000 006c 6d76 6864" )
  filetype="mov"
  extension="mov"
  videotype="MPEG1-Muxed"
  playback_alert="Use FileJuicer to extract the MPEG from this QuickTime mov wrapper file."
 ;;
 esac

 if [ "${filetype}" = "mov" ] && [ "${A:160:9}" = "636d 6f76" ]; then
  videotype="cmov"
 fi

 if [ "${filetype}" = "mov" ] && [ "${A:110:9}" = "636d 6f76" ]; then
  videotype="cmov"
 fi

 if [ "${A:90:9}" = "6d6f 6f76" ]; then # moov
  filetype="mov"
  extension="mov"
 fi

 if [ "${A:10:9}" = "6d6f 6f76" ]; then # moov
  filetype="mov"
  extension="mov"
  if [ "${A:30:9}" = "636d 6f76" ]; then # cmov
   videotype="cmov"
  fi
 fi

 if [ "${A:10:19}" = "6674 7970 7174 2020" ]; then
  filetype="mov"
  extension="mov"
  if [ "${A:30:9}" = "636d 6f76" ]; then
   videotype="cmov"
  fi
 fi

 if [ "${filetype}" = "mov" ]; then
  [ -n "${bsed}" ] && check_for_http
  [ -n "${ffmpeg}" ] && get_more_info
  [ "${videotype}" != "cmov" ] && [ -n "${bsed}" ] && check_for_nsav
  [ "${videotype}" != "cmov" ] && set_mov_features
 fi

fi
}

####################################
check_for_nsav() {
# This routine is dependant on bsed and disables the nsav flag in uncompressed mov data
echo -n " Checking for & unlocking nsav: "
bsed_output=`"${bsed}" "6E73617600000001=6E73617600000000" "${item}" "${item}_nsav_UNLOCKED.tmp" 2>&1`
echo "${bsed_output}"
if [ "${bsed_output}" != "0 matches" ]; then
 file_size_before=`ls -al "${item}" | tr -s "\ " | cut -d " " -f 5`
 file_size_after=`ls -al "${item}_nsav_UNLOCKED.tmp" | tr -s "\ " | cut -d " " -f 5`
 if [ "${file_size_before}" == "${file_size_after}" ]; then
  mv "${item}_nsav_UNLOCKED.tmp" "${item}"
  else
  echo " AFTER DISABLING THE NSAV FLAG THE FILE SIZE HAS CHANGED. It was ${file_size_before} and is ${file_size_after}"
  #rm "${item}_nsav_UNLOCKED.tmp"
 fi
 else # if bsed_output is equal to 0 matches then just delete the temp file
  rm "${item}_nsav_UNLOCKED.tmp"
fi
}

####################################
check_for_http() {
# This routine is dependant on bsed and changes the http:// string to "null://" to prevent opening web pages.
if [ "${filetype}" != "none" ]; then
 echo -n " Checking for URLs: "
 bsed_output=`"${bsed}" "http:\\"="null:\\" "${item}" "${item}_deHTTP.tmp" 2>&1`
 echo "${bsed_output}"
 if [ "${bsed_output}" != "0 matches" ]; then
  # 68007400740070003A002F002F00 is h t t p : / / for asf/wmv file media/stream addresses
  file_size_before=`ls -al "${item}" | tr -s "\ " | cut -d " " -f 5`
  file_size_after=`ls -al "${item}_deHTTP.tmp" | tr -s "\ " | cut -d " " -f 5`
  if [ "${file_size_before}" == "${file_size_after}" ]; then
   mv "${item}_deHTTP.tmp" "${item}"
  else
   echo " AFTER NULLIFYING URL THE FILE SIZE HAS CHANGED. It was ${file_size_before} and is ${file_size_after}"
   #rm "${item}_deHTTP.tmp"
  fi
 else # if bsed_output is equal to 0 matches then just delete the temp file
  rm "${item}_deHTTP.tmp"
 fi
fi
}

####################################
set_mov_features() {
# Note that the call goes out to the first QuickTime Player in the Applications folder so rename older versions
echo " Calling QuickTime Player to disable auto-play and extra tracks."
osascript -e 'tell application "QuickTime Player"' -e "set show welcome movie automatically to false" -e "close every movie" -e "set play movie from beginning when opened to true" -e "set ignore auto play to true" -e "set ignore auto present to true" -e "set only front movie plays sound to true" -e "set open movie in new player to true" -e "end tell"

open -a "QuickTime Player" "${item}"

osascript -e 'tell application "QuickTime Player"' -e "set current time of front movie to 0" -e "set the scale of front movie to normal" -e "set the enabled of (every track of movie 1) to false" -e "set the enabled of (every track of movie 1 whose kind is \"Sound\") to true" -e "set the enabled of (every track of movie 1 whose kind is \"Sound 1\") to true" -e "set the enabled of (every track of movie 1 whose kind is \"Video\") to true" -e "set the enabled of (every track of movie 1 whose kind is \"Video 1\") to true" -e "set the enabled of (every track of movie 1 whose kind is \"Mpeg1 Muxed\") to true" -e "set the auto play of movie 1 to false" -e "set the auto present of movie 1 to false" -e "set the looping of movie 1 to false" -e "set the auto close when done of movie 1 to false" -e "set the auto quit when done of movie 1 to false" -e "set the controller type of movie 1 to standard" -e "set the saveable of (every movie) to true" -e "save (movie 1)" -e "close window 1" -e "end tell"
}

####################################
get_more_info() {
if [ "${filetype}" != "none" ]; then

 ffmpeg_video_info=`"${ffmpeg}" -i "${item}" 2>&1`

 ffmpeg_video=`echo "${ffmpeg_video_info}" | grep "Video: " | tr -s "\ " | cut -d " " -f 7` # mpeg1video,
 ffmpeg_video=`echo "${ffmpeg_video/video/}" | tr "[:upper:]" "[:lower:]"`
 videotype_lower=`echo "${videotype}" | tr "[:upper:]" "[:lower:]"`
 ffmpeg_video="${ffmpeg_video/\,/}"
 [ "${ffmpeg_video}" = "${videotype_lower}" ] && ffmpeg_video=""

 ffmpeg_pixels=`echo "${ffmpeg_video_info}" | grep "Video: " | tr -s "\ " | cut -d " " -f 9` # 720x480,
 [[ "${ffmpeg_pixels}" == "0x"* ]] && ffmpeg_pixels=""

 ffmpeg_fps=`echo "${ffmpeg_video_info}" | grep "Video: " | tr -s "\ " | cut -d " " -f 4` # 29.97
 ffmpeg_fps="${ffmpeg_fps:0:2}"
 ffmpeg_fps="${ffmpeg_fps%.*}"

 #ffmpeg_audio=`"echo "${ffmpeg_video_info}" | grep "Audio: " | tr -s "\ " | cut -d " " -f 5` # mp2,

 # ffmpeg_duration=`echo "${ffmpeg_video_info}" | grep "Duration: " | cut -d "," -f 1 | tr -s "\ " | cut -d " " -f 3 | cut -d "." -f 1` # 00:02:13
 # Ran into a little trouble with the minutes, this needs to be cleaned up.
 # if [ -n "${ffmpeg_duration}" ]; then
 #  ffmpeg_hours="${ffmpeg_duration:0:2}"
 #   ffmpeg_minutes="${ffmpeg_duration:3:2}"
 #  if [ "${ffmpeg_duration:6:2}" -gt "30" ]; then
 #   [ "${ffmpeg_minutes:0:1}" = "0" ] && ffmpeg_minutes="${ffmpeg_minutes:1}"
 #   let ffmpeg_minutes+=1
 #   [ "${ffmpeg_minutes}" -gt "60" ] && let ffmpeg_hours+=1 && let ffmpeg_minutes-=60
 #   [ "${#ffmpeg_minutes}" -lt "2" ] && ffmpeg_minutes="0${ffmpeg_minutes}"
 #  fi
 #  ffmpeg_duration=""
 #  [ "${ffmpeg_hours}" != "00" ]   && ffmpeg_duration="${ffmpeg_hours}h"
 #  [ "${ffmpeg_minutes}" != "00" ] && ffmpeg_duration="${ffmpeg_duration}${ffmpeg_minutes}m"
 # fi

 [ -n "${ffmpeg_video}" ]       && ffmpeg_info="${ffmpeg_video}"
 [ -n "${ffmpeg_pixels}" ]      && ffmpeg_info="${ffmpeg_info}_${ffmpeg_pixels}"
 [ -n "${ffmpeg_fps}" ]         && ffmpeg_info="${ffmpeg_info}_${ffmpeg_fps}fps"
 # [ -n "${ffmpeg_duration}" ]  && ffmpeg_info="${ffmpeg_info}_${ffmpeg_duration}"
 [ -n "${ffmpeg_info}" ]        && ffmpeg_info="${ffmpeg_info}-"

 [ "${ffmpeg_info:0:1}" = "_" ] && ffmpeg_info="${ffmpeg_info:1}"
 just_the_filename="${ffmpeg_info}-${just_the_filename}"
fi
}

####################################
check_for_steganography() {
# check for steganography
echo -n " Checking for steganography. False positives are common..."
steg=`"${stegdetect}" -q "${item}"`
if [ -n "${steg}" ]; then
 echo "${steg##*\ }"
else
 echo "None detected."
fi
}

####################################
display_file_info() {
#[ -n "${filepath}" ] && echo "  Dirname: ${filepath}"
#echo " Filetype: ${filetype}   Videotype: ${videotype}   Extension: ${extension}"
[ -n "${playback_alert}" ] && echo " Playback: ${playback_alert}"
echo
echo "============================================================================
==="
}

####################################
clean_just_the_filename() {
# get rid of problematic characters and squeeze spaces, periods, hyphens and underscores
just_the_filename=`echo "${just_the_filename}" | tr -Cd "[:alnum:] _-.()" | tr -cd "[:print:]" | tr -s "\ \.\_\-"`
# since we are only working with the filename and there is no extension at this point, remove blank spaces at the end
just_the_filename=`echo "${just_the_filename}" | sed -e 's/[ ]*$//'`
}

####################################
generate_output_filename() {
[ "${filetype}" = "${videotype}" ] && filetype="none" # no sense naming the file mpeg1_mpeg1 for instance

if [ "${just_the_filename:0:1}" = "-" -o "${just_the_filename:0:1}" = "_" ]; then
 just_the_filename="${just_the_filename:1}"
fi

if [ "${filetype}" = "none" ] && [ "${videotype}" != "none" ]; then # if videotype is not none and filetype is none
 [ -z "${ffmpeg_info}" ] && target="${videotype}-${just_the_filename}"
 [ -n "${ffmpeg_info}" ] && target="${videotype}_${just_the_filename}"
fi

if [ "${videotype}" = "none" ] && [ "${filetype}" != "none" ]; then # if videotype is none and filetype is not none
  [ -z "${ffmpeg_info}" ] && target="${filetype}-${just_the_filename}"
  [ -n "${ffmpeg_info}" ] && target="${filetype}_${just_the_filename}"
fi

if [ "${videotype}" = "none" ] && [ "${filetype}" = "none" ]; then # if both videotype and filetype are none
 target="${just_the_filename}"
fi

if [ "${videotype}" != "none" ] && [ "${filetype}" != "none" ]; then # if both videotype and filetype are not none
  [ -z "${ffmpeg_info}" ] && target="${filetype}_${videotype}-${just_the_filename}"
  [ -n "${ffmpeg_info}" ] && target="${filetype}_${videotype}_${just_the_filename}"
fi

# Note that we have not added .${extension} yet
# If the new filename is longer than 255 characters then truncate to 250 which allows
# five character extensions such as ".mpeg"
if [ "${#target}" -gt 250 ]; then
 target="${target:0:250}"
fi
target="${target//_-/}" # one more time to try to clean _- from the name due to sloppiness
check_for_existing_target_filename
target="${filepath}/${target}.${extension}"
}

####################################
check_for_existing_target_filename() {
# check if name exists and append a number counting up if it does
if [ -e "${filepath}/${target}.${extension}" ] && [ "${filepath}/${target}.${extension}" != "${item}" ]; then
 target="${target%\_#*}"
 [ "${#target}" -gt 245 ] && target="${target:0:245}" # 245 should accomodate adding _#999.mpeg for instance and still fit a 255 chr filename
 target="${target}_#${name_count}"
 let name_count=name_count+1
 check_for_existing_target_filename
fi
}

####################################
process_folder() {
if [ $# = 0 ] && [ -z "${source_folder}" ]; then
 select_source_folder
fi
[ ! -d "${source_folder}" ] && echo "Folder NOT FOUND" && exit 1 ### Die here if the specified directory is not a directory or not in existance
cd "${source_folder}" ### Put us in the specified directory thus making it the current directory
clear
echo "==================================videntify=================================
==="
[ -n "${bsed}" ]       && echo "      Found bsed: ${bsed}"
[ -z "${bsed}" ]       && echo "No bsed executable found, routines DISABLED. http://www1.bell-labs.com/cgi-user/wwexptools/gensnapshot?bsed"
[ -n "${ffmpeg}" ]     && echo "    Found ffmpeg: ${ffmpeg}"
[ -z "${ffmpeg}" ]     && echo "No ffmpeg executable found, routines DISABLED. http://homepage.mac.com/major4/"
[ -n "${stegdetect}" ] && echo "Found stegdetect: ${stegdetect}"
[ -z "${stegdetect}" ] && echo "No stegdetect executable found, routines DISABLED. http://www.outguess.org/detection.php"
echo
echo "DIRECTORY: "`pwd`
echo "============================================================================
==="
find "${source_folder}" -type f \! -name ".DS_Store" \! -name "." \! -name ".." | while read item; do

 item_created=`osascript -e 'tell application "System Events"' -e "get creation date of file \"$item\"" -e "end tell"`
 chflags nouchg "${item}" &>/dev/null # Unlock the Finder lock flag ignoring any feedback or errors
 declare_variables_for_this_file

 case "${extension}" in
  "mpg" | "mpeg" | "mp4" | "mpe" | "avi" | "asf" | "wmv" | "asx" | "mov" | "rm" | "ram" | "ra" | "divx" | "jpeg" | "jpg" | "pdf" | "p7" | "gif" | "bmp" | "png" | "psd" | "rtf" ) kind="known";;
 esac ### End of case extension indicates a movie

 file_size=`ls -al "${item}" | tr -s "\ " | cut -d " " -f 5`
 if [ "${kind}" = "known" ] && [ "${file_size}" -gt "1" ]; then

  remove_previous_leader_from_filename

  echo ">${filename}"
  A=`hexdump -vn 1000 "${item}" | colrm 1 8`

  # call the function to strip leading if the start of the file is padded with "00"s or "ff"s
  [ "${A:0:39}" = "0000 0000 0000 0000 0000 0000 0000 0000" -o "${A:0:39}" = "ffff ffff ffff ffff ffff ffff ffff ffff" ] && strip_leading

  [ "${filetype}" = "none" ] && check_for_jpeg
  [ "${filetype}" = "none" ] && check_for_P7
  [ "${filetype}" = "none" ] && check_for_pdf
  [ "${filetype}" = "none" ] && check_for_gif
  [ "${filetype}" = "none" ] && check_for_rtf
  [ "${filetype}" = "none" ] && check_for_png
  [ "${filetype}" = "none" ] && check_for_bmp
  [ "${filetype}" = "none" ] && check_for_Photoshop
  [ "${filetype}" = "none" ] && check_video_for_avi
  [ "${filetype}" = "none" ] && check_video_for_asf_or_wmv
  [ "${filetype}" = "none" ] && check_video_for_RealMedia
  [ "${filetype}" = "none" ] && check_for_RealAudio
  [ "${filetype}" = "none" ] && check_video_for_mpeg
  [ "${filetype}" = "none" ] && check_video_for_QuickTime_moov

  [ -n "${playback_alert}" ] && echo " Playback: ${playback_alert}" # since display_file_info is disabled above

  clean_just_the_filename
  generate_output_filename

  # only rename if the name is different than the original file
  if [ "${target}" != "${item}" ]; then
   # Actually renaming the file may fail if we do not have write access
   [ ! -e "${target}" ] && mv "${item}" "${target}" ### Only move the file if the new name does NOT already exist
  fi

  echo "<"`basename "${target}"`
  echo "--------------------------------------------------------------------------------"
 fi ### end of if kind is known
done ### end of reading filenames from folder
}

####################################
find_bsed () {
# Find the bsed executable
bsed="${script_folder}/bsed"
[ ! -f "${bsed}" -o ! -x "${bsed}" ] && bsed=`which bsed`
if [ -z "${bsed}" ]; then
 candidates=`locate bsed |  grep "/bsed$"`
 while [ ! -x "${bsed}" ] && [ ! -f "${bsed}" ]; do
  candidate=`echo "${candidates}" | head -n 1`
  candidates="${candidates:((${#candidate}+1))}"
  [ -x "${candidate}" ] && [ -f "${candidate}" ] && bsed="${candidate}"
 done
fi
}

####################################
find_ffmpeg () {
# Find the ffmpeg executable
ffmpeg="${script_folder}/ffmpeg"
[ ! -f "${ffmpeg}" -o ! -x "${ffmpeg}" ] && ffmpeg=`which ffmpeg`
if [ -z "${ffmpeg}" ]; then
 candidates=`locate ffmpeg |  grep "/ffmpeg$"`
 while [ ! -x "${ffmpeg}" ] && [ ! -f "${ffmpeg}" ]; do
  candidate=`echo "${candidates}" | head -n 1`
  candidates="${candidates:((${#candidate}+1))}"
  # Older versions of ffmpeg don't have the same info output format so we'll try to eliminate them
  candidate_version=`"${candidate}" "-version" 2>&1 | grep -v "unrecognized"`
  [ -x "${candidate}" ] && [ -f "${candidate}" ] && [ -n "${candidate_version}" ] && ffmpeg="${candidate}"
 done
fi
}

####################################
find_stegdetect () {
# Find the stegdetect executable
stegdetect="${script_folder}/stegdetect"
[ ! -f "${stegdetect}" -o ! -x "${stegdetect}" ] && stegdetect=`which stegdetect`
if [ -z "${stegdetect}" ]; then
 candidates=`locate stegdetect |  grep "/stegdetect$"`
 while [ ! -x "${stegdetect}" ] && [ ! -f "${stegdetect}" ]; do
  candidate=`echo "${candidates}" | head -n 1`
  candidates="${candidates:((${#candidate}+1))}"
  [ -x "${candidate}" ] && [ -f "${candidate}" ] && stegdetect="${candidate}"
 done
fi
}

####################################
find_SetFile() {
# Find the SetFile executable which comes with Developer Tools or XCode
[ -x /Developer/tools/SetFile ] && SetFile="/Developer/tools/SetFile"
}

####################################
find_GetFileInfo() {
# Find the GetFileInfo executable which comes with Developer Tools or XCode
[ -x /Developer/tools/GetFileInfo ] && GetFileInfo="/Developer/tools/GetFileInfo"
}

#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# End of Functions (Subroutines)



#vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
# Variables (see also function to set variables for each file)
bsed=""
ffmpeg=""
stegdetect=""
SetFile=""
GetFileInfo=""
candidate=""
candidates=""
candidate_version=""
item=""
file_size="0"
source_folder=""
script_folder=`dirname "${0}"`
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# End of variables



#vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
# Main
osascript -e 'tell application "Terminal" to set the size of the window frontmost to {1065, 450}'
source_folder="${1}"

if [ $# = 0 ]; then
greeting
fi

[ ! -x "${bsed}" ]        && find_bsed
[ ! -x "${ffmpeg}" ]      && find_ffmpeg
[ ! -x "${stegdetect}" ]  && find_stegdetect
[ ! -x "${SetFile}" ]     && find_SetFile
[ ! -x "${GetFileInfo}" ] && find_GetFileInfo
process_folder

if [ -z "${1}" ]; then # If no folder was specified on the command line then start over asking for another folder
source_folder=""
process_folder
fi

echo "============================================================================
==="

exit 0
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# End of Main


#8 callmenames

callmenames
  • Members
  • 1,426 posts

Posted 09 January 2006 - 01:54 PM

Oops, I included the wrong ffmpeg binary in the last zip file. Here is the link to the corrected one.

http://rapidshare.de...y_1.4B.zip.html

#9 callmenames

callmenames
  • Members
  • 1,426 posts

Posted 10 January 2006 - 05:47 PM

videntify 1.5 script only with lots of changes, clean up and optimization

Download it here or copy from the post below but beware some lines may be mangled by the forum post process here.
http://rapidshare.de...ommand.zip.html

[code=auto:0]#!/bin/bash
#
#vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
# Information
#
# videntify 1.5 2006-01-08
# Released into the public domain by the author.
# Feel free to modify or swipe any part of this, no credit required.
# Please share your changes and additions too.


# USAGE
# ./videntify.command <path to folder of video files>
# or double click and either type a path or drag a folder to the terminal window


# DESCRIPTION
# A Mac OS X BASH script to help identify the file format and video type of video files.
# Intended to be used on quantities of downloaded videos to quickly identify those that will and won't
# play using various OS X media players.


# WARNING
# Uncompressed QuickTime Movies will open on the screen (to disable extra tracks and autoplay)
#
# This script -will- rename the video files, prepending file format and video type if known and may
# change the filename extension to reflect the actual file format (for instance an avi file titled
# video.divx with Indeo Video 5 compression would be changed to avi_iv50_video.avi)
#
# Some routines output files to a new file and then replace the orignal which effectively removes Finder color labels


# DEPENDANCIES
# bsed - used to disable the nsav (no-save) flag in uncompressed QuickTime movie data
#        also used to disable web-page addresses in QuickTime mov files (changes http:// to null://)
# [url=http://www1.bell-labs.com/cgi-user/wwexptools/gensnapshot?bsed]http://www1.bell-labs.com/cgi-user/wwexpto...ensnapshot?bsed[/url]
#
# stegdetect - to detect steganography within jpeg images
# [url=http://www.outguess.org/detection.php]http://www.outguess.org/detection.php[/url]
#
# ffmpeg - to get more info on video files. The pre-compiled binary in the ffmpegX application package works well.
# Currently that one is...
# ffmpeg version CVS, build 3277056, Copyright © 2000-2004 Fabrice Bellard - Mac OSX static build for ffmpegX
# It seems to work much better than ffmpeg version 0.4.8 from darwinports or the one included with D-Vision
# [url=http://homepage.mac.com/major4/]http://homepage.mac.com/major4/[/url]
#
# QuickTime Player - to disabled auto-play and extra tracks in QuickTime mov files.
# [url=http://www.apple.com/quicktime/]http://www.apple.com/quicktime/[/url]
#
# Optional (if you don't have them then the script won't set type and creator codes, not a great loss.)
# GetFileInfo and SetFile from Apple's free Developer's Tools or XCode which hopefully came with your Mac
# [url=http://developer.apple.com/tools/]http://developer.apple.com/tools/[/url]


# RECOMMENDATIONS
# Use the freeware IconCleaner (gui) to strip custom icons from the files before running DupSeek (see next)
# [url=http://www.ziksw.com/software/iconcleaner.html]http://www.ziksw.com/software/iconcleaner.html[/url]
#
# Use the freeware DupSeek (cli) to eliminate duplicate files (based on their content) before running this script
# [url=http://www.beautylabs.net/software/dupseek.html]http://www.beautylabs.net/software/dupseek.html[/url]
#
# Download MPlayer OSX, VLC (VideoLAN Client), Windows Media Player and RealPlayer and the DIVX and 3IVX CoDecs.
# They can be found using macupdate.com or versiontracker.com
#
# MPlayer OSX (the official MPlayer)
# This is version 1.0pre8 of MPlayer OSX which includes the binary MPlayer dev-CVS-050904-08:02-4.0.0
# [url=http://mplayerhq.hu/homepage/design7/dload.html]http://mplayerhq.hu/homepage/design7/dload.html[/url]
#
# MPlayer OS X 2 (the buggy one)
# 2b8r5 requires Tiger, 2b8R4 requires 10.3+
# Version 2B8r4 includes the binary MPlayer dev-CVS-041212-12:40-3.3
# [url=http://sourceforge.net/project/showfiles.php?group_id=62947]http://sourceforge.net/project/showfiles.php?group_id=62947[/url]
#
# djoPlayer (small player with few features)
# Version 3.5.1 includes the binary MPlayer dev-CVS-050123-19:55-3.3
# [url=http://homepage.mac.com/djodjodesign/index2.html]http://homepage.mac.com/djodjodesign/index2.html[/url]
#
# VLC (VideoLan Client)
# [url=http://www.videolan.org/vlc/download-macosx.html]http://www.videolan.org/vlc/download-macosx.html[/url]
#
# Windows Media Player for Mac
# [url=http://www.microsoft.com/mac/otherproducts/otherproducts.aspx?pid=windowsmedia]http://www.microsoft.com/mac/otherproducts...id=windowsmedia[/url]
#
# MPEG Streamclip
# It can "Fix Timecode Breaks" and then "Convert to MPEG" to save a fixed copy of problem mpeg videos.
# [url=http://www.alfanet.it/squared5/mpegstreamclip.html]http://www.alfanet.it/squared5/mpegstreamclip.html[/url]
#
# RealPlayer
# [url=http://www.real.com/mac]http://www.real.com/mac[/url]
#
# Set the application for each video filename extension to open with for simplified double-clicking
# EXAMPLE: select a file ending in .asf in the Finder, Get Info on the file, in the "Open With" section
# select the program with which to open the file such as "MPlayer OS X" and click the "Change All" button.
# Set .wmv files to open in Windows Media Player (this script uses the wmv extension for WMV3 videos)


# NOTES
# Whenever possible, avoid using QuickTime Player, Windows Media Player or RealPlayer as they shamelessly
# allow the media files to open browser windows, display things that interfere with the video etc.
#
# Windows Media Player is needed for asf / WMV3 non-DRM playback
#
# asf/wmv files that use DRM (Digital Rights Management) will only playback in Windows - VirtualPC works (slowly),
# install Windows Media Player 10 under Windows within VirtualPC. You must have the license key for the DRM file.
#
# Many of the RealVideo formats will play in VLC or MPlayer but not all.
#
# Many QuickTime formats will play in VLC or MPlayer, but again, not all.
#
# AVI, MOV, RM, ASF/WMV are file formats - they may contain data encoded with any of thousands of CoDecs
# and many will not play under OS X.
#
# Indeo Video 3 files play poorly in MPlayer, Indeo Video 3, 4, and 5 play well in the Classic QuickTime
# Player with the Indeo CoDecs installed. They also play in VirtualPC.
#
# The ToVid package includes a cli utility "IdVid" which calls the players themselves (MPlayer for instance) to
# report far more information about video files, it also takes much longer.
# [url=http://tovid.sourceforge.net/]http://tovid.sourceforge.net/[/url]


# IDEAS FOR ENHANCEMENTS TO THIS SCRIPT (please share your changes)
# Allow the script to work on a single file, list of files or directoris or a single directory instead of just a directory
# Allow "move to" location(s) to be specified, perhaps into folders by file format and then subfolders by video CoDec
# Basic checks such as for write access
# Optionally set permissions, owner / group and flags for each file processed (chflags nouchg already happens)
# Decompress QuickTime Movie Resource data to check for nsav in compressed movies and unlock
# Option to split-out joined mpeg files with timecode breaks using vcdgear
# Identify audio CoDecs as well
# Get file attributes and create date from the original file and preserve them on recreated files

#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# End of Information



#vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
# Functions (Subroutines)

####################################
greeting() {
  clear # note that the === line below is now 10 =s less than the default width of OS X's Terminal window
  echo "======================================================================"
  echo "Script title : videntify"
  echo "Script origin: [url=http://www.machacking.net/forums/index.php?showtopic=5339&hl="]http://www.machacking.net/forums/index.php...=5339&hl="[/url]
  echo
  echo " Attempts to identify media file format and set appropriate filename extension."
  echo " Attempts to identify video compression used in the media files if any."
  echo " Unlocks QuickTime Movies with uncompressed movie data atom."
  echo " Disables autoplay and extra tracks in unlocked QuickTime movies."
  echo " Disables URLs in QuickTime mov files and Windows Media Player asf/wmv files."
  echo " Checks for steganography in jpegs with stegdetect."
  echo " Cleans filenames for compatibility with some players."
  echo
  echo " You will be asked to supply the path(s) of files or folders to inspect."
  echo
  echo " You may either type in the path (bash expansion works) or drag-and-drop"
  echo " a file or folder into this window, click back in the window with the mouse and"
  echo " press the return key."
  echo
}

####################################
select_source() {
echo
echo "  =========================== Select source path ============================="
echo
echo "  The script can be cancelled by pressing Control-C or Command-period"
echo "  Please enter the path of a folder or media file for videntify to inspect."
echo -n "  Path: ";
read -e source_path
echo

if [ ! -e "${source_path}" ]; then
 echo "  There is no folder or file at that path. Please try again." && select_source
fi

if [ -d "${source_path}" ]; then
 source_folder="${source_path}"
fi

if [ -f "${source_path}" ]; then
 item="${source_path}"
fi
}


####################################
strip_leading() {
# strips 40 characters from the front of A, eliminates one entire line at a time from hexdump output
A="${A:40}"
[ "${A:0:39}" = "0000 0000 0000 0000 0000 0000 0000 0000" ] && strip_leading
[ "${A:0:39}" = "ffff ffff ffff ffff ffff ffff ffff ffff" ] && strip_leading
}

####################################
declare_variables_for_this_file() {
# These should be reset for each file
filetype="none"
videotype="none"
videotype_lower=""
avivids=""
avivids2=""
avividsascii=""
avivids2ascii=""
filename=`basename "${item}"`
extension="${filename##*.}"
extension=`echo "${extension}" | tr "[:upper:]" "[:lower:]"`
just_the_filename="${filename%.*}"
[ "${extension}" = "${just_the_filename}" ] && extension="unknown"
# squeeze duplicates of some chrs & cut some chrs out of filename
just_the_filename=`echo "${just_the_filename}" | tr -s "\ \-\_\.\(\)\[\]\{\}" | tr -d "\\\:\!"` # stopped killing # and added killing \
filepath=`dirname "${item}"`
target=""
kind=""
playback_alert=""
nsav=""
A="" # Output from hexdump
wmvtype=""
wmv_drm="0"
http_status=""
file_size_before=""
file_size_after=""
file_size=""
ffmpeg_video_info=""
ffmpeg_info=""
ffmpeg_fps=""
ffmpeg_pixels=""
ffmpeg_video=""
steg=""
name_count="100" # This is to get the Finder to sort them correctly
item_created=""
}

####################################
remove_previous_leader_from_filename() {
# remove our previous leader from the filename
# This needs some cleanup to remove lines that reference leader formats that can't possibly occur anymore
just_the_filename="${just_the_filename/#*\Real?????-/}"
just_the_filename="${just_the_filename/#*\fps_??h??m-/}"
just_the_filename="${just_the_filename/#*\fps_??h?m-/}"
just_the_filename="${just_the_filename/#*\fps_??h??m-DRM_/}"
just_the_filename="${just_the_filename/#*\fps_??h?m-DRM_/}"
just_the_filename="${just_the_filename/#*\fps-DRM_/}"
just_the_filename="${just_the_filename/#*\fps-/}"
just_the_filename="${just_the_filename/#mov_MPEG1-Muxed-/}"
just_the_filename="${just_the_filename/#mov_MPEG1-Muxed_/}"
just_the_filename="${just_the_filename/#asf_????_DRM_/}"
just_the_filename="${just_the_filename/#asf_????_/}"
just_the_filename="${just_the_filename/#avi_????????_/}"
just_the_filename="${just_the_filename/#avi_???????_/}"
just_the_filename="${just_the_filename/#avi_??????_/}"
just_the_filename="${just_the_filename/#avi_????-/}"
just_the_filename="${just_the_filename/#avi_????_/}"
just_the_filename="${just_the_filename/#avi_???_/}"
just_the_filename="${just_the_filename/#mov_????_/}"
just_the_filename="${just_the_filename/#mov_???_/}"
just_the_filename="${just_the_filename/#asf_/}"
just_the_filename="${just_the_filename/#avi_/}"
just_the_filename="${just_the_filename/#mov-/}"
just_the_filename="${just_the_filename/#mov_/}"
just_the_filename="${just_the_filename/#mpeg4-/}"
just_the_filename="${just_the_filename/#mpeg1_/}"
just_the_filename="${just_the_filename/#cmov-/}"
just_the_filename="${just_the_filename/#mpeg-ps_/}"
just_the_filename="${just_the_filename/#CDXA-/}"
just_the_filename="${just_the_filename/#jpeg__/}"
just_the_filename="${just_the_filename/#jpeg-/}"
just_the_filename="${just_the_filename/#mp4v-/}"
just_the_filename="${just_the_filename/#jpeg_/}"
just_the_filename="${just_the_filename/#pdf_/}"
just_the_filename="${just_the_filename/#gif_/}"
just_the_filename="${just_the_filename/#p7_/}"
just_the_filename="${just_the_filename/#pdf-/}"
just_the_filename="${just_the_filename/#gif-/}"
just_the_filename="${just_the_filename/#p7-/}"
# Strip certain leading characters from the front of the filename
just_the_filename="${just_the_filename//#\ /}"
just_the_filename="${just_the_filename//#\_/}"
just_the_filename="${just_the_filename//#\!/}"
just_the_filename="${just_the_filename//#\./}"
# replace space-dash and dash-space with just a dash
just_the_filename="${just_the_filename//\ \-/-}"
just_the_filename="${just_the_filename//\-\ /-}"
}

####################################
check_video_for_avi() {
# file <filename> displays RIFF, Endian and AVI
if [ "${filetype}" = "none" ]; then
 if [ "${A:0:9}" = "5249 4646" -o "${A:0:9}" = "5249 4658" ] && [ "${A:20:7}" = "4156 49" -o "${A:20:9}" = "4344 5841" ]; then # RIFF RIFX & AVI CDXA
  filetype="avi"
  extension="avi"

  avivids="${A:280:9}"
  avivids2="${A:470:9}"

  if [ "${avivids2}" = "f000 0000" ]; then
   avivids2="${A:478:9}"
  fi

  avividsascii=`echo -e "\\x${avivids:0:2}\\x${avivids:2:2}\\x${avivids:5:2}\\x${avivids:7:2}" | tr "[:upper:]" "[:lower:]"`
  avivids2ascii=`echo -e "\\x${avivids2:0:2}\\x${avivids2:2:2}\\x${avivids2:5:2}\\x${avivids2:7:2}" | tr "[:upper:]" "[:lower:]"`

  videotype="${avivids2ascii}"
  [ "${avividsascii}" != "${avivids2ascii}" ] && videotype="${avividsascii}.${avivids2ascii}"
  videotype=`echo "${videotype}" | tr -c -d "[:alnum:]"`

  if [ "${videotype:0:2}" = "iv" ]; then
   [ "${videotype:0:3}" = "iv5" ] && playback_alert="Use QuickTime Player Classic version & Indeo Video 5 CoDec"
   [ "${videotype:0:3}" = "iv4" ] && playback_alert="Use QuickTime Player Classic version & Indeo Video 4 CoDec"
   [ "${videotype:0:3}" = "iv3" ] && playback_alert="Use MPlayer OS X or QuickTime Player Classic version & Indeo Video 3 CoDec"
   # Note that the SetFile command is dependant on Developer Tools or XCode
   # it would be better to do with osascript if possible
   [ -n "${SetFile}" ] && "${SetFile}" -c "TVOD" -t "VfW " "${item}"
   # Remove the filename extension to get OS X to use the type and creator codes and thus open it in QuickTime Player Classic
  fi

  if [ -z "${videotype}" ]; then
   # It appears that just changing the avi extension to mpg allows playback in QuickTime Player but its still a RIFF
   [ "${A:20:9}" = "4344 5841" ] && videotype="CDXA" && playback_alert="Use MPEG Streamclip to Convert to MPEG to extract from this avi wrapper file."
  fi

  [ -n "${ffmpeg}" ] && get_more_info

 fi
fi
}

####################################
check_for_rtf() {
if [ "${filetype}" = "none" ]; then # Don't check it if it was already identified
 if [ "${A:0:12}" = "7B5C 7274 66" ]; then # {\rtf
  filetype="rtf"
  extension="rtf"
  videotype="none"
 fi
fi
}

####################################
check_for_jpeg() {
if [ "${filetype}" = "none" ]; then # Don't check it if it was already identified
 if [ "${A:0:9}" = "ffd8 ffe1" -o "${A:0:9}" = "ffd8 ffe0" ]; then
  filetype="jpeg"
  extension="jpg"
  videotype="none"
  [ -n "${stegdetect}" ] && check_for_steganography
 fi
fi
}

#################################### Not sure if this info is definitive for mp3 so this routine is unused right now
check_for_mp3() {
if [ "${filetype}" = "none" ]; then # Don't check it if it was already identified
 if [ "${A:0:7}" = "4944 3303" ]; then
  filetype="mp3" # creator hook (iTunes) type MPG3
  extension="mp3"
  videotype="none"
 fi
fi
}

####################################
check_for_P7() {
if [ "${filetype}" = "none" ]; then # Don't check it if it was already identified
 if [ "${A:0:14}" = "5037 2033 3332" ]; then
  filetype="P7"
  extension="P7"
  videotype="none"
  playback_alert="XV Visual Schnauzer thumbnail file, not really worth keeping."
 fi
fi
}

####################################
check_for_pdf() {
if [ "${filetype}" = "none" ]; then # Don't check it if it was already identified
 if [ "${A:0:9}" = "2550 4446" ]; then
  filetype="pdf" # creator CARO is Acrobat Reader
  extension="pdf"
  videotype="none"
 fi
fi
}

####################################
check_for_gif() {
if [ "${filetype}" = "none" ]; then # Don't check it if it was already identified
 if [ "${A:0:9}" = "4749 4638" ]; then # GIF8
  filetype="gif"
  extension="gif"
  videotype="none"
 fi
fi
}

####################################
check_for_png() {
if [ "${filetype}" = "none" ]; then # Don't check it if it was already identified
 if [ "${A:0:9}" = "8950 4e47" ]; then # PNG
  filetype="png"
  extension="png"
  videotype="none"
 fi
fi
}

####################################
check_for_bmp() {
if [ "${filetype}" = "none" ]; then # Don't check it if it was already identified
 if [ "${A:0:4}" = "424d" ]; then # BM
  filetype="bmp"
  extension="bmp"
  videotype="none"
 fi
fi
}

####################################
check_for_Photoshop() {
if [ "${filetype}" = "none" ]; then # Don't check it if it was already identified
 if [ "${A:0:4}" = "3842 5053" ]; then # 8BPS
  filetype="psd"
  extension="psd"
  videotype="none"
 fi
fi
}

####################################
check_video_for_asf_or_wmv() {
# file <filename> displays Microsoft ASF
if [ "${filetype}" = "none" ]; then # Don't check it if it was already identified

 if [ "${A:0:39}" = "3026 b275 8e66 cf11 a6d9 00aa 0062 ce6c" ]; then # ASX matches this too - any way to differentiate?
  filetype="asf"
  extension="wmv"
  echo " Checking asf/wmv file for video type"
# Strings is slow on large files, change to bsed and set m-1-1 for min 1 max 1 matches (assuming bsed stops looking)
  wmvtype=`strings "${item}" | grep "WMV1\|WMV2\|WMV3\|MP43\|M4S2"`
  videotype=`echo "${wmvtype:0:4}" | head -n 1`

  # Check for DRM, first of two checks
  if [ "${A:475:9}" = "4452${newline}4d00" ]; then
   just_the_filename="DRM_${just_the_filename}"
   playback_alert="REQUIRES Microsoft Windows OS and the license key to decrypt the content."
   wmv_drm="1"
  fi

  # Check for DRM, second of two checks
  if [ -z "${playback_alert}" ]; then
   drm=`strings -32 "${item}" | grep "http://www.microsoft.com/isapi/redir.dll\|vidlock"`

   if [ "${drm:0:40}" = "http://www.microsoft.com/isapi/redir.dll" ] && [ -z "${playback_alert}" ]; then
    just_the_filename="DRM_${just_the_filename}"
    playback_alert="REQUIRES Microsoft Windows OS and the license key to decrypt the content."
    wmv_drm="1"
   fi

   if [[ "${drm}" = *"vidlock"* ]] && [ -z "${playback_alert}" ]; then
    just_the_filename="DRM_${just_the_filename}"
    playback_alert="REQUIRES Microsoft Windows OS and the license key to decrypt the content."
    wmv_drm="1"
   fi

  fi

  case "M4S2" in
   # Note that M4S2 asf/wmv files will play in MPlayer on OS X but NOT in Windows Media Player 9
   "${videotype}" ) videotype="M4S2"; extension="asf"; [ "${playback_alert}" = "" ] && playback_alert="Use MPlayer.";;
  esac

  case "MP43" in
   "${videotype}" ) videotype="MP43"; extension="asf";;
  esac

  case "WMV1" in
   "${videotype}" ) videotype="WMV1"; extension="asf"
   # Only advise about a player if the playback alert isn't already set to the DRM message.
   [ "${playback_alert}" = "" ] && playback_alert="Use Windows Media Player, VLC or MPlayer.";;
  esac

  case "WMV2" in
   "${videotype}" ) videotype="WMV2"; extension="asf"
   [ "${playback_alert}" = "" ] && playback_alert="Use Windows Media Player, VLC or MPlayer.";;
  esac

  case "WMV3" in
   "${videotype}" ) videotype="WMV3"; extension="wmv"
   [ "${playback_alert}" = "" ] && playback_alert="Use Windows Media Player.";;
  esac

  if [ "${filetype}" = "asf" -o "${filetype}" = "wmv" ] && [ "${wmv_drm}" != "1" ]; then
   [ -n "${bsed}" ] && check_for_http
   # 68007400740070003A002F002F00 = h t t p : / / for asf/wmv files but these are the media addresses... would cripple streams
  fi

  [ "${filetype}" = "asf" -o "${filetype}" = "wmv" ] &&  [ -n "${ffmpeg}" ] && get_more_info

 fi
fi
}

####################################
check_video_for_RealMedia() {
# file <filename> displays RealMedia file
if [ "${filetype}" = "none" ]; then # Don't check it if it was already identified
 if [ "${A:0:9}" = "2e52 4d46" ]; then # .RMF
  filetype="RealMedia" # creator PNst
  extension="rm"
  playback_alert="Use RealPlayer / RealONE Player."
  [ -n "${ffmpeg}" ] && get_more_info
  #[ -n "${bsed}" ] && check_for_http # hmmm, might cripple streams?
 fi
fi
}

####################################
check_for_RealAudio() {
if [ "${filetype}" = "none" ]; then # Don't check it if it was already identified
 if [ "${A:0:9}" = "2e72 61fd" ]; then # .ra.
  filetype="RealAudio" # creator PNst
  extension="ra"
  playback_alert="Use RealPlayer / RealONE Player."
  [ -n "${ffmpeg}" ] && get_more_info
  #[ -n "${bsed}" ] && check_for_http # hmmm, might cripple streams?
 fi
fi
}

####################################
check_video_for_mpeg() {
# file <filename> displays MPEG System Stream data for MPEG-PS and MPEG Video stream data for MPEG1
if [ "${filetype}" = "none" ]; then # Don't check it if it was already identified
 if [ "${A:0:9}" = "0000 01ba" ]; then
  filetype="mpeg-ps" # MPEG program stream file
  extension="mpg"
  videotype="mpeg-ps"
  [ -n "${ffmpeg}" ] && get_more_info
 fi

 if [ "${A:0:9}" = "0000 01b3" ]; then
  filetype="mpeg1" # MPEG video stream
  extension="mpg"
  videotype="mpeg1"
  playback_alert="Use MPEG Streamclip to Convert to MPEG to enclose this inside an MPEG Program Stream file for maximum compatibility."
  [ -n "${ffmpeg}" ] && get_more_info
 fi

 if [ "${A:0:9}" = "4740 0010" ]; then
  filetype="mpeg-ts" # MPEG transport stream file
  extension="mpg"
  videotype="mpeg-ts"
  [ -n "${ffmpeg}" ] && get_more_info
 fi
fi
}

####################################
check_video_for_QuickTime_moov() {
# file <filename> displays Apple QuickTime movie file (moov)
# or just "data" on some files (unknown magic numbers for newer qt file format probably?)
if [ "${filetype}" = "none" ]; then # Don't check it if it was already identified

 # Let's get this out of the way first so that if it does match we can skip the rest of this function
 if [ "${A:0:39}" = "0000 001c 6674 7970 4d53 4e56 0100 002c" ]; then # ftypMSNV 350 length 9 6d70 3476=mp4v
  filetype="mov"
  extension="mp4"
  [ "${A:350:9}" = "6d70 3476" ] && videotype="mp4v"
  playback_alert="Some MSNV MP4 files will only play in QuickTime if the filename extension is mp4."
  [ -n "${ffmpeg}" ] && get_more_info
  # now exit this function to avoid checking for nsav or urls since apparently this is not a normal mov
  return 0
 fi

 # Check to see if it is a mov file without a compressed header
 if [ "${A:90:9}" = "6d6f 6f76" -o "${A:10:9}" = "6d6f 6f76" -o \
      "${A:10:19}" = "6674 7970 7174 2020" ]; then # moov or "ftypqt  "
  filetype="mov"
  extension="mov"
 fi

 if [ "${A:0:29}" = "0000 001c 6674 7970 4d53 4e56" ]; then
  filetype="mov"
  extension="mov"
  videotype="mpeg4" # I seriously doubt that this is definitive...
 fi

 if [ "${A:10:9}" = "6d64 6174" ]; then # mdat
  filetype="mov"
  extension="mov"
  videotype="cvid" # I don't think this is definitive either
 fi

 case "${A:0:39}" in
 "0000 051d 6d6f 6f76 0000 006c 6d76 6864" | "0000 043e 6672 6565 0000 006c 6d76 6864" | \
 "0000 0483 6d6f 6f76 0000 006c 6d76 6864" | "0000 052b 6d6f 6f76 0000 006c 6d76 6864" | \
 "0000 04e6 6672 6565 0000 006c 6d76 6864" | "0000 051d 6672 6565 0000 006c 6d76 6864" | \
 "0000 073d 6672 6565 0000 006c 6d76 6864" | "0000 052b 6d6f 6f76 0000 006c 6d76 6864" )
  filetype="mov"
  extension="mov"
  videotype="MPEG1-Muxed"
  # MPEG Streamclip will "Convert to MPEG" to get the mpeg out of this wrapper but it doesn't recognize all mov files.
  # Is there an open-source utility to extract mpegs from within files?
  playback_alert="Try MPEG Streamclip to Convert to MPEG or use FileJuicer or CanOpener to extract the MPEG from this QuickTime mov wrapper file."
 ;;
 "0001 45ea 6672 6565 0000 006c 6d76 6864" | "0000 63fc 6672 6565 0000 006c 6d76 6864" | \
 "0000 27fe 6672 6565 0000 006c 6d76 6864" | "0000 0020 6674 7970 7174 2020 2005 0300" )
  filetype="mov"
  extension="mov"
 ;;
 esac

 # Check to see if it has a compressed movie header
 # We'll do this last so the previous routines can't reset the videotype to mov which would cause
 # us to try opening it in QuickTime Player even though it can't be saved.
 if [ "${A:30:9}" = "636d 6f76" -o "${A:110:9}" = "636d 6f76" -o "${A:160:9}" = "636d 6f76" ]; then # cmov
  filetype="mov"
  videotype="cmov"
 fi

 if [ "${filetype}" = "mov" ]; then
  [ -n "${bsed}" ] && check_for_http
  [ -n "${ffmpeg}" ] && get_more_info
  [ "${videotype}" != "cmov" ] && [ -n "${bsed}" ] && check_for_nsav
  [ "${videotype}" != "cmov" ] && set_mov_features
 fi

fi
}

####################################
check_for_nsav() {
# This routine is dependant on bsed and disables the nsav flag in uncompressed mov data
echo -n " Checking for & unlocking nsav: "
bsed_output=`"${bsed}" "6E73617600000001=6E73617600000000" "${item}" "${item}_nsav_UNLOCKED.tmp" 2>&1`
echo "${bsed_output}"
if [ "${bsed_output}" != "0 matches" ]; then
 file_size_before=`ls -al "${item}" | tr -s "\ " | cut -d " " -f 5`
 file_size_after=`ls -al "${item}_nsav_UNLOCKED.tmp" | tr -s "\ " | cut -d " " -f 5`
 if [ "${file_size_before}" == "${file_size_after}" ]; then
  mv "${item}_nsav_UNLOCKED.tmp" "${item}"
  else
  echo " AFTER DISABLING THE NSAV FLAG THE FILE SIZE HAS CHANGED. It was ${file_size_before} and is ${file_size_after}"
  #rm "${item}_nsav_UNLOCKED.tmp"
 fi
 else # if bsed_output is equal to 0 matches then just delete the temp file
  rm "${item}_nsav_UNLOCKED.tmp"
fi
}

####################################
check_for_http() {
# This routine is dependant on bsed and changes the http:// string to "null://" to prevent opening web pages.
if [ "${filetype}" != "none" ]; then
 echo -n " Checking for URLs: "
 bsed_output=`"${bsed}" "http:\\"="null:\\" "${item}" "${item}_deHTTP.tmp" 2>&1`
 echo "${bsed_output}"
 if [ "${bsed_output}" != "0 matches" ]; then
  # 68007400740070003A002F002F00 is h t t p : / / for asf/wmv file media/stream addresses
  file_size_before=`ls -al "${item}" | tr -s "\ " | cut -d " " -f 5`
  file_size_after=`ls -al "${item}_deHTTP.tmp" | tr -s "\ " | cut -d " " -f 5`
  if [ "${file_size_before}" == "${file_size_after}" ]; then
   mv "${item}_deHTTP.tmp" "${item}"
  else
   echo " AFTER NULLIFYING URL THE FILE SIZE HAS CHANGED. It was ${file_size_before} and is ${file_size_after}"
   #rm "${item}_deHTTP.tmp"
  fi
 else # if bsed_output is equal to 0 matches then just delete the temp file
  rm "${item}_deHTTP.tmp"
 fi
fi
}

####################################
set_mov_features() {
# Note that the call goes out to the first QuickTime Player alphabetically in the Applications folder
# Rename undesired versions of QuickTime Player to avoid having them called - e.g. zQuickTime Player Classic
echo " Calling QuickTime Player to disable auto-play and extra tracks."
# Here we issue one osascript command to open QuickTime Player and make it behave
# We also have it close any open movies to avoid confusion
osascript -e 'tell application "QuickTime Player"' \
-e "activate" \
-e "set show welcome movie automatically to false" \
-e "close every movie" \
-e "set play movie from beginning when opened to true" \
-e "set ignore auto play to true" \
-e "set ignore auto present to true" \
-e "set only front movie plays sound to true" \
-e "set open movie in new player to true" \
-e "end tell"

# Tell QuickTime Player to open the movie
open -a "QuickTime Player" "${item}"

# Tell QuickTime Player what changes to make in this movie file
# We're doing all the commands in a single osascript command because its noticeably faster than calling osascript for each command
osascript -e 'tell application "QuickTime Player"' \
-e "set current time of front movie to 0" \
-e "set the scale of front movie to normal" \
-e "set the enabled of (every track of movie 1) to false" \
-e "set the enabled of (every track of movie 1 whose kind is \"Sound\") to true" \
-e "set the enabled of (every track of movie 1 whose kind is \"Sound 1\") to true" \
-e "set the enabled of (every track of movie 1 whose kind is \"Video\") to true" \
-e "set the enabled of (every track of movie 1 whose kind is \"Video 1\") to true" \
-e "set the enabled of (every track of movie 1 whose kind is \"Mpeg1 Muxed\") to true" \
-e "set the auto play of movie 1 to false" \
-e "set the auto present of movie 1 to false" \
-e "set the looping of movie 1 to false" \
-e "set the auto close when done of movie 1 to false" \
-e "set the auto quit when done of movie 1 to false" \
-e "set the controller type of movie 1 to standard" \
-e "set the saveable of (every movie) to true" \
-e "save (movie 1)" \
-e "close window 1" \
-e "end tell"
}

####################################
get_more_info() {
if [ "${filetype}" != "none" ]; then

 # Ask ffmpeg for info about the file
 ffmpeg_video_info=`"${ffmpeg}" -an -vn -i "${item}" 2>&1`

 # Parse for video codec - parsing was tested with ffmpeg version CVS builds 3211266 & 3277056, & ffmpeg version 0.4.8
 ffmpeg_video=`echo "${ffmpeg_video_info}" | grep "Video: " | sed -e "s/, / /g" -e "s/: / /g" -e "s/^.*Video //" -e "s/ .*$//" -e "s/video//"`
 ffmpeg_video=`echo "${ffmpeg_video}" | tr "[:upper:]" "[:lower:]"`

 # Parse for frames per second - parsing was tested with ffmpeg version CVS builds 3211266 & 3277056, & ffmpeg version 0.4.8
 ffmpeg_fps=`echo "${ffmpeg_video_info}" | grep "Video: " | sed -e "s/, / /g" -e "s/: / /g"`
 if [ "${ffmpeg_fps##*\ }" = "fps" ]; then
  ffmpeg_fps="${ffmpeg_fps%\ *}"       # cut space-fps from the end
 else
  ffmpeg_fps="${ffmpeg_fps%%\ fps\ *}" # cut from the end back to the farthest match of space-fps-space
 fi
 ffmpeg_fps="${ffmpeg_fps##*\ }"      # cut from the beginning up to and including the last space

 # Parse for pixel dimensions - parsing was tested with ffmpeg version CVS builds 3211266 & 3277056, & ffmpeg version 0.4.8
 ffmpeg_pixels=`echo "${ffmpeg_video_info}" | grep "Video: " | sed -e "s/, / /g" -e "s/: / /g" -e "s/\///"`

 # Note that this routine to cut kbs must precede the routine to cut fbs as kbs follows fbs in some versions of ffmpeg
 if [ "${ffmpeg_pixels##*\ }" = "kbs" ]; then
  ffmpeg_pixels="${ffmpeg_pixels%\ *}"  # cut space-kbs from the end
  ffmpeg_pixels="${ffmpeg_pixels%\ *}"  # cut space and the kbs value from the end
 fi

 if [ "${ffmpeg_pixels##*\ }" = "fps" ]; then
  ffmpeg_pixels="${ffmpeg_pixels%\ *}"  # cut space-fps from the end
  ffmpeg_pixels="${ffmpeg_pixels%\ *}"  # cut space and the fps value from the end
 fi

  ffmpeg_pixels="${ffmpeg_pixels##*\ }" # cut from the beginning up to and including the last space

 # If ffmpeg named a video codec that matches our videotype then only use our videotype in the file name to avoid redundancy
 videotype_lower=`echo "${videotype}" | tr "[:upper:]" "[:lower:]"`
 [ "${ffmpeg_video}" = "${videotype_lower}" ] && ffmpeg_video=""

 #ffmpeg_audio=`"echo "${ffmpeg_video_info}" | grep "Audio: " | tr -s "\ " | cut -d " " -f 5` # mp2,

 # ffmpeg info does not give accurate duration for all video types so we'll ignore it

 [ -n "${ffmpeg_video}" ]       && ffmpeg_info="${ffmpeg_video}"
 [ -n "${ffmpeg_pixels}" ]      && ffmpeg_info="${ffmpeg_info}_${ffmpeg_pixels}"
 [ -n "${ffmpeg_fps}" ]         && ffmpeg_info="${ffmpeg_info}_${ffmpeg_fps}fps"
 [ -n "${ffmpeg_info}" ]        && ffmpeg_info="${ffmpeg_info}-"

 [ "${ffmpeg_info:0:1}" = "_" ] && ffmpeg_info="${ffmpeg_info:1}"
 just_the_filename="${ffmpeg_info}-${just_the_filename}"
fi
}

####################################
check_for_steganography() {
# check for steganography
echo -n " Checking for steganography. False positives are common..."
steg=`"${stegdetect}" -q "${item}"`
if [ -n "${steg}" ]; then
 echo "${steg##*\ }"
else
 echo "None detected."
fi
}

####################################
display_file_info() {
# This routine is no longer called because the script now displays the directory at start and
# the other information is shown as part of the output filename.
#[ -n "${filepath}" ] && echo "  Dirname: ${filepath}"
#echo " Filetype: ${filetype}   Videotype: ${videotype}   Extension: ${extension}"
# Playback alert gets displayed in the process_file function now
#[ -n "${playback_alert}" ] && echo " Playback: ${playback_alert}"
echo
echo "======================================================================"
}

####################################
clean_just_the_filename() {
# get rid of problematic characters and squeeze spaces, periods, hyphens and underscores
just_the_filename=`echo "${just_the_filename}" | tr -Cd "[:alnum:] _-.()#," | tr -cd "[:print:]" | tr -s "\ \.\_\-"` # added #, to the -Cd
# since we are only working with the filename and there is no extension at this point, remove junk at the end
just_the_filename=`echo "${just_the_filename}" | sed -e 's/[ _-]*$//'`
}

####################################
generate_output_filename() {
[ "${filetype}" = "${videotype}" ] && filetype="none" # no sense naming the file mpeg1_mpeg1 for instance

if [ "${just_the_filename:0:1}" = "-" -o "${just_the_filename:0:1}" = "_" ]; then
 just_the_filename="${just_the_filename:1}"
fi

# if videotype is not none and filetype is none
if [ "${filetype}" = "none" ] && [ "${videotype}" != "none" ]; then
 [ -z "${ffmpeg_info}" ] && target="${videotype}-${just_the_filename}"
 [ -n "${ffmpeg_info}" ] && target="${videotype}_${just_the_filename}"
fi

# if videotype is none and filetype is not none
if [ "${videotype}" = "none" ] && [ "${filetype}" != "none" ]; then
  [ -z "${ffmpeg_info}" ] && target="${filetype}-${just_the_filename}"
  [ -n "${ffmpeg_info}" ] && target="${filetype}_${just_the_filename}"
fi

# if both videotype and filetype are none
if [ "${videotype}" = "none" ] && [ "${filetype}" = "none" ]; then
 target="${just_the_filename}"
fi

# if both videotype and filetype are not none
if [ "${videotype}" != "none" ] && [ "${filetype}" != "none" ]; then
  [ -z "${ffmpeg_info}" ] && target="${filetype}_${videotype}-${just_the_filename}"
  [ -n "${ffmpeg_info}" ] && target="${filetype}_${videotype}_${just_the_filename}"
fi

# Note that we have not added .${extension} yet
# If the new filename is longer than 255 characters then truncate to 250 which allows
# five character extensions such as ".mpeg"
if [ "${#target}" -gt 250 ]; then
 target="${target:0:250}"
fi
target="${target//_-/-}" # one more time to try to clean _- from the name due to sloppiness
check_for_existing_target_filename
target="${filepath}/${target}.${extension}"
}

####################################
check_for_existing_target_filename() {
# check if name exists and append a number counting up if it does
if [ -e "${filepath}/${target}.${extension}" ] && [ "${filepath}/${target}.${extension}" != "${item}" ]; then
 target="${target%\_#*}"
 # Truncate filename to 245 chrs which should accomodate adding _#999.mpeg for instance and still fit a 255 chr filename
 [ "${#target}" -gt 245 ] && target="${target:0:245}"
 target="${target}_#${name_count}"
 let name_count=name_count+1
 check_for_existing_target_filename
fi
}

####################################
process_folder() {
# Die here if the specified directory is not a directory or not in existence
[ ! -d "${source_path}" ] && echo "Folder NOT FOUND" && exit 66
# Put us in the specified directory thus making it the current directory
cd "${source_path}"
echo "DIRECTORY: "`pwd`
echo "======================================================================"
find "${source_path}" -type f \! -name ".DS_Store" \! -name "." \! -name ".." | while read item; do
 process_file
done # end of reading filenames from folder
}

####################################
process_file() {
# the osascript command below generates the following error when a double-quote character is part of the
# folder name of the folder to process but then the script still works
# 287:301: syntax error: Expected end of line but found application constant or consideration. (-2741)
# its probably in how we quoted the command and/or filename for osascript?
#item_created=`osascript -e 'tell application "System Events"' -e "get creation date of file \"$item\"" -e "end tell"`

# Unlock the Finder lock flag ignoring any feedback or errors
chflags nouchg "${item}" &>/dev/null
declare_variables_for_this_file

case "${extension}" in
 "mpg" | "mpeg" | "mp4" | "mpe" | "avi" | "asf" | "wmv" | "asx" | "mov" | "rm" | "ram" | \
 "ra" | "divx" | "jpeg" | "jpg" | "pdf" | "p7" | "gif" | "bmp" | "png" | "psd" | "rtf" ) kind="known";;
esac

file_size=`ls -al "${item}" | tr -s "\ " | cut -d " " -f 5`
if [ "${kind}" = "known" ] && [ "${file_size}" -gt "1" ]; then

 remove_previous_leader_from_filename

 echo ">${filename}"
 A=`hexdump -vn 1000 "${item}" | colrm 1 8`

 # call the function to strip leading if the start of the file is padded with "00"s or "ff"s
 [ "${A:0:39}" = "0000 0000 0000 0000 0000 0000 0000 0000" -o \
   "${A:0:39}" = "ffff ffff ffff ffff ffff ffff ffff ffff" ] && strip_leading

 [ "${filetype}" = "none" ] && check_for_jpeg
 [ "${filetype}" = "none" ] && check_for_P7
 [ "${filetype}" = "none" ] && check_for_pdf
 [ "${filetype}" = "none" ] && check_for_gif
 [ "${filetype}" = "none" ] && check_for_rtf
 [ "${filetype}" = "none" ] && check_for_png
 [ "${filetype}" = "none" ] && check_for_bmp
 [ "${filetype}" = "none" ] && check_for_Photoshop
 [ "${filetype}" = "none" ] && check_video_for_avi
 [ "${filetype}" = "none" ] && check_video_for_asf_or_wmv
 [ "${filetype}" = "none" ] && check_video_for_RealMedia
 [ "${filetype}" = "none" ] && check_for_RealAudio
 [ "${filetype}" = "none" ] && check_video_for_mpeg
 [ "${filetype}" = "none" ] && check_video_for_QuickTime_moov

 # Displaying this here instead of calling the display_file_info function because this
 # is all we need now because the filename itself shows the remainder of the info.
 [ -n "${playback_alert}" ] && echo " Playback: ${playback_alert}"

 clean_just_the_filename
 generate_output_filename

 # only rename if the name is different than the original file
 if [ "${target}" != "${item}" ]; then
  # Actually renaming the file may fail if we do not have write access
  # Only move the file if the new name does NOT already exist
  [ ! -e "${target}" ] && mv "${item}" "${target}"
 fi

 echo "<"`basename "${target}"`
 echo "----------------------------------------------------------------------"
fi # end of if kind is known
}

####################################
find_bsed () {
# Find the bsed executable
# If it is in the folder with this script try that one first
bsed="${script_folder}/bsed"
# If that one didn't work out, try finding one in the bash PATH
[ ! -f "${bsed}" -o ! -x "${bsed}" ] && bsed=`which bsed`
# If we still have nothing, use locate
if [ -z "${bsed}" ]; then
 candidates=`locate bsed |  grep "/bsed$"`
 while [ ! -x "${bsed}" ] && [ ! -f "${bsed}" ]; do
  candidate=`echo "${candidates}" | head -n 1`
  candidates="${candidates:((${#candidate}+1))}"
  # If we found one that is an executable file then use it otherwise give up
  [ -x "${candidate}" ] && [ -f "${candidate}" ] && bsed="${candidate}"
 done
fi
}

####################################
find_ffmpeg () {
# Find the ffmpeg executable
# If it is in the folder with this script try that one first
ffmpeg="${script_folder}/ffmpeg"
# If that one didn't work out, try finding one in the bash PATH
[ ! -f "${ffmpeg}" -o ! -x "${ffmpeg}" ] && ffmpeg=`which ffmpeg`
# If we still have nothing, use locate
if [ -z "${ffmpeg}" ]; then
 candidates=`locate ffmpeg |  grep "/ffmpeg$"`
 while [ ! -x "${ffmpeg}" ] && [ ! -f "${ffmpeg}" ]; do
  candidate=`echo "${candidates}" | head -n 1`
  candidates="${candidates:((${#candidate}+1))}"
  # The check for older versions is now disabled due to improved parsing of the ffmpeg output
  # Older versions of ffmpeg don't have the same info output format so we'll try to eliminate them
  #candidate_version=`"${candidate}" "-version" 2>&1 | grep -v "unrecognized"`
  #[ -x "${candidate}" ] && [ -f "${candidate}" ] && [ -n "${candidate_version}" ] && ffmpeg="${candidate}"
  # If we found one that is an executable file then use it otherwise give up
  [ -x "${candidate}" ] && [ -f "${candidate}" ] && ffmpeg="${candidate}"
 done
fi
}

####################################
find_stegdetect () {
# Find the stegdetect executable
# If it is in the folder with this script try that one first
stegdetect="${script_folder}/stegdetect"
# If that one didn't work out, try finding one in the bash PATH
[ ! -f "${stegdetect}" -o ! -x "${stegdetect}" ] && stegdetect=`which stegdetect`
# If we still have nothing, use locate
if [ -z "${stegdetect}" ]; then
 candidates=`locate stegdetect |  grep "/stegdetect$"`
 while [ ! -x "${stegdetect}" ] && [ ! -f "${stegdetect}" ]; do
  candidate=`echo "${candidates}" | head -n 1`
  candidates="${candidates:((${#candidate}+1))}"
  # If we found one that is an executable file then use it otherwise give up
  [ -x "${candidate}" ] && [ -f "${candidate}" ] && stegdetect="${candidate}"
 done
fi
}

####################################
find_SetFile() {
# Find the SetFile executable which comes with Developer Tools or XCode
[ -x /Developer/tools/SetFile ] && SetFile="/Developer/tools/SetFile"
}

####################################
find_GetFileInfo() {
# Find the GetFileInfo executable which comes with Developer Tools or XCode
[ -x /Developer/tools/GetFileInfo ] && GetFileInfo="/Developer/tools/GetFileInfo"
}

#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# End of Functions (Subroutines)



#vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
# Variables (see also function to set variables for each file)
bsed=""
ffmpeg=""
stegdetect=""
SetFile=""
GetFileInfo=""
candidate=""
candidates=""
candidate_version=""
item=""
file_size="0"
source_path=""
script_folder=`pwd`
newline=$'\n'
repeat=1
rerun=""
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# End of variables

#vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
# Error codes
# 65 = no arguments found on the command line and they are required
# 66 = folder not found
#^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

#vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
# Main

# Make the Terminal window wider to accomodate long file names without wrapping as often
osascript -e 'tell application "Terminal" to set the size of the window frontmost to {1065, 450}'
clear # clear the window

[ ! -x "${bsed}" ]        && find_bsed
[ ! -x "${ffmpeg}" ]      && find_ffmpeg
[ ! -x "${stegdetect}" ]  && find_stegdetect
[ ! -x "${SetFile}" ]     && find_SetFile
[ ! -x "${GetFileInfo}" ] && find_GetFileInfo

echo "=============================videntify================================"
[ -n "${bsed}" ]       && echo "      Found bsed: ${bsed}"
[ -z "${bsed}" ]       && echo "No bsed executable found, routines DISABLED."
[ -z "${bsed}" ]       && echo " Get bsed from [url=http://www1.bell-labs.com/cgi-user/wwexptools/gensnapshot?bsed"]http://www1.bell-labs.com/cgi-user/wwexpto...shot?bsed"[/url]
[ -n "${ffmpeg}" ]     && echo "    Found ffmpeg: ${ffmpeg}"
[ -z "${ffmpeg}" ]     && echo "No ffmpeg executable found, routines DISABLED. [url=http://homepage.mac.com/major4/"]http://homepage.mac.com/major4/"[/url]
[ -n "${stegdetect}" ] && echo "Found stegdetect: ${stegdetect}"
[ -z "${stegdetect}" ] && echo "No stegdetect executable found, routines DISABLED. [url=http://www.outguess.org/detection.php"]http://www.outguess.org/detection.php"[/url]
echo

if [ $# -gt 0 ]; then
repeat=0 # don't repeat the script
until [ -z "${1}" ]; do
 [ -d "${1}" ] && source_path="${1}" && process_folder
 [ -f "${1}" ] && item="${1}" && echo "FILE: ${item}" && process_file
 shift
done
fi

if [ $repeat -eq 1 ]; then
greeting
[ $# -eq 0 ] && select_source
if [ -d "${source_folder}" ]; th

Edited by Valirian_Oak, 12 February 2006 - 07:26 AM.


#10 Siph0n

Siph0n
  • V.I.P.
  • 2,870 posts

Posted 12 January 2006 - 08:43 PM

Fix ur [code=auto:0] tags homie before you can't edit anymore :P

Oh yeah...can you zip it?

Edited by Siph0n, 12 January 2006 - 08:43 PM.

Is a rose bush wrong in its thorny self-adornment? Is fire truly bad when it engulfs mere material in beautiful flame? So it is with Siph0n...


Click: Thar? Yes, right thar.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users