Created
June 20, 2025 18:59
-
-
Save matheuseduardo/52078ce2e5e77492801d608048db56fa to your computer and use it in GitHub Desktop.
bash script to parse old files from JVC camera with movie files metadata
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Variável de controle de depuração | |
DEBUG=true | |
# Função para mensagens de depuração | |
deb() { | |
if [[ "$DEBUG" == true ]]; then | |
echo "$1" | |
fi | |
} | |
parse_moi() { | |
local filename="$1" | |
# Verifica se o arquivo existe | |
if [[ ! -f "$filename" ]]; then | |
echo "Erro: Arquivo '$filename' não encontrado." | |
return 1 | |
fi | |
# Verifica se o arquivo é legível | |
if [[ ! -r "$filename" ]]; then | |
echo "Erro: Arquivo '$filename' não é legível." | |
return 1 | |
fi | |
# Lê o arquivo em formato hexadecimal | |
deb "Lendo arquivo '$filename'..." | |
local hex_data | |
hex_data=$(od -An -tx1 -v "$filename" | tr -d ' \n') | |
if [[ -z "$hex_data" ]]; then | |
echo "Erro: Falha ao ler o arquivo ou arquivo vazio." | |
return 1 | |
fi | |
deb "Dados hexadecimais lidos: $hex_data" | |
# Extrai os campos com base nas posições dos bytes | |
deb "Extraindo campos..." | |
local version | |
version="${hex_data:0:4}" | |
deb "version = $version" | |
# Tamanho (bytes 2-5, unsigned long, big-endian) | |
local size | |
size=$((16#${hex_data:4:8})) | |
deb "size = $size" | |
# Ano (bytes 6-7, unsigned short, big-endian) | |
local year | |
year=$((16#${hex_data:12:4})) | |
deb "year = $year" | |
# Mês (byte 8, unsigned char) | |
local month | |
month=$((16#${hex_data:16:2})) | |
deb "month = $month" | |
# Dia (byte 9, unsigned char) | |
local day | |
day=$((16#${hex_data:18:2})) | |
deb "day = $day" | |
# Hora (byte 10, unsigned char) | |
local hour | |
hour=$((16#${hex_data:20:2})) | |
deb "hour = $hour" | |
# Minutos (byte 11, unsigned char) | |
local minutes | |
minutes=$((16#${hex_data:22:2})) | |
deb "minutes = $minutes" | |
# Milissegundos (bytes 12-13, unsigned short, big-endian) | |
local milliseconds | |
milliseconds=$((16#${hex_data:24:4})) | |
deb "milliseconds = $milliseconds" | |
# Segundos (milissegundos / 1000) | |
local seconds | |
seconds=$((milliseconds / 1000)) | |
deb "seconds = $seconds" | |
# Verifica se todos os campos foram extraídos corretamente | |
if [[ -z "$version" || -z "$size" || -z "$year" || -z "$month" || -z "$day" || -z "$hour" || -z "$minutes" || -z "$milliseconds" || -z "$seconds" ]]; then | |
echo "Erro: Falha ao extrair alguns campos. Verifique o formato do arquivo." | |
return 1 | |
fi | |
# Formata a saída como JSON | |
deb "Formatando saída JSON..." | |
printf '{ | |
"version": "%s", | |
"size": %d, | |
"year": "%s", | |
"month": "%02d", | |
"day": "%02d", | |
"hour": "%02d", | |
"minutes": "%02d", | |
"seconds": "%02d" | |
}\n' "$version" "$size" "$year" "$month" "$day" "$hour" "$minutes" "$seconds" | |
} | |
# Verifica se um argumento foi passado | |
if [[ $# -eq 0 ]]; then | |
echo "Erro: Nenhum arquivo especificado." | |
echo "Uso: $0 arquivo.moi" | |
exit 1 | |
fi | |
# Chama a função com o argumento fornecido | |
parse_moi "$1" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment