I have got some PDF files i need to crop (crop to trimbox etc), which I can do with the following command
convert -define pdf:use-trimbox=true -density 300 original.pdf outcome.pdf
It does the job however the outcome.pdf quality if not as sharp as original PDF. When I crop them on my desktop software (Acrobat Pro) the result it same quality but in ImageMagick I can not keep the same quality in the outcome.
My question is how can i crop a pdf page without compromising from the quality?
i have been searching and trying different settings for weeks but not been succesfull.
Most likely the problem is that ImageMagick is having the PDF rendered to a bitmap by Ghostscript, and then exporting the bitmap wrapped up in a PDF file. Without seeing the original I can't say for sure, but if the original contained JPEG images, then most likely you are ending up with JPEG being applied twice, or simply rendering at all is causing the problem.
Your best bet is going to be to use a tool which can simply apply a CropBox to the page(s). You can do this with Ghostscript, for example (which may also modify the PDF in other ways, including the double JPEG quantisation, so beware).
gs -sDEVICE=pdfwrite \
-sOutputFile=cropped.pdf \
-dBATCH -dNOPAUSE \
-c "<</ColorImageFilter /FlateEncode>> setdistillerparams" \
-f <input.pdf> \
-c "[ /CropBox [ 0 0 100 100] /PAGES pdfmark" \
-f
The first section between -c and -f tells the pdfwrite device to use FlateEncode for colour images, the default is JPEG, using Flate will ensure you don't get quantisation applied twice.
The second section between -c and -f tells the pdfwrite device to write a CropBox to the file and to make it 0,0 to 100,100. The units are the usual units in PDF; 1/72 inch, you can use fractional values.
I'm sure there are other tools which will do this, possible even more easily.
Have you tryed to increase the density? That's the purpose:
http://www.imagemagick.org/script/command-line-options.php#density
Otherwise try:
-quality 100
From:
Convert PDF to image with high resolution
Related
I am using php imagemagic to convert svg to jpg and i use the below command for the conversion.
convert -density 250 source.svg target.jpg
I can able to convert svg to jpg successfully but some elements in svg are missing. Please check below example
Input SVG
Output JPG
Here you can clearly see that the light grey shades on the sides are replaced with white color. Can someone let me know how to fix this.
FYI here is the link to download the actual SVG
https://drive.google.com/file/d/1vC5yaXds7ogcsTWjaDzkXZZFyKzLCsgf/view?usp=sharing
Which SVG renderer are you using? It could be the internal Imagemagick MSVG/XML, the RSVG delegate or Inkscape (in order of increasing functionality). You can find out by adding -verbose to your command line.
convert -verbose -density 250 source.svg target.jpg
I used Imagemagick 6.9.10.11 Q16 Mac OSX with its internal MSVG/XML renderer, RSVG 2.42.2_2 and Inkscape 0.92.3_4. All three produced different results. The RSVG was the worst. The Inkscape was the best. Here are my results using your command.
convert -density 250 MSVG:source.svg target_msvg.jpg
convert -density 250 RSVG:source.svg target_rsvg.jpg
convert -density 250 source.svg target_inkscape.jpg
Usually RSVG does better than Imagemagick's MSVG. But here it seems to do worse. It could be due to the way I modified my delegates.xml file in order to be able to run RSVG while Inkscape was installed. Also the MSVG renderer has been improved over the last few releases. So an older version may not produce as good of a result.
Finally I found a workaround for this. I converted svg to canvas image (base64 format) via javascript and through php i have converted the base64 data to jpg image.
When I try to upload videos captured from my iPhone in my app, the server performs a conversion from .mov to .mp4 so that it can be played in other platforms. However the problem is that when I shoot the video (in portrait orientation) and it is converted (using ffmpeg) and then played back from the server, it appears to be rotated. Any idea?
FFMPEG changed the default behavior to auto rotate video sources with rotation metadata in 2015. This was released as v2.7.
If your ffmpeg version is v2.7 or newer, but your rotation metadata isn't respected, the problem is likely that you are using custom rotation based on metadata. This will cause the same logic to be applied twice, changing or cancelling out the rotation.
In addition to removing your custom rotation (recommended), there's an option to turn off auto rotation with -noautorotate.
ffmpeg -noautorotate -i input.mp4...
This will also work in some older releases.
For sake of completeness, the reason this is happening is that iPhones only actually capture video in one fixed orientation. The measured orientation is then recorded in Apple-specific metadata.
The effect is that Quicktime Player reads the metadata and rotates the video to the correct orientation during playback, but other software (e.g., VLC) does not and shows it as oriented in the actual codec data.
This is why rotate=90 (or vflip, or transpose, or etc.) will work for some people, but not others. Depending on how the camera is held during recording, the rotation necessary could be 90, 180, or even 270 degrees. Without reading the metadata, you're just guessing at how much rotation is necessary and the change that fixes one video will fail for another.
What you can also do is remove the QuickTime specific metadata when rotate the .mov.
This will make sure that the video is rotated the same way in VLC and QuickTime
ffmpeg -i in.mov -vf "transpose=1" -metadata:s:v:0 rotate=0 out.mov
Here's the documentation on the -metadata option (from http://ffmpeg.org/ffmpeg.html):
-metadata[:metadata_specifier] key=value (output,per-metadata)
Set a metadata key/value pair.
An optional metadata_specifier may be given to set metadata on streams or chapters. See -map_metadata documentation for details.
This option overrides metadata set with -map_metadata. It is also possible to delete metadata by using an empty value.
For example, for setting the title in the output file:
ffmpeg -i in.avi -metadata title="my title" out.flv
To set the language of the first audio stream:
ffmpeg -i INPUT -metadata:s:a:1 language=eng OUTPUT
Depending on which version of ffmpeg you have and how it's compiled, one of the following should work...
ffmpeg -vf "transpose=1" -i input.mov output.mp4
...or...
ffmpeg -vfilters "rotate=90" -i input.mov output.mp4
Use the vflip filter
ffmpeg -i input.mov -vf "vflip" output.mp4
Rotate did not work for me and transpose=1 was rotating 90 degrees
So - I too ran into this issue, and here my $0.02 on it:
1.) some videos DO have Orientation/Rotation metadata, some don't:
MTS (sony AVHCD) or the AVIs I have - DO NOT have an orientation tag.
MOVs and MP4s (ipad/iphone or samsung galaxy note2) DO HAVE it.
you can check the setting via 'exiftool -Rotation file'.
My videos often have 90 or 180 as the rotation.
2.) ffmpeg - regardless of the man-page with the metadata-tag, just doesn't EVER seem to set it in the output file. - the rotation-tag is ALWAYS '0'.
it correctly reports it in the output - but it's never set right to be reported by exiftool. - But hey - at least it's there and always 0.
3.) rotation angles:
if you want rotate +/- 90: transpose=1 for clockwise 90, 2 ccw
now if you need 180 degree - just add this filter TWICE.
remember - it's a filter-chain you specify. :-) - see further down.
4.) rotate then scale:
this is tricky - because you quickly get into MP4 output format violations.
Let's say you have a 1920x1080 MOV.
rotate by 90 gives 1080x1920
then we rescale to -1:720 -> 1080*(720/1920) = 405 horiz
And 405 horizontal is NOT divisable by 2 - ERROR. fix this manually.
FIXING THIS automatically - requires a bit of shell-script work.
5.) scale then rotate:
you could do it this way - but then you end up with 720x1280. yuck.
But the filter-example here would be:
"-vf yadif=1,scale=-1:720,transpose=1"
It's just not what I want - but could work quite OK.
Putting it all together: - NOTE - 'intentionally WRONG Rotation-tag', just to demonstrate - it won't show up AT ALL in the output !
This will take the input - and rotate it by 180 degree, THEN RESCALE IT - resetting the rotation-tag. - typically iphone/ipad2 can create 180deg rotated material.
you just can leave '-metadata Rotation=x' out the line...
/usr/bin/ffmpeg -i input-movie.mov -timestamp 2012-06-23 08:58:10 -map_metadata 0:0 -metadata Rotation=270 -sws_flags lanczos -vcodec libx264 -x264opts me=umh -b 2600k -vf yadif=1,transpose=1,transpose=1,scale=1280:720 -f mp4 -y output-movie.MP4
I have multiple devices - like a settop box, ipad2, note2, and I convert ALL my input-material (regardless whether it's mp4,mov,MTS,AVI) to 720p mp4, and till now ALL the resulting videos play correct (orientation,sound) on every dev.
Hope it helps.
For including into web pages my portrait-format videos from iPhone, I just discovered the following recipe for getting .mp4 files in portrait display.
Step 1: In QuickTime Player, Export your file to 480p (I assume that 720p or 1080p would work as well). You get a .mov file again.
Step 2: Take the new file in QT Player, and export to “iPad, iPhone…”. You get a .m4v file.
Step 3: I’m using Miro Video Converter, but probably any readily-available converter at all will work, to get your .mp4 file.
Works like a (long-winded) charm.
I've filmed the video with Ipad3 and it was oriented upside down, which I suppose is the common situation of all Apple devices at some versions. Besides of it, the 3-minutes long MOV file (1920x1090) took about 500 Mb in size, which made it not available to share easily. I had to convert it to MP4, and analyzing all threads I've found on stackoverflow, here's the final code string for ffmpeg I've used (ffmpeg ver. 2.8.4):
ffmpeg -i IN.MOV -s 960x540 -metadata:s:v rotate="0" -acodec libmp3lame OUT.mp4
I suppose you may just leave '-metadata:s:v rotate="0"' if you don't need the resize and audio codec change. Note that if you resize the video, width and height should fully divide to 4.
Although the topic is old.
Hope this will help some one:
Get ffmpeg latest version : https://www.ffmpeg.org/download.html
The command that worked for me (to flip 180 degrees):
ffmpeg -noautorotate -i input.mp4 -filter:v "rotate=PI" output.mp4
When the degrees are determined by -filter:v "PI/180*degrees"
for example
-filter:v "45*PI/180" for 45 degrees
A nice explanation is here
https://superuser.com/questions/578321/how-to-rotate-a-video-180-with-ffmpeg
Or... to simply change the tag in an existing file:
Read the current rotation
exiftool -Rotation <file>
then, for example:
exiftool -Rotation=180 <file>
to set it to 180
So, i have kind of accepted this task on work but im really not sure if its possible.
We are going to build a website where users can upload videos from their computers and mobile phone browsers. The video files can be a large range of aspect ratios, width, height, codex and file formats.
I will have access to ffmpeg from php exec command on a web server.
Is it possible to use this to convert the user files to one file format that works on computers, android and iphone.
The requirements is that we can set a max width, to witch the video will be scaled, dynamically to match height.
Does anyone know is this can be done, and be done in a reasonable amount of time. Will do project on 2 days. And if so some pointers in the right direction would be nice.
Had the same problem but solved by using HandBrake the open source video transcoder
https://handbrake.fr/
If your target can only be one file format, then I would choose mp4 baseline. (However some browsers won't play it, which is why the html tag offers multiple source flags, which usually include webm and ogg video...)
Using ffprobe -show_streams $uploadedFile you can get the dimensions (and aspect ratio) of the file. Using math you can get the new dimensions based on your needs.
$newDim=$new_width.":".$new_height;
$output = shell_exec("/usr/bin/ffmpeg -i $uploadedFile -f mp4 \
-c:a libfdk_aac -b:a 128k -c:v libx264 -vprofile baseline \
-movflags faststart -vf scale=$newDim $output");```
Here is the breakdown:
f mp4 > format mp4
c:a libfdk_aac > audio codec
c:v libx264 > video codec
vprofile baseline > minimal codec usage for mobile
movflags faststart > put the moov atom at the beginning of file
$output > should have '.mp4' as a file ending
Of course the devil is in the details (and the number of processing cores you can throw at an online converter), but this will get you up and running at least.
Edit: Actually answered the question. :)
By the way, ffmpeg does offer the vf flag: -vf scale=320,-1, but sometimes it gives you a dimension not divisible by 2 which throws an error in x264 encoding. Its better to do the math yourself.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
This post was edited and submitted for review 8 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I want to make video from pdf file.Is it possible?
I created video from images with ffmpeg but not
able to create video from PDF files.
here is script to convert pdf to video:
exec("convert -geometry 1600x1600 -density 200x200 -quality 100 -resize 800x $pdf_path $temp_images");
exec("ffmpeg -loop 1 -f image2 -framerate 0.5 -b 1800 -i $temp_images_wildcard -c:v libx264 -preset slow -tune stillimage -r 5 -t 22 -y $frame_target 2>&1",$output);
Assuming your PDF files are in your working directory, to create a movie file from a set of PDF files you can execute:
exec("mogrify -verbose -density 500 -resize 800 -format png ./*.pdf")
exec("convert -delay 600 *.png movie.mp4")
This requires Imagemagick and Ghostscript to be installed. Works on Linux/Mac OS X/Microsoft Windows. Not vectorial.
If you want to generate some PDF files to test the commands, here is a python script that generates PDF files:
import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
for i in range(10):
np.random.seed(i)
dates = pd.date_range('1/1/2000', periods=50)
print('dates: {0}'.format(dates))
df = pd.DataFrame(np.random.randn(len(dates), 1), index=dates.astype(str), columns=['A'])
print('df: {0}'.format(df))
plt.figure(figsize=(60,15))
df.plot(y='A', use_index=True)
plt.xticks(rotation=70)
plt.savefig('plot_{0}.pdf'.format(i), dpi=300, bbox_inches='tight')
FYI:
Imagemagick Convert PDF to JPEG: FailedToExecuteCommand `"gswin32c.exe" / PDFDelegateFailed --> that's the error message you get if Ghostscript is not installed.
Convert every pdf in the current directory to png --> convert doesn't support batch conversion, unlike mogrify.
ffmpeg requires the images to have some very specific naming convention, unlike convert which simply takes the lexicographical order.
Using Imagemagick to convert images to the video: background of the video is black, whereas the background of the pictures are white
How can I run mogrify over the 3 million JPG files? -> mogrify can only process one file at the time, but one can go around with xargs.
You could convert the PDF to images and then use your old method to produce the video
Standard FPS (frames per second) in a video is around 30, that's why keeping it in usual document formats with regular paging is impractical. There's a way to convert a pdf file into images, then you can use it in your video.
A while back I used a PNG optimisation service called (I think) "smush it". You fed it a weblink and it returned a zip of all the PNG images with their filesizes nicely, well, smushed...
I want to implement a similar optimisation feature as part of my website's image upload process; does anyone know of a pre-existing library (PHP or Python preferably) that I can tap into for this? A brief Google has pointed me towards several command line style tools, but I'd rather not go down that route if possible.
Execute with PHP this command line tools
pngcrush -rem gAMA -rem cHRM -rem iCCP -rem sRGB -brute -l 9 -max -reduce -m 0 -q IMAGE
optipng -o7 -q pngout.png
pngout pngout.png -q -y -k0 -s0
advpng -z -4 pngout.png > /dev/null
pngcrush
OptiPNG
pngout
advpng
As long as your PHP is compiled with GD2 support (quite common nowadays):
<?php
$image = imagecreatefromstring(file_get_contents('/path/to/image.original.png'));
imagepng($image, '/path/to/image.smushed.png', 9);
This will read in any image format GD2 understands (not just PNG) and output a PNG gzipped as the maximum compression level without sacrificing quality.
It might be of less use today than years ago though; most image editors already do this, since gzipping doesn't cost as much CPU-wise as it used to.
Have you heard of PNGCrush? You could check out the source, part of PNG and MNG Tools at SourceForge, and transcribe or wrap it in Python.
I would question the wisdom of throwing away other chunks (like gAMA and iCCP), but if that's what you want to do it's fairly easy to use PyPNG to remove chunks:
#!/usr/bin/env python
import png
import sys
input=sys.stdin
out=sys.stdout
def critical_chunks(chunks):
for type,data in chunks:
if type[0].isupper():
yield type,data
chunks = png.Reader(file=input).chunks()
png.write_chunks(out, critical_chunks(chunks))
the critical_chunks function is essentially filtering out all but the critical PNG chunks (the 4 letter type for a critical chunk starts with an uppercase letter).