Convert multiple Flash (flv) Videos to, for example, MPEG encoding

With ffmpeg and bash this is really easy. Also .flv and .mpeg are easily interchangeable by all other video codecs supported by ffmpeg:

#!/bin/bash
##convert all videos of $srctype type to $desttype type encoding
SRCCODEC="flv"
DESTCODEC="mpeg"
for i in *.$SRCCODEC; do
    echo
    echo -n "###################  "
    echo -n "will convert \"$i\""
    echo "  ###################"
    echo
    ffmpeg -i "${i}" -y "${i%%.${SRCCODEC}}.${DESTCODEC}" && rm -vf "$i"
done

Note that this includes removing the original video file only after a successful recode run overwriting existing destination files (-y option). The interesting part is this: ${i%%.${SRCCODEC}}. It removes the source’s postfix, i.e. file extension. You could save this as a text file, say convert_all_videos_in_pwd.sh and chmod +x filename. I have my own scripts go to ~/bin which I include in $PATH via ~/.bashrc. If you do so you would typically call this within the videos’ directory including logging all output and errors to convert.log, i.e. redirecting STDOUT (= file descriptor 1) and STDERR (= file descriptor 2) via &>:

convert_all_videos_in_pwd.sh &> convert_all.log &

If you want to see the output you could use tail -f logfile or read more on redirection and duplication. You could also do all that in a single command line:

for i in *.flv; do echo converting $i; ffmpeg -i "${i}" -y "${i%%.flv}.mpeg" && rm -vf "$i"; done

Delete folder recursively via php if ftp says: Prohibited directory name

Using eclipse to sync an ftp folder with my local XAMPP installation I ran into he following situation. I ended up with a folder created on the remote machine called 'C:\xampp\tmp' that I could not delete or even view properties or content of using any ftp client I could get hands on (filezilla, ncftp3, gnome ftp “server connect”). Since the folder was on webspace I thought: “Mmh, why not try php.” So I created a file called delfolder.php right in the folder containing the miscurious c:-directory and finally came up with this:

< ?php
function rm_recursive($filepath) {
    if (is_dir($filepath) && !is_link($filepath)) {
        if ($dh = opendir($filepath)) {
            while (($sf = readdir($dh)) !== false) {
                if ($sf == '.' || $sf == '..' ) {
                    continue;
                }
                if (!rm_recursive($filepath.'/'.$sf)) {
                    throw new Exception($filepath.'/'.$sf.' could not be deleted.');
                }
            }
            closedir($dh);
        }
        return rmdir($filepath);
    }
    return unlink($filepath);
}

// Path to directory you want to delete
$directory = 'C:\xampp\tmp';

// Delete it
if (rm_recursive($directory)) {
    echo "{$directory} has been deleted";
} else {
    echo "{$directory} could not be deleted";
}

This, of course, only works if you have either direct access via shell (but than again why not just use rm -Rf ‘C:\xampp\tmp’) or FTP and web, i.e. HTTP, access to the folder in question.

Resources:

Were you ever in the desperate need to know all primes below 1000?

In Python calculating primes can be done with a one-liner using functional programming concepts lambda function, map(), reduce() and filter():

# Primes <1000
print filter(None,map(lambda y:y*reduce(lambda x,y:x*y!=0,map(lambda x,y=y: y%x, range(2,int(pow(y,0.5)+1))),1), range(2, 1000)))

To try this out on a system with python installed in a terminal/console/… type python. In the command prompt showing up paste the line, hit enter and you’ll get:
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]Update: Thanks to Matt and Mark the code snippet now can look much nicer:

# Primes <1000
print filter(None, map(lambda y: y*reduce(lambda x,y: x*y!=0, map(lambda x,y=y: y%x, range(2, int(pow(y, 0.5)+1))),1),range(2,1000)))

Update: Not quite as accurate (compare start of the sequence, 3 is missing) but much shorter using another formula and filter() and lambda techniques only:

filter(lambda x: x % 2 != 0 and x % 3 != 0, range(2, 1000))

[5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]

Let Bash do the math: Doing calculations using that bash

Have you ever wanted to “just do some math” without much fuss? I noticed a while ago (but haven’t really used it since) that bash (born again shell) has, amongst many other evaluations, an arithmetic one. Using the syntax $((expression)) you can evaluate the expression to arithmetically. Operations allowed are quite a few. From man bash:

ARITHMETIC EVALUATION
The shell allows arithmetic expressions to be evaluated, under certain circumstances (see the let and declare builtin commands and Arithmetic Expansion). Evaluation is done in fixed-width integers with no check for overflow, though division by 0 is trapped and flagged as an error. The operators and their precedence, asso ciativity, and values are the same as in the C language. The following list of operators is grouped into levels of equal-precedence operators. The levels are listed in order of decreasing precedence.

       id++ id--   variable post-increment and post-decrement
       ++id --id   variable pre-increment and pre-decrement
       - +             unary minus and plus
       ! ~             logical and bitwise negation
       **              exponentiation
       * / %         multiplication, division, remainder
       + -             addition, subtraction
       <>       left and right bitwise shifts
       =
                        comparison
       == !=       equality and inequality
       &               bitwise AND
       ^               bitwise exclusive OR
       |                bitwise OR
       &&             logical AND
       ||              logical OR
       expr?expr:expr
                        conditional operator
       = *= /= %= += -= <>= &= ^= |=
                        assignment
       expr1 , expr2
                        comma

Shell variables are allowed as operands; parameter expansion is performed before the expression is evaluated. Within an expression, shell variables may also be referenced by name without using the parameter expansion syntax. A shell variable that is null or unset evaluates to 0 when referenced by name without using the parameter expansion syntax. The value of a variable is evaluated as an arithmetic expression when it is ref erenced, or when a variable which has been given the integer attribute using declare -i is assigned a value. A null value evaluates to 0. A shell variable need not have its integer attribute turned on to be used in an expression.

Constants with a leading 0 are interpreted as octal numbers. A leading 0x or 0X denotes hexadecimal. Other wise, numbers take the form [base#]n, where base is a decimal number between 2 and 64 representing the arith metic base, and n is a number in that base. If base# is omitted, then base 10 is used. The digits greater than 9 are represented by the lowercase letters, the uppercase letters, @, and _, in that order. If base is less than or equal to 36, lowercase and uppercase letters may be used interchangeably to represent numbers between 10 and 35.

Resizing Digital Pictures Made Easy

One might think it’s not that much of a task to resize a bunch of files in one directory so a certain dimention. Say you want to have all jpg images in your directory have width 1024 with aspect ratio maintained. For one image using GIMP actually this is easy. Just open it, go to image > scale image and type “1024″ for width. That’s it. Knowing of GIMP’s batch mode feature I though: Well, now let’s just do this very same thing in batch mode for multiple files. Googling I found this passage Adrian Likins GIMP Batch How-To:

Batch mode is slow. Its not really a practical replacement for tools like ImageMagick or NetPBM when it comes to large scale image conversions or similar. At least not without writing some very clever scripts.
The problem with this approach is that gimp/script-fu has no built in procedures to iiterate though a list of images. So you cant easily tell gimp to load up *.jpg and run predator.scm on them, at least not without it taking a _long_ time.

There’s the solution, I thought. So I installed ImageMagick for Windows and made myself ready to run a Windows batch for loop on the files. After double checking against ImageMagick’s delivered help for convert I knew the syntax:

convert  -resize 1024

So far so good. But now comes the Windows batch bit. Even though fairly powerfull even compared with bash or other mighty script languages on Linux I couldn’t figure out how to run more than one command per iteration step. This is what I came around to, which does what intended, but without a pretty notice to the user that could have been done with adding something like ; @echo %i

for /f %i in ('dir /b *.jpg') do convert %i -resize 50% %i

Note that I chose to halve the pics rather than to hard-set the width so I don’t risk upright pictures to become bigger in file size than the original. There you go!

Update: I finally came around to do it with Linux. Here is the Bash equivalent to the above (plus an additional nice notification which is easy with Bash):

for i in *.jpg; do echo "converting \"$i\"..."; convert "$i" -resize 50% "$i"; done

Follow

Get every new post delivered to your Inbox.