Renommer les fichiers GoPro

L'ordre alphabéthique des fichiers GoPro ne correspond pas à l'ordre dans lequel ils ont été enregistrés.

$ ls
GH014910.MP4
GH014911.MP4
GH014912.MP4
GH024910.MP4
GH024912.MP4
GH034912.MP4

Pour renommer les fichiers GoPro dans l'ordre, enregistrez et exécutez ce script python :

from os import listdir
from os.path import isfile, join
import re

def get_files(directory = '.'):
    return [f for f in listdir(directory) if isfile(join(directory, f))]

def filter_ghmp4(files_list):
    r = re.compile("GH[0-9]{6}\.MP4")
    return list(filter(r.match, files))

def logic_name(ghmp4_name):
    video_number = ghmp4_name[4:8]
    chunk_number = ghmp4_name[2:4]
    return 'LN{video_number}-{chunk_number}.MP4'.format(video_number=video_number, chunk_number=chunk_number)

def create_logic_names(ghmp4_list):
    new_names = {}
    for name in ghmp4_list:
        new_name = logic_name(name)
        new_names[new_name] = name
    return new_names

def print_suggestion(logic_names):
    new_names_list = [f for f in logic_names]
    sorted_new_names_list = sorted(new_names_list)
    for new_name in sorted_new_names_list:
        print(logic_names[new_name] + ' -> ' + new_name)

def print_command_to_rename(logic_names):
    new_names_list = [f for f in logic_names]
    for new_name in new_names_list:
        print('mv ' + logic_names[new_name] + ' ' + new_name)

def new_file_already_exist(logic_names, directory = '.'):
    already_exist = False
    files = get_files(directory)
    new_names_list = [f for f in logic_names]
    for new_name in new_names_list:
        if new_name in files:
            print('Warning: ' + new_name + ' already exist')
            already_exist = True
    return already_exist

if __name__ == '__main__':
    directory = '.'
    files = get_files(directory)
    ghmp4 = filter_ghmp4(files)
    if not ghmp4:
        exit('No files found')
    logic_names = create_logic_names(ghmp4)

    print ('Suggestion :')
    print_suggestion(logic_names)

    if new_file_already_exist(logic_names, directory):
        print('Rename existing files')
    else:
        print ('Commands to execute :')
        print_command_to_rename(logic_names)

Le script vous propose de les renommer de façon logique, ainsi que les commandes à exécuter pour cela :

$ python logicname.py 
Suggestion :
GH014910.MP4 -> LN4910-01.MP4
GH024910.MP4 -> LN4910-02.MP4
GH014911.MP4 -> LN4911-01.MP4
GH014912.MP4 -> LN4912-01.MP4
GH024912.MP4 -> LN4912-02.MP4
GH034912.MP4 -> LN4912-03.MP4
Commands to execute :
mv GH014912.MP4 LN4912-01.MP4
mv GH034912.MP4 LN4912-03.MP4
mv GH014911.MP4 LN4911-01.MP4
mv GH024912.MP4 LN4912-02.MP4
mv GH024910.MP4 LN4910-02.MP4
mv GH014910.MP4 LN4910-01.MP4

Copiez-collez les commandes dans votre terminal, exécutez-les, et le tour est joué !

$ mv GH014912.MP4 LN4912-01.MP4
mv GH034912.MP4 LN4912-03.MP4
mv GH014911.MP4 LN4911-01.MP4
mv GH024912.MP4 LN4912-02.MP4
mv GH024910.MP4 LN4910-02.MP4
mv GH014910.MP4 LN4910-01.MP4
$ ls
LN4910-01.MP4
LN4910-02.MP4
LN4911-01.MP4
LN4912-01.MP4
LN4912-02.MP4
LN4912-03.MP4

L'ordre alphabetique de vos fichiers GoPro correspond maintenant à l'ordre dans lequel ils ont été enregistrés.