LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 11-05-2013, 03:09 PM   #16
w1k0
Senior Member
 
Registered: May 2008
Location: Poland
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309

Original Poster
Rep: Reputation: 234Reputation: 234Reputation: 234

After a few months I came back to my any2avi script. The previous version from post #15 wasn’t too good. It produced the files with strong artifacts around the fast moving objects or objects moving in the dark. In some cases after the rescaling a problem concerning non-square pixels appeared. The script didn’t recognize the accepted formats so it performed some unwanted conversions as well.

The new version of the script solves these problems. Thanks to the configuration section you may list all accepted formats in order to avoid unnecessary conversions. You may use the full lists of the video and audio formats or the shortened lists using the regular expressions (see: the script). The parameters and formats which I stored in that section of the script are accepted by my device. You may remove some of them or add the new ones for the best compatibility with your device. If you’d like to produce the most compatible files leave in the configuration section just mpeg4 video format and mp3 audio format. The new script uses just ffmpeg program but you should compile it with LAME, X264, FAAC, and XVID enabled.

Have fun!

any2avi
Code:
#!/bin/bash

# any2avi: converts any video file to avi file compatible with most devices

# compile ffmpeg with LAME, X264, FAAC, and XVID enabled:
#
# FAAC=yes XVID=yes ./ffmpeg.SlackBuild

#
# CONFIGURATION SECTION BEGINS HERE
#

accepted_width="720"            # highest accepted width
accepted_height="576"           # highest accepted height

accepted_video="mpeg4 msmpeg4 mpeg2video mpeg1video"    # all accepted video codecs
accepted_audio="mp3 ac3 mp2 wmav2 pcm_s16le"            # all accepted audio codecs

#accepted_video="(ms)?mpeg4 mpeg[12]video"      # all accepted video codecs
#accepted_audio="mp[23] ac3 wmav2 pcm_s16le"    # all accepted audio codecs

#
# CONFIGURATION SECTION ENDS HERE
#

infile="$1"

if [ "$infile" == "" ]
then
cat <<EOF
any2avi: converts any video file to avi file compatible with most devices

any2avi file

EOF
    exit
fi

tempfile="/tmp/.ffmpeg.tmp"

accepted_scale=${accepted_width}x${accepted_height}

ffmpeg -i "$infile" 2> $tempfile

if [ ! -e $tempfile ]
then
cat <<EOF
any2avi: cannot create $tempfile file

EOF
    exit
fi

video_string=$(grep 'Video: ' $tempfile)
audio_string=$(grep 'Audio: ' $tempfile)

rm $tempfile

orig_vcodec=$(echo $video_string | awk '{print $4}')
orig_vcodec=${orig_vcodec/,/}
orig_acodec=$(echo $audio_string | awk '{print $4}')
orig_acodec=${orig_acodec/,/}
orig_scale=`echo $video_string | sed -E "s/.* ([0-9]+x[0-9]+)[ ,].*/\1/"`

orig_width=$orig_scale
orig_width=${orig_width/x*/}
orig_height=$orig_scale
orig_height=${orig_height/*x/}

orig_aspect=$(echo "scale=2 ; $orig_width/$orig_height" | bc)

if [ $orig_width -le $accepted_width ] && [ $orig_height -le $accepted_height ]
then
    video="rejected"
    for video_format in $accepted_video
    do
        if [ "$(echo $orig_vcodec | egrep -w $video_format)" != "" ]
        then
            video="accepted"
        fi
    done
    audio="rejected"
    for audio_format in $accepted_audio
    do
        if [ "$(echo $orig_acodec | egrep -w $audio_format)" != "" ]
        then
            audio="accepted"
        fi
    done
    if [ "$video" == "accepted" ] && [ "$audio" == "accepted" ]
    then
cat <<EOF
any2avi: no conversion required

#   scale = $orig_scale
#  aspect = $orig_aspect
#  vcodec = $orig_vcodec
#  acodec = $orig_acodec

EOF
        exit
    fi
fi

function count_scale() {
    if [ $orig_width -gt $accepted_width ]
    then
        factor=$(echo "scale=2 ; $orig_width/$accepted_width" | bc)
        width=$accepted_width
        height=$(echo "$orig_height/$factor" | bc)
        height=$(($height/2))
        height=$(($height*2))
    else
        width=$orig_width
        height=$orig_height
    fi
    if [ $height -gt $accepted_height ]
    then
        factor=$(echo "scale=2 ; $height/$accepted_height" | bc)
        height=$accepted_height
        width=$(echo "$width/$factor" | bc)
        width=$(($width/2))
        width=$(($width*2))
    fi
    scale=${width}x${height}
}

count_scale

aspect=$(echo "scale=2 ; $width/$height" | bc)
bitrate=$(($width*$height*50*25/256/1000))k

function run() {
cat <<EOF
# accepted:
#   scale = $accepted_scale
#  vcodec = $accepted_video
#  acodec = $accepted_audio

# original:
#   scale = $orig_scale
#  aspect = $orig_aspect
#  vcodec = $orig_vcodec
#  acodec = $orig_acodec

# used:
#   scale = $scale
#  aspect = $aspect
# bitrate = $bitrate
#  vcodec = mpeg4 (libxvid)
#  acodec = mp3   (libmp3lame)

$command

EOF
    eval $command
}

command="ffmpeg -i \"$infile\" -codec:v libxvid -b:v $bitrate -aspect $aspect -s $scale -codec:a libmp3lame -b:a 160k -ar 48000 \"$infile.avi\""
run
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do you create a DVD that can play in a DVD player from regular .avi videos? whited Linux - Software 2 04-09-2008 05:45 PM
I can't play .avi file Gins Linux - General 9 08-19-2007 01:56 PM
convert .avi's to play on a dvd player kryptobs2000 Linux - Software 8 01-11-2007 11:23 PM
Cannot Play back DVD on PC recorded from a DVD recorder shazam75 Linux - Software 2 12-29-2005 03:11 AM
No colour when I play DVD or AVI videoos annashaw Linux - Laptop and Netbook 1 02-08-2005 04:51 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration