Ravas coding goodies

For discussions about programming and projects not necessarily associated with Porteus.
User avatar
Rava
Contributor
Contributor
Posts: 4064
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.0 x86_64 + 4.0 i586
Location: Forests of Germany

Ravas coding goodies

Post#121 by Rava » 23 Aug 2022, 11:44

Since I mentioned my lsfind "suite" in some other thread, here small examples when using parts of the "suite".
This might be at times complicated or confusing; remember: I coded the lsfind suite for myself and therefore never worked on anything like user friendliness.
So this time it's more of a "coding badly" :lol:

luh is short for "Look for external Usb thumb-drives and external Hard-disks"
It is an alias for two other aliases:

Code: Select all

root@porteus:/Lsfind# type luh
luh is aliased to `lu;lh'
root@porteus:/Lsfind# type lu
lu is aliased to `locate_extUSB.sh |/bin/grep --color=never " device[s ][ i][as]"'
root@porteus:/Lsfind# type lh
lh is aliased to `locate_extHD.sh |/bin/grep --color=never " device[s ][ i][as]"'
Me lazy much? No, I thought having a very long alias luh combining both codes is more strain on the memory than nesting the aliases to other aliases. It might take a tiny bit more time in execution, but me presumes it spares a bit of main memory.

Code: Select all

root@porteus:/Lsfind# luh
extHD is extHD8 - devices are /mnt/sdb1 /mnt/sdb2 /mnt/sdb3 /mnt/sdb4
only reports the extHD8, since the USB thumb-drive is one of the non numbered ones, thes my "suite" not recognize by itself.

Therefore it must be done manually, and I let the script "lsfind-extXYZ.sh " explain what it needs.

lsfind sits in /usr/local/bin, as well as its "helper" scripts that are uses to determine how many partitions the known internal or external hard-disks or thumb-drives have.
The scripts themselves that create the data (*ls.zg and *find.gz) all sit in my ext2 container mounted in /Lsfind - since I do want that database in one definite place (better to share the most recent databases abong my PC systems)

The script for the internal lsfind is only valid for the intHDD that is the one the PC itself has.

In case of my current system - intHDD1 - it is made like so:

Code: Select all

root@porteus:/Lsfind# l lsfind-int*
-rwxr-xr-x 1 root 5778 2021-02-17 08:10 lsfind-intHDD.sh
-rw-r--r-- 1 root 5778 2021-02-17 08:10 lsfind-intHDD.shBAK
root@porteus:/Lsfind# l .lsfind-intHD*sh
-rwxr-xr-x 1 root 5052 2018-02-24 05:18 .lsfind-intHDC1.sh
As you see, only one valid "lsfind-int*" script, the other one is "deactivated" by making it hidden.

Now for the rest: manually executing lsfind via the helper script ./lsfind-extXYZ.sh

Code: Select all

root@porteus:/Lsfind# l /mnt/sdc1/extUSBXX*
-rwxrwxrwx 1 guest 0 2022-02-11 10:40 /mnt/sdc1/extUSBXX
-rwxrwxrwx 1 guest 0 2016-12-26 10:32 /mnt/sdc1/extUSBXX_Hama_Blau_08GB
root@porteus:/Lsfind# ./lsfind-extXYZ.sh 
lsfind-extXYZ.sh V0.8
Error: 2 parameters needed:
$1 the valid mount folder of the extXYZ drive, e.g. /mnt/sdc1
$2 the name of the extXYZ drive, e.g. extUSBXZ, extUSBXX2, etc.
Cave! For now there is no sanity check!

(check which drive it is with alias "extHD+USB") or with this ls instead:
ls --color=auto /mnt/sd[a-z][1-9]/ext[HU][DS]* /mnt/sd[a-z]/ext[HU][DS]* 2>/dev/null

If you need to add a 2nd partition do it like so after running this script
(assuming you have USBXX1 mounted on /mnt/sdb1 & /mnt/sdb2):
df -mT /mnt/sdb2/ |/bin/grep sdb2 >>extUSBXX1.df
lsfind /mnt/sdb2 extUSBXX1_2
root@porteus:/Lsfind# ./lsfind-extXYZ.sh /mnt/sdc1 extUSBXX
lsfind-extXYZ.sh V0.8
Target folder given is: /mnt/sdc1 - extUSB Name given is: extUSBXX
/mnt/sdc1/extUSBXX
/mnt/sdc1/extUSBXX_Hama_Blau_08GB
Sleeping 3 seconds. Abort now with Ctrl+C !
starting lsfind: mountpath: /mnt/sdc1 - drive name: extUSBXX
schreibe... /tmp/4893/extUSBXX.ls
schreibe... /tmp/4893/extUSBXX.find
gzip -f /tmp/4893/extUSBXX.ls
gzip -f /tmp/4893/extUSBXX.find
writing extUSBXX.df: will only look for one partition: /mnt/sdc1
If you need to add a 2nd partition do it like so after running this script:
(assuming you have USBXX1 mounted on /mnt/sdb1 & /mnt/sdb2):
df -mT /mnt/sdb2/ |/bin/grep sdb2 >>extUSBXX1.df
lsfind /mnt/sdb2 extUSBXX1_2
lsfind itself is less user friendly, since I coded all that for myself:

Code: Select all

root@porteus:/Lsfind# lsfind
no given directory or given directory not found- aborting!
Despite what it might seem, lsfind wants its parameter exactly the way ./lsfind-extXYZ.sh explained it above:

Code: Select all

lsfind /mnt/sdb2 extUSBXX1_2
as in /mnt/sdb2 -> valid mount path
extUSBXX1_2 -> appropriate name for the extUSBXX1_2.ls.gz and extUSBXX1_2.find.gz database files.

The scripts giving the info to lsfind - or when the user runs lsfind by itself have to make sure all info given to lsfind is correct, lsfind is stupid in itself and just does the ls and the find where it got told the mount point is and gives the databases the names it got told to use.

When the mount point is incorrect or you choose a wrong name for the database then confusion can occur.

E.g. when you let lsfind write into intHDA1* database files when instead the correct name would have been extHD1*.

Searching later for files would give you the results in "intHDA1_1" - but when you look there, the consent would be different, since the drive was instead the external one "extHD1_1".
Cheers!
Yours Rava

User avatar
Rava
Contributor
Contributor
Posts: 4064
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.0 x86_64 + 4.0 i586
Location: Forests of Germany

Ravas coding goodies

Post#122 by Rava » 23 Aug 2022, 21:06

After years of using my make-ffplay-script I just now stumbled upon mp3 files that my script thinks it's a video - because these files have a small image of the album cover embedded - and the way I coded the distinction between "video" and "audio only" means - such mp3's are seen as video and the album cover will be displayed by default.

As my help.ffplay explains the essentials to me how - including how to switch modes:

Code: Select all

root@porteus:/Lsfind# help.ffplay
help.ffplay V0.2
9, 0      decrease and increase volume respectively
/, *      decrease and increase volume respectively

ffplay-hide_banner:
ffplay -autoexit -hide_banner -i FILE

a   Cycle audio channel
t   Cycle subtitle channel

ffplay-hide_banner-drawtext ●● needs background fix ●●:
ffplay -autoexit -hide_banner -vf "drawtext=text='%{pts\:hms}'" -vf "drawtext=box" -i
(The original help.ffplay - like all my help.* scripts use colours for highlighting stuff which I cannot display by simply using [ code ] )
Cheers!
Yours Rava

User avatar
Rava
Contributor
Contributor
Posts: 4064
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.0 x86_64 + 4.0 i586
Location: Forests of Germany

Ravas coding goodies

Post#123 by Rava » 26 Aug 2022, 08:21

tail+tee - coding-challenge - the first ever coding challenge for you fine Porteus folks by yours truly…

This is the script I use for logging /var/log/messages, it sits locally in /mnt/sda4/backup/ and needs to be executed via ./tail+tee_VLM_intHDD1_4.sh - I do not want it in $PATH

Code: Select all

!/bin/bash
mydate=$(date +%Y-%m-%d)
echo mydate suffix I am using: $mydate
myfile=/mnt/sda4/backup/var_log_messages_$mydate
echo myfile I am using: $myfile
echo $(date +%d.%m.%Y\ %H:%M:%S) started: $0 >>$myfile
echo '# cat /etc/porteus/*' >>$myfile
cat /etc/porteus/* >>$myfile
echo '# uname -r' >>$myfile
uname -r >>$myfile
tail -fn 10 /var/log/messages|tee -a $myfile
I should have used logpath as variable, like so

Code: Select all

!/bin/bash
logpath=/mnt/sda4/backup/
mydate=$(date +%Y-%m-%d)
echo mydate suffix I am using: $mydate
myfile=${logpath}var_log_messages_$mydate
[rest same as above]
Since I coded it for myself, I obtained in using any checks, like, is ${logpath} a valid path via test -d $logpath - but it could still go amiss even when $logpath is a valid path.
Also, the script can only be executed by root (normal users have no read access into /var/log/messages:

Code: Select all

guest@porteus:~$ tail /var/log/messages
tail: cannot open '/var/log/messages' for reading: Permission denied
No check if the user executing ./tail+tee_VLM_intHDD1_4.sh is root.

Since I coded it for myself, I have no issues omitting these sanity tests.

Now, it would make me very happy if one of you folks would be up to the challenge in enhancing the script, include the logpath= as variable like I already did in the part [ code ] snippet, but also include some more sanity tests:

☯ is the user root? If not, exit with a warning (not give the option to su as root)
☯ is ${logpath} a valid path
☯ is ${logpath} writeable (even for root it could be he has no write access - e.g. when the whole partition is mounted read-only)
☯ At least check via the first write command into $myfile if that was a success (via checking in the very next line if $? is zero or non-zero)

and as the icing of the cake:
☯ make a check if at least 500 MB is free on the partition where ${logpath} is located - of all code stuff I think this is the hardest to pull of, but your mileage may vary.

Anyhow, I hope at least one is up to the challenge. If you only learn by reading others code but never try solving a given issue you most probably enhance your coding abilities less than you could if you have challenges (more than the ones you give yourself - unless these go to the borders of your capabilities, they you already train the best way) once in a while.
___________________________________________

When you want to delete the current contents of that saved data (because no issues cam up so far and you want too keep the file small since it is easier to look into, and it saves space), do not delete it. That brakes the handling of tee and it will no longer log anything in the directed file, it will also not complain about any issues.

When you want to reset all data, do this instead:

Code: Select all

root@porteus:/4/backup# echo >var_log_messages_2022-08-26
root@porteus:/4/backup#
when "/4/backup" represents your ${logpath} and var_log_messages_2022-08-26 represents the used var_log_messages_$mydate .

Rava out.
Cheers!
Yours Rava

User avatar
Rava
Contributor
Contributor
Posts: 4064
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.0 x86_64 + 4.0 i586
Location: Forests of Germany

Ravas coding goodies

Post#124 by Rava » 26 Aug 2022, 08:21

Once again about my lsfind "suite" - see here:

Now the automated recognition of partitions - as example the extHD8 - since currently I always use the extUSB_FlashDisks that are non-numbered and must be handled manually, while all numbered extUSB_FlashDisks and all extHDs are handled automatically.

See for yourself how the lsfind works for itself and what the results look like:

Code: Select all

root@porteus:/Lsfind# ./lsfind-extHD8.sh 
lsfind_extHD8.sh V0.5
testing for device .. devices are /mnt/sdb1 /mnt/sdb2 /mnt/sdb3 /mnt/sdb4
schreibe... /tmp/5509/extHD8_1.ls
schreibe... /tmp/5509/extHD8_1.find
gzip -f /tmp/5509/extHD8_1.ls
gzip -f /tmp/5509/extHD8_1.find
schreibe... /tmp/5524/extHD8_2.ls
schreibe... /tmp/5524/extHD8_2.find
gzip -f /tmp/5524/extHD8_2.ls
gzip -f /tmp/5524/extHD8_2.find
schreibe... /tmp/5539/extHD8_3.ls
schreibe... /tmp/5539/extHD8_3.find
gzip -f /tmp/5539/extHD8_3.ls
gzip -f /tmp/5539/extHD8_3.find
schreibe... /tmp/5580/extHD8_4.ls
schreibe... /tmp/5580/extHD8_4.find
gzip -f /tmp/5580/extHD8_4.ls
gzip -f /tmp/5580/extHD8_4.find
df_extHD.sh V0.6
./df_extHD.sh: line 53: [: syntax error: `-gt' unexpected
chosen extHD is extHD8 which has 4 partitions.
testing for device .. devices are /mnt/sdb1 /mnt/sdb2 /mnt/sdb3 /mnt/sdb4
________________________________________________________________________________
Executing ./lsfind-extHD8.sh took 00:06:20 …
All seems fine. Exit now.
root@porteus:/Lsfind# l extHD8*
-rw-r--r-- 1 root     486 2022-08-26 09:48 extHD8.df
-rw-r--r-- 1 root   73128 2022-08-26 09:41 extHD8_1.find.gz
-rw-r--r-- 1 root  125904 2022-08-26 09:41 extHD8_1.ls.gz
-rw-r--r-- 1 root     164 2020-03-26 00:23 extHD8_1_du.dirm
-rw-r--r-- 1 root  142136 2022-08-26 09:42 extHD8_2.find.gz
-rw-r--r-- 1 root  276924 2022-08-26 09:42 extHD8_2.ls.gz
-rw-r--r-- 1 root     143 2020-03-26 00:23 extHD8_2_du.dirm
-rw-r--r-- 1 root 3214652 2022-08-26 09:47 extHD8_3.find.gz
-rw-r--r-- 1 root 5248143 2022-08-26 09:46 extHD8_3.ls.gz
-rw-r--r-- 1 root     198 2020-04-12 15:38 extHD8_3_du.dirm
-rw-r--r-- 1 root 2373065 2022-08-26 09:48 extHD8_4.find.gz
-rw-r--r-- 1 root 4164872 2022-08-26 09:48 extHD8_4.ls.gz
-rw-r--r-- 1 root     183 2020-03-26 00:24 extHD8_4_du.dirm
I have yet to code the "du.dirm" into lsfind so that has to be done manually as well.

It makes much sense to include it into the suit, since after the ls and find the whole drive with all sub-folders got read, and running a du -m afterwards means most data is in the current cache anyway.
Cheers!
Yours Rava

User avatar
Rava
Contributor
Contributor
Posts: 4064
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.0 x86_64 + 4.0 i586
Location: Forests of Germany

Ravas coding goodies

Post#125 by Rava » 30 Aug 2022, 09:03

Updated make-ffplay-script to V3.49
reason: added .ogg file format

the diff

Code: Select all

root@porteus:/mnt/live/memory/images# diff /usr/local/bin/make-ffplay-script 991-usr_local_bin_RECENT.xzm/usr/local/bin/make-ffplay-script 
2c2
< MYVERSION=V3.49 # please add V at the beginning - newest info at bottom!
---
> MYVERSION=V3.48 # please add V at the beginning - newest info at bottom!
5c5
< # avi 	flv	mp3	mp4	mpg	mkv	ogv	TS	ts	webm	divx 	VOB	vob 	ogg
---
> # avi 	flv	mp3	mp4	mpg	mkv	ogv	TS	ts	webm	divx 	VOB	vob
11d10
< # V3.49 added .ogg file format
36c35
< 	find "$PARAMETER" |grep -E "\.avi$|\.flv$|\.mp[34g]$|\.mkv$|\.ogv$|\.[tT][sS]$|\.webm$|\.divx$|\.[vV][oO][bB]$|\.ogg$" |sort | while read line ; do
---
> 	find "$PARAMETER" |grep -E "\.avi$|\.flv$|\.mp[34g]$|\.mkv$|\.ogv$|\.[tT][sS]$|\.webm$|\.divx$|\.[vV][oO][bB]$" |sort | while read line ; do
The whole code:

Code: Select all

#!/bin/sh
MYVERSION=V3.49 # please add V at the beginning - newest info at bottom!
# V3.4 - $1 is the relative folder with the files, will be stripped of any "/" - e.g. Filme or Filme/ works but not /mnt/sda1/video/Filme nor /mnt/sda1/video/Filme/
# for now supported fi le endings are
# avi 	flv	mp3	mp4	mpg	mkv	ogv	TS	ts	webm	divx 	VOB	vob 	ogg
# V3.4: now added "chmod -v a+x "$PARAMETER".sh - will create error message on NTFS or VFAT fs, necessary for Linux fs like ext2,ext3 etcetera.
# V3.5: now uses ffplay-hide_banner script instead of using ffplay itself. ffplay-hide_banner distinguishes between video and media-file that is sound only. In the latter case the following is added: -showmode waves -x 500 -y 150 
# V3.46 changed into using script ffplay-hide_banner-fs (start in -fs aka full-screen mode for videos - nothing changed for audio-only-files) instead of script ffplay-hide_banner-
# V3.47 back to direct call of ffplay via function
# V3.48 using "double quotes" because of consecutive whitespaces in filename bug
# V3.49 added .ogg file format
echo make-ffplay-script $MYVERSION

if [ $# -ne 1 ]; then
	echo 'No parameter given. Need one parameter - the relative Folder e.g. "folder/".
Abort.'
	exit 2
fi

PARAMETER=$(echo "$1"|sed 's|/||g')
if [ -d "$PARAMETER" ]; then
	echo working on "$PARAMETER"… writing into "$PARAMETER".sh
	echo '#!/bin/sh
echo "$0"' >"$PARAMETER".sh
echo '# made by make-ffplay-script '$MYVERSION '- blame Rava' >>"$PARAMETER".sh
echo 'function myffplay {
ffprobe -hide_banner 2>&1 "$*" |grep Stream|grep "Video:" >/dev/null
if [ $? -eq 0 ]; then
	# return zero -> video file: default
	ffplay -autoexit -hide_banner -fs -i "$*"
else
	# return non-zero -> audio only - adjusting…
	ffplay -make-ffplay-scriptautoexit -hide_banner -showmode waves -x 500 -y 150 -i "$*"
fi
}' >>"$PARAMmake-ffplay-scriptETER".sh
	find "$PARAMETER" |grep -E "\.avi$|\.flv$|\.mp[34g]$|\.mkv$|\.ogv$|\.[tT][sS]$|\.webm$|\.divx$|\.[vV][oO][bB]$|\.ogg$" |sort | while read line ; do
{make-ffplay-script
 echo myffplay \""${line}\"" >>"$PARAMETER".sh
} ; done
echo >>"$PARAMETER".sh
	chmod -v a+x "$PARAMETER".sh
else
	echo Given parameter "$PARAMETER" is not a directory. Abort.
	exit 1
fi
Cheers!
Yours Rava

User avatar
Rava
Contributor
Contributor
Posts: 4064
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.0 x86_64 + 4.0 i586
Location: Forests of Germany

Ravas coding goodies

Post#126 by Rava » 30 Aug 2022, 20:33

Embarrassing to admit but I forgot to add aac - now make-ffplay-script is V3.50

Code: Select all

#!/bin/sh
MYVERSION=V3.50 # please add V at the beginning - newest info at bottom!
# V3.4 - $1 is the relative folder with the files, will be stripped of any "/" - e.g. Filme or Filme/ works but not /mnt/sda1/video/Filme nor /mnt/sda1/video/Filme/
# for now supported file endings are
# avi 	flv	mp3	mp4	mpg	mkv	ogv	TS	ts	webm	divx 	VOB	vob 	ogg 	aac
# V3.4: now added "chmod -v a+x "$PARAMETER".sh - will create error message on NTFS or VFAT fs, necessary for Linux fs like ext2,ext3 etcetera.
# V3.5: now uses ffplay-hide_banner script instead of using ffplay itself. ffplay-hide_banner distinguishes between video and media-file that is sound only. In the latter case the following is added: -showmode waves -x 500 -y 150 
# V3.46 changed into using script ffplay-hide_banner-fs (start in -fs aka full-screen mode for videos - nothing changed for audio-only-files) instead of script ffplay-hide_banner-
# V3.47 back to direct call of ffplay via function
# V3.48 using "double quotes" because of consecutive whitespaces in filename bug
# V3.49 added .ogg file format
# V3.50 added .aac file format
echo make-ffplay-script $MYVERSION

if [ $# -ne 1 ]; then
	echo 'No parameter given. Need one parameter - the relative Folder e.g. "folder/".
Abort.'
	exit 2
fi

PARAMETER=$(echo "$1"|sed 's|/||g')
if [ -d "$PARAMETER" ]; then
	echo working on "$PARAMETER"… writing into "$PARAMETER".sh
	echo '#!/bin/sh
echo "$0"' >"$PARAMETER".sh
echo '# made by make-ffplay-script '$MYVERSION '- blame Rava' >>"$PARAMETER".sh
echo 'function myffplay {
ffprobe -hide_banner 2>&1 "$*" |grep Stream|grep "Video:" >/dev/null
if [ $? -eq 0 ]; then
	# return zero -> video file: default
	ffplay -autoexit -hide_banner -fs -i "$*"
else
	# return non-zero -> audio only - adjusting…
	ffplay -autoexit -hide_banner -showmode waves -x 500 -y 150 -i "$*"
fi
}' >>"$PARAMETER".sh
	find "$PARAMETER" |grep -E "\.avi$|\.flv$|\.mp[34g]$|\.mkv$|\.ogv$|\.[tT][sS]$|\.webm$|\.divx$|\.[vV][oO][bB]$|\.ogg$|\.aac$" |sort | while read line ; do
{
 echo myffplay \""${line}\"" >>"$PARAMETER".sh
} ; done
echo >>"$PARAMETER".sh
	chmod -v a+x "$PARAMETER".sh
else
	echo Given parameter "$PARAMETER" is not a directory. Abort.
	exit 1
fi
Cheers!
Yours Rava

User avatar
Rava
Contributor
Contributor
Posts: 4064
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.0 x86_64 + 4.0 i586
Location: Forests of Germany

Ravas coding goodies

Post#127 by Rava » 10 Sep 2022, 07:11

known bug of my make-ffplay-script (all version up to the most recent one)

neither the folder nor the multimedia files therein can contain ' nor " - I had to change some names e.g.

Code: Select all

the whatever's whatever
or

Code: Select all

say "today" to me
into less correct

Code: Select all

the whatevers whatever
say today to me
because I did not manage to fix the issue in a mere few half hours of trying to fix it and was too busy elsewhere to delve that deep into it to really support all kinds of UNIX-valid paths or filenames. Image
Cheers!
Yours Rava

User avatar
gnintilgyes
White ninja
White ninja
Posts: 20
Joined: 14 Sep 2022, 17:52
Distribution: Porteus v5 MATE/Cinnamon

Ravas coding goodies

Post#128 by gnintilgyes » 15 Sep 2022, 15:47

I should have looked through the whole topic but actually saw the first two pages and the last page. Trying to make some sense out of this stuff to see how I could use it. You're very dedicated to your Linux system, making me look like I'm unappreciative and like I know almost nothing about programming.

Resuming watching a movie… (Post by Rava #68506)

The example here is something I could use but I might have to change "Xdialog" to "zenity". Came across a Linux distro (not the one shown as "Distribution" next to my avatar) that didn't come with "xmessage" but did have "zenity". Which reminds me of something... I'm going to start a thread soon.

Ravas coding goodies (Post by Rava #76848)

From this link, I needed something that worked on Void Linux. :o Also to set the master volume to maximum, while adjusting "playback" volume from panel icon slider.

Ravas coding goodies (Post by Rava #89548)

This one makes "ffplay" program more useful to me, thank you!
__________

The "bash" script language is a PITA to me. Sometimes I write something simple such as renaming a bunch of files, and when I run the "sh" script the processor complains. I keep forgetting the Linux terminal is not like "cmd.exe", on the former, have to replicate command lines for the sake of printing, one of the lines must be preceded by "echo". So I have to deal with the stony silence sometimes for a few seconds before getting back the terminal prompt after it renamed a few hundred files or something else. The "ls" command is unusable to me sometimes when I need full paths entered into a string array. That's why I have done it in Lua which is easier for me, although I figured it out "the other way" even though I was forced also to employ "gawk".

I have never written by own "case... esac" line, never bothered to write my own function and have yet to get around a few commands like "fg". As I've said, compared to you, I know almost nothing about programming in "bash" and feel ashamed. I felt I had to say something since you were complaining in the first few pages that nobody cared or noticed what you were doing here. That could be because this forum population is rather small.

User avatar
Rava
Contributor
Contributor
Posts: 4064
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.0 x86_64 + 4.0 i586
Location: Forests of Germany

Ravas coding goodies

Post#129 by Rava » 15 Sep 2022, 15:58

gnintilgyes wrote:
15 Sep 2022, 15:47
I have never written by own "case... esac" line, never bothered to write my own function and have yet to get around a few commands like "fg"
Functions are very neat, especially when you have similar tasks that need slighly different input but what is transformed is the same (just the end result differs when the input is different)
You don't have to feel ashamed, you go wherever you go, and use what suits you best. For me, bash scripts are quite easy and I have some that took a while to code, but they solve recurring data processing, and in the end, be it 6 hours or 3 days it took me coding these scripts, now I just have to execute the script and the task is done in a mere second or a few minutes tops, while manually doing it again and again would have cost me much more hours in the long run.

By design, any OS should be easy to automatise since that is the great advantage of anything IT - you give it some data, and it processes it for you, like rotating a video using ffmpeg
Rotation via transpose
0 = 90° CounterClockwise and Vertical Flip (default):
ffmpeg -v error -i in.mp4 -vf "transpose=0" out.mp4
1 = 90° Clockwise:
ffmpeg -v error -i in.mp4 -vf "transpose=1" out.mp4
2 = 90° CounterClockwise:
ffmpeg -v error -i in.mp4 -vf "transpose=2" out.mp4
3 = 90° Clockwise and Vertical Flip:
ffmpeg -v error -i in.mp4 -vf "transpose=3" out.mp4
or the use of metadata:
extracting an ffmetadata file
ffmpeg -i INPUT -f ffmetadata FFMETADATAFILE

Reinserting edited metadata information from the FFMETADATAFILE file
ffmpeg -i INPUT -i FFMETADATAFILE -map_metadata 1 -codec copy OUTPUT
:)
Cheers!
Yours Rava

User avatar
Rava
Contributor
Contributor
Posts: 4064
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.0 x86_64 + 4.0 i586
Location: Forests of Germany

Ravas coding goodies

Post#130 by Rava » 18 Sep 2022, 15:26

There is one thing not working with ffplay - playing music on console - meaning the Virtual Console, not some xterminal:

Code: Select all

guest@porteus:/7/sound$ ffplay Violet\ Evergarden\ Automemories\ OST\ WithoutSinging.aac
Could not initialize SDL - No available video device
(Did you set the DISPLAY variable?)
guest@porteus:/7/sound$ mpv Violet\ Evergarden\ Automemories\ OST\ WithoutSinging.aac
[ffmpeg/demuxer] aac: Estimating duration from bitrate, this may be inaccurate
 (+) Audio --aid=1 (aac 2ch 44100Hz)
AO: [pulse] 44100Hz stereo 2ch float
A: 00:00:48 / 01:06:36 (1%)
at least mpv works.

Of couse, mpg123 or mpg321 would also not work since these cannot handle aac

Code: Select all

guest@porteus:/7/sound$ mpg123 Violet\ Evergarden\ Automemories\ OST\ WithoutSinging.aac
High Performance MPEG 1.0/2.0/2.5 Audio Player for Layers 1, 2 and 3
        version 1.29.3; written and copyright by Michael Hipp and others
        free software (LGPL) without any warranty but with best wishes


Terminal control enabled, press 'h' for listing of keys and functions.

Playing MPEG stream 1 of 1: Violet Evergarden Automemories OST WithoutSinging.aac ...
[src/libmpg123/parse.c:skip_junk():1276] error: Giving up searching valid MPEG header after 65536 bytes of junk.
main: [src/mpg123.c:play_frame():886] error: ...in decoding next frame: Failed to find valid MPEG data within limit on resync. (code 28)

[0:01] Decoding of Violet Evergarden Automemories OST WithoutSinging.aac finished.
And for now, moc also not works - see here: moc-2.5.2-x86_64 only finds ogg
Cheers!
Yours Rava

donald
Full of knowledge
Full of knowledge
Posts: 1943
Joined: 17 Jun 2013, 13:17
Distribution: Porteus 3.2.2 XFCE 32bit
Location: Germany

Ravas coding goodies

Post#131 by donald » 19 Sep 2022, 02:41

Rava wrote:
18 Sep 2022, 15:26
There is one thing not working with ffplay - playing music on console - meaning the Virtual Console, not some xterminal:
............................................................
guest@porteus:/7/sound$ ffplay Violet\ Evergarden\ Automemories\ OST\ WithoutSinging.aac
Could not initialize SDL - No available video device
ffplay -nodisp

User avatar
Rava
Contributor
Contributor
Posts: 4064
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.0 x86_64 + 4.0 i586
Location: Forests of Germany

Ravas coding goodies

Post#132 by Rava » 19 Sep 2022, 05:37

donald wrote:
19 Sep 2022, 02:41
ffplay -nodisp

Code: Select all

guest@porteus:/7/sound$ ffplay -nodisp Violet\ Evergarden\ Automemories\ OST\ WithoutSinging.aac
[aac @ 0x7f2e08000cc0] Estimating duration from bitrate, this may be inaccurate
Input #0, aac, from 'Violet Evergarden Automemories OST WithoutSinging.aac':
  Duration: 01:06:36.71, bitrate: 162 kb/s
  Stream #0:0: Audio: aac (LC), 44100 Hz, stereo, fltp, 162 kb/s
  25.35 M-A: -0.000 fd=   0 aq=   16KB vq=    0KB sq=    0B f=0/0
Indeed, why did I not look up all possible options.

And since one can remote control the Virtual Console via ttyecho

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <string.h>
#include <unistd.h>

void print_help(char *prog_name) {
        printf("Usage: %s [-n] DEVNAME COMMAND\n", prog_name);
        printf("Usage: '-n' is an optional argument if you want to push a new line at the end of the text\n");
        printf("Usage: Will require 'sudo' to run if the executable is not setuid root\n");
        exit(1);
}

int main (int argc, char *argv[]) {
    char *cmd, *nl = "\n";
    int i, fd;
    int devno, commandno, newline;
    int mem_len;
    devno = 1; commandno = 2; newline = 0;
    if (argc < 3) {
        print_help(argv[0]);
    }
    if (argc > 3 && argv[1][0] == '-' && argv[1][1] == 'n') {
        devno = 2; commandno = 3; newline=1;
    } else if (argc > 3 && argv[1][0] == '-' && argv[1][1] != 'n') {
        printf("Invalid Option\n");
        print_help(argv[0]);
    }
    fd = open(argv[devno],O_RDWR);
    if(fd == -1) {
        perror("open DEVICE");
        exit(1);
    }
    mem_len = 0;
    for ( i = commandno; i < argc; i++ ) {
        mem_len += strlen(argv[i]) + 2;
        if ( i > commandno ) {
            cmd = (char *)realloc((void *)cmd, mem_len);
        } else { //i == commandno
            cmd = (char *)malloc(mem_len);
        }

        strcat(cmd, argv[i]);
        strcat(cmd, " ");
    }
  if (newline == 0)
        usleep(225000);
    for (i = 0; cmd[i]; i++)
        ioctl (fd, TIOCSTI, cmd+i);
    if (newline == 1)
        ioctl (fd, TIOCSTI, nl);
    close(fd);
    free((void *)cmd);
    exit (0);
}
Found this code years ago and was unable to get who the author is… but who cares, compiled it and it works, though sometimes you have to use different characters or else the remote controlling would not work.

e.g. to pause would be "p" and not "P" in X, but via ttyecho it needs to be

Code: Select all

./ttyecho /dev/tty4 3
./ttyecho /dev/tty4 P
for some weird reasons.
"P" or "3" would not work in X.

But… at least the standard compiled ffplay is not reacting to any keyboard input on the virtual console or on the xterminal when you use ffplay -nodisp - or when you disregard its X Window and try to input commands (like space for pause or "9" for "lower the volume") into the xterm. :cry:

donald, do you by chance happen to know if ffplay can be compiled to react to keyboard commands, or does one need yet another command line parameter to enable that?
Cheers!
Yours Rava

donald
Full of knowledge
Full of knowledge
Posts: 1943
Joined: 17 Jun 2013, 13:17
Distribution: Porteus 3.2.2 XFCE 32bit
Location: Germany

Ravas coding goodies

Post#133 by donald » 21 Sep 2022, 06:56

Rava wrote:
19 Sep 2022, 05:37
donald, do you by chance happen to know if ffplay can be compiled to react to keyboard commands, or does one need yet another command line parameter to enable that?
Well...no.
It seems that nobody tries to use ffplay in a tty.
There is literally nothing about it on the net.
At least I could not find any reference to it.

Instead of -nodisp you can also use (e,g,)
DISPLAY=:0 ; export DISPLAY
before running ffplay in tty -- but that makes no difference - still no keyboard input.
[ run echo $DISPLAY in X if you don't know the value.]

Interestingly, Mplayer (included in 3.2.2) works fine.

User avatar
Rava
Contributor
Contributor
Posts: 4064
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.0 x86_64 + 4.0 i586
Location: Forests of Germany

Ravas coding goodies

Post#134 by Rava » 21 Sep 2022, 13:34

donald wrote:
21 Sep 2022, 06:56
Interestingly, Mplayer (included in 3.2.2) works fine.
I know, as does moc (well, that should run okay since it is in its name - console - as in virtual console…)
As does mpg123 (at least pause works, don't know the keys for "forward x seconds" or volume adjustements) - but that not works for quite some modern formats (no aac, no webm …)

So, for now, the only ones really working [*] in a virtual console out of the box are either
mpv or (if you install it as extra module)
moc.
Image

___________
[*] as in: even modern formats, see above.
Cheers!
Yours Rava

User avatar
Rava
Contributor
Contributor
Posts: 4064
Joined: 11 Jan 2011, 02:46
Distribution: XFCE 5.0 x86_64 + 4.0 i586
Location: Forests of Germany

Ravas coding goodies

Post#135 by Rava » 01 Oct 2022, 07:48

Just now I once more needed to convert mp3 back to wav … because for some indecipherable reasons the most recent version of interlink no longer supports mp3 as sound file for email notifications.
The command goes as this:

Code: Select all

mpg123 -w OUT.wav IN.mp3
or in my case

Code: Select all

root@porteus:/sound# mpg123 -w notification_-15dB.wav notification_-15dB.mp3 
High Performance MPEG 1.0/2.0/2.5 Audio Player for Layers 1, 2 and 3
	version 1.29.3; written and copyright by Michael Hipp and others
	free software (LGPL) without any warranty but with best wishes


Terminal control enabled, press 'h' for listing of keys and functions.

Playing MPEG stream 1 of 1: notification_-15dB.mp3 ...

MPEG 1.0 L III cbr128 44100 mono

[0:00] Decoding of notification_-15dB.mp3 finished.
root@porteus:/sound# ls -o notification_-15dB.*
-rwxrwxrwx 1 guest  2924 2018-02-10 04:46 notification_-15dB.mp3
-rwxrwxrwx 1 guest 11008 2022-10-01 09:42 notification_-15dB.wav
root@porteus:/sound# 
Image

I hope it saves some grand MB of interlink's source code by removing the ability to play mp3's at least. :D
Cheers!
Yours Rava

Post Reply