Suppressing stubborn console output on system calls in Ruby - ruby

I'm calling imagemagik's convert program from within ruby to convert image types. I'm redirecting both stdout and stderr to /dev/null but I'm still getting console text. It only occurs when converting FROM webp so I suspect it's output from the Ubuntu webp package.
buffer = `convert -quiet "#{temp_dir}#{tmp_image_filename}" "#{temp_dir}#{new_image_filename}" > /dev/null 2>&1`
Output:
Decoded /tmp/magick-3658rrhNn7wh4IW2. Dimensions: 580 x 300 . Format: lossy. Now saving...
Saved file /tmp/magick-3658nGuNL-bzCkRA
Is this tty output? I can't figure out how to capture and suppress it. I added the quiet attribute to convert command line but it had no affect (another reason I suspect webp is the culprit). I've tried several other tips in my stackoverflow search with system, IO, wrapping in : $(...), etc to no avail. Any help?
Thanks!
Eric

Question was answered in the comments. I moved to Open3.capture3 using a format like below and the console text from webp is captured.
stdout, stderr, status = Open3.capture3("convert -flatten \"#{$temp_dir}#{tmp_image_filename}\" \"#{$temp_dir}#{#image_filename}\"")

Related

How to save ninja build output without loosing the compressed format of output

I tride to duplicate the output of the ninja build system to the separate file but I want to save original campresed look of the ninja output.
If I tee ninja (ninja all | tee -a someFile) I get wall of text enter image description here
Instead of updating one line.
If there is a better way to duplicate the output of ninja to file without loosing the compress formatig of output please let me know!
UPD: I find out that ninja update lines with [K escape sequence (erasing the line) and after capturing or rederecting ninga output it vanishing. If some body know how to allowed system to capture all tipy of escape sequence, it will solve my problem

Use ffmpeg to edit metadata titles for multiple files

I'd like to be able to add/edit video metadata titles to multiple files at once or with a single command, but I don't know how to tell ffmpeg to do this.
I read a similar post on the Ubuntu Forums, but I have never used string manipulation in Linux before, so the commands I'm seeing in the post are way out of my comprehension at the moment, and much of the discussion goes over my head.
I've got all of my video files in a filename format that includes the show name, the episode number, and episode title. For example:
show_name - episode_number - episode_title.extension
Bleach - 001 - A Shinigami Is Born!.avi
Is there a simple way to read the title and episode number from the filename and put it into a metadata tag without having to go through each and every file manually?
EDIT 1: So I found out that I can iterate through files in a directory, and echo the filename, and I was told by a friend to try bash to parse the strings and return values from that to use in the ffmpeg command line. The problem is, I have absolutely no idea how to do this. The string manipulation in bash is very confusing on first look, and I can't seem to get it to output what I want into my variables. My test bash:
for file in "Bleach - 206 - The Past Chapter Begins! The Truth from 110 Years Ago.mkv"; do extension=${file##*.} showName=${file%% *} episode=${file:9:3}; echo Extension: $extension Show: $showName Episode: $episode; done
That outputs
Extension: mkv Show: Bleach Episode: 206
Which are all the variables I'm going to need, I just don't know how to move those to be run in ffmpeg now.
EDIT 2: I believe I was able, through much trial and error, to find a bash command that would do exactly what I wanted.
for file in *; do newname=${file:0:-4}_2 ext=${file##*.} filename=${file} showname=${file%% *} episode=${file:9:3} nameext=${file##*- } title=${nameext%.*}; ffmpeg -i "$filename" -metadata title="$title" -metadata track=$episode -metadata album=$showname -c copy "$newname.$ext"; mv -f "$newname.$ext" "$filename"; done
This lets me parse the information from the filename, copy it to some variables, and then run ffmpeg using those variables. It outputs to a second file, then moves that file to the original location, overwriting the original. One could remove that section out if you're not sure about how it's going to parse your files, but I'm glad I was able to get a solution that works for me.

.cat.fastq to .cat.fasta file conversion problems

I'm trying to convert fastq to fasta without doing a quality filter first. When I try to use fastx toolkit to run this conversion, it gives me an error message when it runs into a low quality base and terminates the conversion so that my converted output ends very early. (error says something like quality score below -30).
I then tried to use a sed solution posted earlier on this forum about how to convert to fasta using sed. The line was this:
sed -n '1~4s/^#/>/p;2~4p'
the line I input to the terminal was:
sed -n '1~4s/^#/>/p;2~4p' Sample_As_L001_R1.cat.fastq
It spit out what I wanted, but printed directly into the terminal.
How do I get this info to not print on the terminal, but to print to an output file?
How do I specify the file/file name that I want the output to go into. Thanks.
redirect it to a file
sed -n '1~4s/^#/>/p;2~4p' Sample_As_L001_R1.cat.fastq > Sample_As_L001_R1.cat.fasta

Is it possible to clear the screen within ghostscript?

I could not find the command to clear the screen within ghostscript under windows. Could you please help me?
Thanks.
So you want to clear the text window? How about:
28 { ()= } repeat % output 28 blank lines
[There are several short recipes to output a newline: ()=, ()==, <>=, <>==, / =, (\n)print]
In Linux, you can discover the appropriate terminal control string with infocmp -L|grep clear_screen.
Then you can emit a hex string. (Sadly, the PLRM does not provide (\E) to generate an escape.) For Konsole, it's:
<1b5b481b5b324a> print flush
or
(\033[H\033[2J) print flush
On windows, ghostscript implements its own terminal window; and while it probably has such a code, there is no infocmp to discover what it might be.
erasepage, or showpageif you want to start a new page.

Can't run ffmpeg as subprocess in Ruby

I am trying following code to determine video resolution by running ffmpeg utility as subprocess and gain its output and parse it:
IO.popen 'ffmpeg -i ' + path_to_file do |ffmpegIO|
# my parse goes here
end
...but ffmpeg output is still connected to stdout and ffmepgIO.readlines is empty. Are there some special treatment needed for ffmpeg utility? Or are there other way to gain ffmpeg output?
I tested this code under WinXP and Fedora Linux - results are same.
To follow-up on mouviciel's comment, you would need to use something like popen3:
require 'open3'
Open3.popen3("ffmpeg", "-i", path_to_file) { |stdin, stdout, stderr|
# do stuff
}
(btw, note that using an array as parameter to popen would be a bit safer, especially if the filename may include spaces or any kind of character that would need to be quoted otherwise)
FFmpeg stdout is for media output. For logging information (such as resolution) you have to parse ffmpeg stderr.
Depending on what you are trying to do it might be easier to use the rvideo gem.
For example:
video = RVideo::Inspector.new(:file => path_to_file)
video.resolution # => "480x272"
video.width # => 480
video.height # => 272

Resources