1

I am writing my first ever Nautilus script but I am not sure how to continue. What do I need to add to this nautilus script so that all MP4 files in the folder are deleted after FFMPEG finishes converting them to MOV?

#!/bin/bash

echo -e "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" | find '*.mp4'

for i in *.mp4; do ffmpeg -i "$i" -c:v copy -c:a pcm_s16le -ar 48000 -ac 2 "$(echo "$i"|cut -d\. -f1).mov";done

Thank you! :)

  • A possibly better way to remove and replace the file extension would be "${i%.*}.mov" (note this will remove the shortest matching suffix - which I'm assuming is what you really want - so for example other.file.mp4 becomes other.file.mov rather than other.mov) – steeldriver Aug 28 '18 at 17:30
2

Just remove (I would prefer with force option enabled) the file - $i:

#!/bin/bash

echo -e "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" | find '*.mp4'

for i in *.mp4 
do 
    ffmpeg -i "$i" -c:v copy -c:a pcm_s16le -ar 48000 -ac 2 "$(echo "$i"|cut -d\. -f1).mov"
    rm -f "$i"
done
  • If you wish you can replace the newlines with semicolon ; sign.

Another option is to move the file to the trash instead of delete it:

    gvfs-trash "$i"

In addition, your script can be improved in the following way:

#!/bin/bash -e

# Get the items selected in Nautilus as an array
IFS_BAK=$IFS
IFS=$'\t\n'
FILE_LIST=($NAUTILUS_SCRIPT_SELECTED_FILE_PATHS)
IFS=$IFS_BAK

# For each item in the array $FILE_LIST
for ((i=0; i<${#FILE_LIST[@]}; i++))
do
    # Get the file extension
    FILE_EXT="${FILE_LIST[$i]##*.}"

    # If the item is a file and its extension is mp4 
    if [[ -f ${FILE_LIST[$i]} ]] && [[ $FILE_EXT == 'mp4' ]]
    then
        # Get the filename
        FILE_NAME="${FILE_LIST[$i]%.*}"
        # Compose the name of the new file
        THE_NEW_FILE="${FILE_NAME}.mov"

        # Do the conversion
        ffmpeg -i "${FILE_LIST[$i]}" -c:v copy -c:a pcm_s16le -ar 48000 -ac 2 "$THE_NEW_FILE"

        # Remove the item: rm -f "${FILE_LIST[$i]}"; or move it to the trash:
        gvfs-trash "${FILE_LIST[$i]}"

        # Output a message, note ##*/ will remove the path from the filename
        notify-send "mp4 to mov" "${THE_NEW_FILE##*/} - was created\n${FILE_LIST[$i]##*/} - was moved to the Тrash"
    fi
done

Here is one related question: Script to merge Video and subs then delete the existing files

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.