Extracting images from video in JRuby-1.7.13 - ruby-on-rails

I want to extract images from a m4v video sent from mobile to my rails server. These images will be later used for face recognization purposes. There is a gem called "streamio-ffmpeg" that does this job nicely and easily but the problem is that it does not support JRuby-1.7.13 that I am currently using on my server. It's a big application and upgrading the JRuby version not desirable at this moment.
Can someone please suggest JRuby1.7.13 compatible alternative solutions/gems to extract the images from a video file?

From the sourcecode, it looks like streamio-ffmpeg outputs the underlying command by default :
FFMPEG.logger.info("Running transcoding...\n#{command}\n")
So all you have to do is execute :
movie.screenshot("screenshot_%d.jpg", { vframes: 50, frame_rate: '6/2' }, validate: false)
on a system where streamio-ffmpeg is installed.
You look at the output, extract the command, and use it somewhere else with :
system("ffmpeg arguments_you_extracted_from_the_logs")
without having to install streamio-ffmpeg.

Related

"No such file or directory" error on cropped ImageMagick image

I have a function in a DelayedJobs-run background process on my Rails app that receives Base64 image blobs, resizes and crops them, and then saves them:
image64 = Base64.decode64(screenshot)
image = MiniMagick::Image.read(image64)
image.resize "#{image.width * 0.5}x#{image.height * 0.5}"
image.crop("#{image.width}x250") if ("#{image.height}".to_i > 250)
Base64.encode64(image.to_blob)
The addition of the .crop call, however, stops this process working my production CentOS Linux server, resulting in this error from the .to_blob call:
Errno::ENOENT: No such file or directory # rb_sysopen - /tmp/mini_magick20170228-1709-t2rcyo
/path/app/vendor/bundle/ruby/2.4.0/gems/mini_magick-4.6.1/lib/mini_magick/image.rb:175:in `binread'
/path/app/vendor/bundle/ruby/2.4.0/gems/mini_magick-4.6.1/lib/mini_magick/image.rb:175:in `to_blob'
Without cropping, the function works. Running this in the foreground (i.e. in rails console) does not cause an issue.
Updating ImageMagick, ensuring /usr/bin is in PATH and sim-linking identify did not work (per this).
Any help is appreciated.
Per #mmichael, this question / answer shifted the problem. Specifically, changing
image.crop("#{image.width}x250")
to
image.crop("#{image.width}x250+0+0")
allows the .to_blob to happen without issue.
If anyone can explain the issue as to why this happens when run in the background only, I'll gladly accept your answer instead.

open office crashes after some time giving garbled font in converted PDF

We are converting word to pdf using the openoffice(3.4.1 version) in java with JODConverter.
below is the code used.
OpenOfficeConnection connection =
new SocketOpenOfficeConnection(2100);
try
{
connection.connect();
DocumentConverter converter =
new OpenOfficeDocumentConverter(connection);
converter.convert(inputFile, outputFile);
connection.disconnect();
return "Sucess " + DestinationPath + DestinationFileName;
}
catch (Exception localException1) {
}
The problem is that after random no of days the converted PDF contains the garbled fonts.
like # # ! $ $ " % &
The only solution we have so far is to restart the server. System guys are saying the the problem is with Open Office.
We are using open office to convert the document since it converts the doc files exactly including all the formatting and table structure.
what could be the solution to this.
So OpenOffice can be a little temperamental when running on a server, especially as it isn't multi-threaded and you end up having to run a pool of OpenOffice processes - see How can I use OpenOffice in server mode as a multithreaded service?.
Added to that often the rendering is off when converting to PDF - see https://forum.openoffice.org/en/forum/viewtopic.php?f=7&t=68865 which is why you may want to consider using a conversion service to automate the conversion tasks for you ?
For complete transparency I work for Zamzar (an online file conversion service), we have recently released a developer API - https://developers.zamzar.com/ that allows you to convert between a multitude of file types, specifically applicable to you here in that we support both doc and docx to pdf with little or not loss in the way the PDF is rendered. It maybe worth a look to see if this is a better alternative to trying to run your own solution through OpenOffice on a server.

Converting PDF to PNG with transparent background

We have a Ruby on Rails application that needs to convert a PDF into a PNG with a transparent background. We're using rmagick 2.13.1. On our development machines the following code works exactly how we want it.
pages = Magick::Image.from_blob(book.to_pdf.render){ self.density = 300 }
page = pages[0]
image_file = Tempfile.new(['preview_image', '.png'])
image_file.binmode
image_file.write( page.to_blob { |opt| opt.format = "PNG" } )
We thens save the image_file and all is peachy. When we deployed to a review server on Heroku, though, the generated image has a white background. It turns out that Heroku's cedar stack is using imagemagick ImageMagick 6.5.7-8 2010-12-02 where we're using ImageMagick 6.7.5-7 2012-05-08 on our development machines.
I've scoured the net for older posts that might apply to the older version to try and figure out how to generate the transparent PNGs. It's surely supported, but, so far I haven't been able to figure out the right combination of settings.
To verify that it wasn't the PDF generation that was the problem, I downloaded a PDF generated on Heroku and successfully converted it using the above code (slightly modified to read the file in instead of generate it) to a transparent PNG.
Some of the things I've tried in various combinations are:
page.matte = true
page.format = "PNG32"
page.background_color = "none"
page.transparent_color = "white"
page.transparent("white")
So, the question is "is this possible?". If so, which settings do I need to set on the image before writing it out?
I'm also investigating including a compiled binary of a more up to date Imagemagick on Heroku.
Any help is appreciated.
This should no longer be an issue, as Heroku has ImageMagick versions 6.7-6.9 on their various stacks.

Problems converting video to pictures using python 2.7

Tried a lot of options and am running out of ideas. I was hoping someone here could help. I am trying to write some code in python that will extract frames (say every tenth frame) from a video (.avi or .wmv) and create a picture (.jpg preferably - but other formats will do). I have had no success and was wondering if someone could assist me in solving my problem by providing an alternative to what I have tried and failed.
I have tried PyMedia (the example in their tutorial http://pymedia.org/tut/src/dump_video.py.html does not work - the program bombs out when it looks for the video codecs):
#dm= muxer.Demuxer( inFile.split( '.' )[ -1 ] ) This line does not work
dm= muxer.Demuxer( 'avi' ) #This modified line does seem to work however
i= 1
inFile = "VideoTest.avi"
f= open( inFile, 'rb' )
s= f.read( 400000 )
r= dm.parse( s )
v= filter( lambda x: x[ 'type' ]== muxer.CODEC_TYPE_VIDEO, dm.streams )
v_id= v[ 0 ][ 'index' ]
print 'Assume video stream at %d index: ' % v_id
c= vcodec.Decoder( dm.streams[ v_id ] ) #this is the point where it crashes.
I have tried OpenCV v2.2 for Python but that doesn't work either (I can get most of OpenCV to work - except the one function, CaptureFromFile, that I need does not work). I believe the reason this function doesn't work is because on Windows it requires highGui to operate and for some reason, python and opencv cannot find highgui eventhough it is in the correct directory. I also understand OpenCV has issues with finding and applying correct video codecs so I am not sure which is the cause of my problem.
I have looked at pyFFMPEG but the last build for that was version 2.6 and I am running python 2.7.
I am running this on Windows Vista and Windows 7 machines, have Python 2.7 and OpenCV 2.2 loaded on C:\ and all other python packages (pygames, pymedia, numpy, scipy) installed in C:\python27\Libs\site-packages..." I downloaded and installed from executables, all packages were built for python 2.7. My Path variable includes Python27 and OpenCV and I have a PYTHONPATH variable.
Thanks for any ideas or recommendations.
I tried the following to extract each frames as a separate image:
import cv2
file_path = "some\path\to\the\file.avi"
video_object = cv2.VideoCapture(path)
success = True
while success:
success,frame = video_object.read()
if success:
cv2.imwrite(path[:-4]+"_"+str(i)+".jpg",frame)
print("Done!")
This script saves each frame as an image in the same folder as the source file. It may not be the most efficient way to do this, but it works for me! I know my string parsing is not the most recommended, but it works.
video_object.read() returns two object. The first is a bool indicating whether the reading operation was a success or not, and the second is the image.
There may be limitations with respect to the video codec, of which I am not aware. I'm using Python 2.7 with the most recent version of openCV and Numpy as of 11/7/2013.

Where do the temp files go when using MiniMagick in a Ruby on Rails app?

I'm using MiniMagick to perform some image resizing on images uploaded through a multi-part form. I need to generate a few different types of images from the originally uploaded file. Here's the code that's performing the image processing:
// Generates a thumbnail image
mm = MiniMagick::Image.open(Rails.root.join('public', 'uploads', new_url))
mm.resize(thumbnail_dimensions.join("x"))
mm.write(Rails.root.join('public', 'uploads', "t_"+new_url))
// Generates cropped version
mm_copy = MiniMagick::Image.open(Rails.root.join('public', 'uploads', new_url))
mm_copy.crop('200x200')
mm_copy.write(Rails.root.join('public', 'uploads', "c_"+new_url))
new_url is the path to the image in the public folder. The thumbnail routine works perfectly. When the app goes to start processing the cropped version, that is where things start breaking and I can't for the life of me figure it out. I receive the following error when from this code:
No such file or directory - /tmp/mini_magick20110627-10055-2dimyl-0.jpg
I read some stuff about possible race conditions with the garbage collector in Rails but I wasn't able to resolve the issue. I tried this from the console as well and can create MiniMagick instances but receive the No such file error there as well. At this point, I have no idea where to go so I'm hoping someone here has some helpful suggestions. Thanks for your help!
Details:
OS: Ubuntu (Lucid Lynx)
Rails Version: 3.0.7
Ruby Version: 1.8.7
MiniMagick Version: 3.3
Did you installed ImageMagick?
If not,
try sudo apt-get install ImageMagick,
and then restart your webrick server
it's probably the race condition which is mentioned here:
https://ar-code.lighthouseapp.com/projects/35/tickets/6-race-condition-with-temp_file
here's one fix:
http://rubyforge.org/tracker/index.php?func=detail&aid=9417&group_id=1358&atid=5365
alternatively, and probably easier, you could try this:
// Generates a thumbnail image
mm = MiniMagick::Image.open(Rails.root.join('public', 'uploads', new_url))
mm_copy = mm.clone # clone the opened Image, instead of re-opening it
mm.resize(thumbnail_dimensions.join("x"))
mm.write(Rails.root.join('public', 'uploads', "t_"+new_url))
// Generates cropped version
mm_copy.crop('200x200')
mm_copy.write(Rails.root.join('public', 'uploads', "c_"+new_url))

Resources