Multimedia scripts
mouse 1621 · person cloud · link
Last update
2024-09-05
2024
09-05
«scripts per organizzare i files multimediali a suoceri e moglie»

MOGLIE / ANDROID


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# ----- FOTO -------------------------------------------------------------------
# resize f.jpg to _f.jpg
ruby -e '%w{fileutils shellwords}.each{|l| require l}; lista=Dir["**/*"].grep(/\.jpg/i).sort; lista.each_with_index{|f,i| '\
'puts "#{((i+1)/lista.size.to_f*100).to_i}%\t#{f}"; next if File.basename(f).start_with?("_"); system %Q[ '\
'vips jpegsave #{f.shellescape} /tmp/rz.jpg --Q 80 && '\
'jhead -autorot /tmp/rz.jpg && '\
'mv /tmp/rz.jpg #{File.dirname(f).shellescape}/_#{File.basename(f).shellescape} && '\
'rm -f #{f.shellescape} || echo ERRORE ] }'
# rename by exif/file date
ruby -e 'lista=Dir["**/*"].grep(/\.jpg/i).sort; lista.each_with_index{|f,i| '\
'print "#{((i+1)/lista.size.to_f*100).to_i}%\t"; '\
'system %Q[jhead -n"%Y-%m-%d_%H-%M-%S" "#{f}"] }'
## OR rename _f.jpg to f.jpg
#ruby -e 'lista=Dir["**/*"].grep(/\.jpg/i).sort; lista.each_with_index{|f,i| '\
#'puts "#{((i+1)/lista.size.to_f*100).to_i}%\t#{f}"; next unless File.basename(f).start_with?("_"); '\
#'File.rename(f, File.join(File.dirname(f), File.basename(f).sub(/^_/, ""))) }'

# crea sottocartelle foto e video (per ogni cartella avente files video)
ruby -e 'Dir["**/*"].grep_v(/\.jpg/i).map{|f| File.dirname(f) if File.file?(f) }.compact.sort.uniq.each{|d| '\
'Dir.chdir(d){ files = Dir["*"]; system "mkdir -p foto video"; '\
'files.each{|f| dst = (f =~ /\.jpg/i ? :foto : :video); File.rename(f, "#{dst}/#{f}") } } }'

# zippa cartelle di sole foto
ruby -e 'require "shellwords"; Dir["**/*"].grep(/\.jpg/i).map{|f| File.dirname(f) }.compact.sort.uniq.each{|d| '\
'puts d; Dir.chdir(File.join d, ".."){ n=File.basename(d); system "zip -r #{n.shellescape}.zip #{n.shellescape} && rm -rf #{n.shellescape}" } }'


# ----- AUDIO ------------------------------------------------------------------
# converti video neri in MP3  =>  ffmpeg -i in.mp4 -ab 128k out.mp3
mdkir audio && cd audio
for i in *.mp4; ffmpeg -i $i -ab 128k (string replace mp4 mp3 $i); end # fish
#for i in *.mp4; do ffmpeg -i "$i" -ab 128k "${i%.*}.mp3"; done # bash
rm -f *.mp4


# ----- VIDEO ------------------------------------------------------------------
# converti video MP4 in AVI/h264
for i in (find -type f -iname "*.mp4" -printf '%s\t%p\n' | sort -n | cut -f 2)
  echo $i
  vid2all -l -v 2048 -a 192 --no-expand --no-resize $i /mnt/ramd/out.avi \
    && mv /mnt/ramd/out.avi (string replace -i mp4 avi $i) \
    || touch (string replace -i mp4 ERR $i)
end

find -type f -name "*.ERR"

# sposta tutti gli MP4
for i in (find -type f -iname "*.mp4"|sed -r 's/^..//'); mv -i $i (echo $i|sed -r 's/ /_/g ; s/\//--/g'); end

SUOCERI / WINDOWS


external tools

comprimi_media

1
2
3
4
5
6
7
8
9
10
11
#!/bin/env ruby
dirs = %x[ find -mindepth 1 -maxdepth 1 -type d ].split("\n").sort

dirs.each do |d|
  puts "===== CARTELLA [ #{d} ] ====="
  print 'foto : '; system 'ls|egrep -i "(jpe*g|png|heic)$"|wc -l', chdir: d
  print 'video: '; system 'ls|egrep -i "(mp4|mov|avi|mpeg|hevc)$"|wc -l', chdir: d
  system 'comprimi_media.single', chdir: d
end

puts "\n\nFINE"

comprimi_media.single

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/bin/env ruby

%w{ shellwords FileUtils progressbar }.each{|l| require l }

pb_opts = { progress_mark: '#', remainder_mark: '_', length: 79,
            starting_at: 0, format: '%t: %J
[%B] %e'
} all_files = Dir['*'].sort all_files.grep(/\.(aae)$/i).each{|f| FileUtils.rm_f f } re_images = /\.(jpe*g|png|heic)$/i files = all_files.grep(re_images) if files.size > 0 puts '' FileUtils.mkdir_p '_fatti' FileUtils.mkdir_p 'foto' pb = ProgressBar.create pb_opts.merge(title: "#{files.size} immagini", total: files.size) files.each do |f| pb.log " => #{f}" fout = f.sub(re_images, '.jpg') system %Q| convert -quality 80 #{f.shellescape} foto/#{fout.shellescape} | FileUtils.mv f, "_fatti/#{f}" if $?.to_i == 0 pb.increment end end re_videos = /\.(mp4|mov|avi|mpeg|hevc)$/i files = all_files.grep(re_videos) if files.size > 0 puts '' FileUtils.mkdir_p '_fatti' FileUtils.mkdir_p 'video' pb = ProgressBar.create pb_opts.merge(title: "#{files.size} video", total: files.size) files.each do |f| pb.log " => #{f}" fout = f.sub(re_videos, '.mp4') # https://superuser.com/questions/326629/how-can-i-make-ffmpeg-be-quieter-less-verbose system %Q| ffmpeg -hide_banner -loglevel error -stats -i #{f.shellescape} -c:v libx264 -c:a libmp3lame -b:a 128k video/#{fout.shellescape} | FileUtils.mv f, "_fatti/#{f}" if $?.to_i == 0 pb.increment end end if Dir['*'].reject{|f| File.directory? f }.size == 0 # elinmina fatti se tutto e' andato a buon fine FileUtils.rm_rf '_fatti' # elimina l'unica cartella presente dopo averne spostato i files nel parent if Dir['*'].size == 1 dir = Dir['*'].first system "mv -i * ..", chdir: dir FileUtils.rm_rf dir if $?.to_i == 0 end end # mostra evntuali cartelle _fatti ancora presenti dirs = %x[ find -mindepth 1 -type d -name _fatti ].split("\n").sort.map{|d| File.dirname d[2,300] } if dirs.size > 0 puts "Cartelle da controllare:" puts dirs end

misc ruby

1
2
3
4
5
# rename_foto_by_ts
ruby -e 'Dir["**/*"].grep(/\.jpg/i).sort.each{|f| system %Q[jhead -n"%Y-%m-%d_%H-%M-%S" "#{f}"] }'

# enumerate files
Dir['*'].sort.each_with_index{|f, i| File.rename f, "#{'%04d' % (i+1)}.mp4"}; nil

misc shell

1
2
3
4
5
# PDF to JPG
ls *.pdf | sed -r 's/(.+).pdf/pdftoppm -jpeg \0 \L\1.jpg/' | sh

# lossless rotate JPG
jpegtran -rotate 90 in.jpg > out.jpg