Renaming Files With Sequential Numbers, Plus Moviemaking Therefrom

The avconv (formerly ffmpeg) image-to-video programs expect sequentially numbered files, with the numbers in a fixed-width part of the file name, thusly: dsc00001.jpg.

Given a set of files (previously normalized to lowercase) like this:

ll | head
total 286576
-rwxr-xr-x 1 ed ed 595708 Jan 23 19:14 dsc00940.jpg
-rwxr-xr-x 1 ed ed 515561 Jan 23 19:14 dsc00941.jpg
-rwxr-xr-x 1 ed ed 580190 Jan 23 19:14 dsc00942.jpg
-rwxr-xr-x 1 ed ed 571387 Jan 23 19:14 dsc00943.jpg
-rwxr-xr-x 1 ed ed 573207 Jan 23 19:14 dsc00944.jpg
-rwxr-xr-x 1 ed ed 571086 Jan 23 19:14 dsc00945.jpg
-rwxr-xr-x 1 ed ed 571600 Jan 23 19:14 dsc00946.jpg
-rwxr-xr-x 1 ed ed 571547 Jan 23 19:14 dsc00947.jpg
-rwxr-xr-x 1 ed ed 565706 Jan 23 19:15 dsc00948.jpg

A Bash one-liner loop does the renumbering:

sn=1 ; for f in *jpg ; do printf -v dn 'dsc%05d.jpg' "$(( sn++ ))" ; mv $f $dn ; done

The results look pretty much like you’d expect:

ll | head
total 286556
-rwxr-xr-x 1 ed ed 595708 Jan 23 19:14 dsc00001.jpg
-rwxr-xr-x 1 ed ed 515561 Jan 23 19:14 dsc00002.jpg
-rwxr-xr-x 1 ed ed 580190 Jan 23 19:14 dsc00003.jpg
-rwxr-xr-x 1 ed ed 571387 Jan 23 19:14 dsc00004.jpg
-rwxr-xr-x 1 ed ed 573207 Jan 23 19:14 dsc00005.jpg
-rwxr-xr-x 1 ed ed 571086 Jan 23 19:14 dsc00006.jpg
-rwxr-xr-x 1 ed ed 571600 Jan 23 19:14 dsc00007.jpg
-rwxr-xr-x 1 ed ed 571547 Jan 23 19:14 dsc00008.jpg
-rwxr-xr-x 1 ed ed 565706 Jan 23 19:15 dsc00009.jpg

Because you’re renaming the files anyway, don’t bother to normalize ’em:

sn=1 ; for f in *JPG ; do printf -v dn 'dsc%05d.jpg' "$(( sn++ ))" ; mv $f $dn ; done

And, of course, you can fetch ’em from the camera while doing that:

sn=1 ; for f in /mnt/part/DCIM/100MSDCF/*JPG ; do printf -v dn 'dsc%05d.jpg' "$(( sn++ ))" ; cp -a $f $dn ; done

That leaves the DSC*JPG original files on the camera, where you can delete all of them in one operation when you’re happy with the results.

If you don’t need the full resolution, reserialize and resize each picture on the fly:

sn=1 ; for f in /mnt/part/DCIM/100MSDCF/*JPG ; do printf -v dn 'dsc%05d.jpg' "$(( sn++ ))" ; convert $f -resize 50% $dn ; done

That’s based on combining several hints turned up by the usual Google search.

To assemble a quick-and-simple movie from the images:

avconv -r 30 -i dsc%05d.jpg -q 5 movie.mp4

The image quality certainly isn’t up to what you (well, I) would expect from a 1920×1080 “HD” file, but the Sony HDR-AS30V Zeiss camera lens seems to be a fisheye pinhole (170° view angle, 2.5 mm f/2.8) backed with relentless image compression:

Sony HDR-AS30V Action Camera
Sony HDR-AS30V Action Camera

Memo to Self: It’s not worth creating and remembering Yet Another Script.

7 thoughts on “Renaming Files With Sequential Numbers, Plus Moviemaking Therefrom

  1. Well, here are mine:

    #!/bin/bash
    start=3217
    for i in seq '3644 3703'; do
    file="100_${i}.JPG"
    if [ -e $file ]; then
    echo "File $file --> 104_${start}.JPG";
    mv $file 104_${start}.JPG;
    ((start++))
    else
    echo "File $file missing"
    fi
    done

    I have the whole saved as a script, and just edit and run it. Obviously the numbers have to be changed each time. The file 100_3644.JPG gets moved to 104_3217.JPG and so on.

    It skips deleted files and tells me, which is nice since the camera also does movies which it numbers sequentially.

    I save the pictures to a folder called “originals”

    The next one:

    for i in seq '3217 3312'; do convert -resize 1024  104_${i}.JPG ./201402_${i}.JPG ;done

    saves a smaller copy one folder up the tree and adds a date to the filename.

    These smaller copies I use for watching and sending to people. The originals stay stashed away for ordering prints.

    [Ed: the seq commands have two backticks, which cannot be placed in comments, so you’ll see normal quotes here.]

  2. oh – your blog software removed the back apostrophe around “seq blabla”
    Obviously that’s crucial.

    1. WordPress has been chewing on my source code listings, so I’m not surprised the backticks vanished. What is surprising is that I can’t insert them from this side, either. I’ve wrapped the code sections, added single quotes, and appended an explanatory note.

      I haven’t gotten to the point of stashing any of the files away yet… thanks for the tip!

      1. tip: (in bash at least) you can use $() instead of backticks, like “for i in $(seq 3217 3312) ….” i actually prefer it because it’s easier to type and read for me

        [Ed: removed the spurious quotes per the next comment.]

        1. Oops, I mindlessly copied the misplaced single quotes, the correct command is of course $(seq 3217 3312) Also, a nice side effect of the $(…) notation is that in most text editors you get paren matching but not all have backtick matching.

          [Ed: they’re gone!]

    1. I loves me some DIY Bash, even if it’s hard to show off the backticks…

Comments are closed.