1

I need to make a shell script that uses jq to rename below mp4 file(s). (could have use jasonpipe but didn't know how to install it on mac)

File Dowloaded:

  1. title1-1000221327.mp4
  2. title1-1000221327.info.json
  3. title1-1000221328.mp4
  4. title1-1000221328.info.json

I want to rename file 1&3 by extracting from above respective json file (2&3):

  • "series": "ShowName", as ShowName
  • "season_number": 1, as "S01"
  • "episode_number": 6, as "E006"
  • "title": "title1", as title1

also insert space & - between these four variables " "-" "

After rename file should be:

ShowName - S01E006 - title1.mp4

contents of "title1-1000221327.info.json"

Edit: thanks for the tip: solved the jq portion sofar

jq '"\(.series) - S0\(.season_number)E00\(.episode_number) - \(.title).mp4"'
{
"description": "Bla Bla Bla",
"episode": "title1",
"episode_number": 6,
"season": "Chapter 1",
"season_number": 1,
"series": "ShowName",
"title": "title1",
"upload_date": "20181006"
}

P.S. my question is base on this question: Rename files based on JSON content

2
-1

I would make it with the find command in Linux to get the filenames out and parse it move the files to the new file name out called from jq.

I hope I can write tomorrow an example for you.

(My English is not the best)

Hope that helps you a little bit out for the start

Daniprog

Edit: use it with this command: find (your path to the files) -type f -name „*.mp4“ -exec mv {} (your new path of the files)(the strings out of the jq array) \;

1
  • This answer lacks the necessary details to be useful to anybody. It is probably better suited as a comment to the question. – Kusalananda Jul 13 at 7:40
1

I'm going to assume we're dealing with files in a single directory (the current directory) and that all JSON files are correct in terms of formatting and contents. I'm also assuming that our shell is bash.

It looks like you are trying to manually do zero-filling of integers for both the season number (to two digits) and the episode number (to three digits). We can do that a bit more properly with a printf format, but jq doesn't support that, so what we will create is a shell loop that call jq to parse out a shell command which, when evaluated, creates a shell variable newname with the correct new filename.

for name in *.info.json; do
    oldname=${name%.info.json}.mp4

    if [ ! -e "$oldname" ]; then
        printf 'No such file "%s"\n' "$oldname" >&2
        continue
    fi

    unset newname
    eval "$(
        jq -r '[
            "printf", "-v", "newname", "%s - S%.2dE%.3d - %s.mp4",
            .series, .season_number, .episode_number, .title
        ] | @sh' -- "$name"
    )"

    if [ -z "$newname" ] || [ -e "$newname" ]; then
        printf 'Can not rename "%s" to "%s"\n' "$oldname" "$newname" >&2
        continue
    fi

    mv -- "$oldname" "$newname"
done

For the given JSON example file, the jq command in this loop would create the string

'printf' '-v' 'newname' '%s - S%.2dE%.3d - %s.mp4' 'ShowName' 1 6 'title1'

The quoting is done by the @sh operator, which makes it safe to evaluate in the shell using eval.

This shell command would, in bash, create the variable newname with the contents

ShowName - S01E006 - title1.mp4

The loop also constructs the expected name of the file that needs to be renamed. If that name does not exist, a diagnostic message is printed and the loop simply skips to the next name.

Before actually renaming the file, a rudimentary sanity check on the new name is performed to make sure it's actually set to something and that the new name isn't already taken by some other file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.