Hacker News new | past | comments | ask | show | jobs | submit login

> I even use it when an image I saved ends up as .html for some reason and then use the info screen to determine the real extension it should be so I can rename it

If you’re on Linux on macOS, you’ll have access to the `file` command-line tool which can do it effectively. In a couple of lines of shell scripting you can even have it auto-change the extension.

https://en.wikipedia.org/wiki/File_(command)




I had someone try to tell me this or some similar alternative method once, but for this particular use-case my method seemed faster. Usually I'm viewing the dir with the file in ranger, hit r (shortcut for :open_with ), type mpv, enter, I in mpv for info, q to quit, a in ranger to rename the file with old name pretyped in. It's a pretty smooth workflow. Assuming I start from the same place in ranger I have to press colon and type `shell -w file %s` and hit enter which may or may not be faster but I think is more keystrokes. I could bind it to a key in ranger if I found an open one and then that might be better. It's sometimes weeks between instances of the mis-saved images or else I might've tried harder to optimize this. The shell script idea is neat.


Personally I use a shell script to do it automatically for me... with "file":

    #!/bin/bash
    # Usage: fix-extensions <media file(s)>
    # Renames the files to consistent file extensions based on content

    for file in "$@"; do
        [ -f "$file" ] || continue
        dir=$(dirname "$file")
        name=$(basename "$file")
        prefix="${name%.*}"
        ext="${name##*.}"

        magic=$(file -b "$file")
        newext=""
        case "$magic" in
        *"Apple QuickTime movie"*)        newext=mov;;
        "ISO Media, Apple iTunes Video"*) newext=mp4;;
        "ISO Media, MP4"*)                newext=mp4;;
        "ISO Media, MPEG-4 (.MP4)"*)      newext=mp4;;
        "Macromedia Flash data"*)         newext=swf;;
        "Macromedia Flash Video")         newext=flv;;
        "Matroska data")                  newext=mkv;;
        "Microsoft ASF"*)                 newext=wmv;;
        "MPEG sequence"*)                 newext=mpeg;;
        "Ogg data, OGM video"*)           newext=ogm;;
        "RealMedia file")                 newext=rm;;
        "RIFF "*" data, AVI,"*)           newext=avi;;
        "WebM")                           newext=webm;;
        esac

        if [ -z "$newext" ]; then
            echo >&2 "$file: not renaming, unknown extension for type '$magic'"
        elif [ "$ext" != "$newext" ]; then
            mv -v "$file" "$dir/$prefix.$newext"
        fi
    done


> The shell script idea is neat.

Could be something as simple as:

  file_path="${1}"
  mime="$(file --mime-type --brief "${file_path}")"
  ext="${mime#image/}"
  mv "${file_path}" "${file_path%.*}.${ext}"
That does no checks (e.g. is the file an image) and may not work on something on something like a video, but it’ll do for common image types such as png, jpeg, webp, heic, gif…




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: