Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: Rotating avi file using ffmpeg and imagemagick

  1. #1
    Join Date
    Jan 2014
    Beans
    16

    Rotating avi file using ffmpeg and imagemagick

    I am following the example for rotating movies 90 degrees using ffmpeg and imagemagick as suggested in another thread. The question I am asking how can I use ffmpeg to reassemble a movie clip from the images that is compatible with the original movies from which they were derived.

    i am making a movie out of many avi clips. The movies were all shot with the same camera. One of the clips was rotated at a 90 degree angle. I need to rotate it. I've tried many solutions like avidemux, etc. to rotate the films but the video frames become very uneven and the sound is far out of sync. I am trying to export the avi movie to images using ffmpeg, editing them using imagemagick, and then reassembling them as an avi in a mode that matches the orginal movie. After that i hope to add the sound back in like a sound track.

    The question I am asking how can I use ffmpeg to create a movie from the images that is compatible with the originals. The orginal movies have the following specifications:

    Code:
    ffmpeg -i MVI_0183.AVI
    
    Input #0, avi, from 'MVI_0183.AVI':
      Metadata:
        creation_time   : 2003-08-16 11:02:42
        encoder         : CanonMVI01
      Duration: 00:00:03.19, start: 0.000000, bitrate: 2361 kb/s
        Stream #0.0: Video: mjpeg, yuvj422p, 320x240, 15 tbr, 15 tbn, 15 tbc
        Stream #0.1: Audio: pcm_u8, 11024 Hz, 1 channels, u8, 88 kb/s
    In case you are interested, here are my steps to date.

    1. strip individual images out of the movie (images are now 320x240)
    Code:
    ffmpeg -i MVI_0183.AVI image_dump/%d.png
    2. turn them on their side (images are now 240 x 320)
    Code:
    mogrify -rotate 90 image_dump/*.png
    3. Then I made a directory temp inside directory image_dump so that I could preserve the above work and experiment a little. I wanted to crop the bottom off the bottom of the image to make it 240x240 and then add a black border so that the rotated image returned to a 320x240 frame size. When i was satisfied that the images were right, i used this command below:
    Code:
    mogrify -path temp -format png -gravity south -chop 0x80 -background black -extent 320x240 *.png
    4. My next step is to use ffmpeg to reassemble the video. Something like this???
    Code:
    ffmpeg -i temp/%d.png -o output.avi



    FYI. A more complete set of specifications is provided below:





    Code:
    mediainfo MVI_0183.AVI 
    General
    Complete name                            : MVI_0183.AVI
    Format                                   : AVI
    Format/Info                              : Audio Video Interleave
    File size                                : 922 KiB
    Duration                                 : 3s 200ms
    Overall bit rate                         : 2 361 Kbps
    Mastered date                            : Sat Aug 16 11:02:42 2003
    Writing application                      : CanonMVI01
    
    Video
    ID                                       : 0
    Format                                   : JPEG
    Codec ID                                 : MJPG
    Duration                                 : 3s 200ms
    Bit rate                                 : 2 265 Kbps
    Width                                    : 320 pixels
    Height                                   : 240 pixels
    Display aspect ratio                     : 4:3
    Frame rate                               : 15.000 fps
    Color space                              : YUV
    Chroma subsampling                       : 4:2:2
    Bit depth                                : 8 bits
    Scan type                                : Progressive
    Compression mode                         : Lossy
    Bits/(Pixel*Frame)                       : 1.966
    Stream size                              : 885 KiB (96%)
    
    Audio
    ID                                       : 1
    Format                                   : PCM
    Format settings, Sign                    : Unsigned
    Codec ID                                 : 1
    Duration                                 : 3s 200ms
    Bit rate mode                            : Constant
    Bit rate                                 : 88.2 Kbps
    Channel(s)                               : 1 channel
    Sampling rate                            : 11.024 KHz
    Bit depth                                : 8 bits
    Stream size                              : 34.4 KiB (4%)
    Interleave, duration                     : 800 ms (12.00 video frames)
    Interleave, preload duration             : 1000 ms
    Last edited by rioguia2; February 1st, 2014 at 05:03 AM. Reason: [CODE]

  2. #2
    Join Date
    Nov 2011
    Location
    /dev/root
    Beans
    Hidden!

    Re: Rotating avi file using ffmpeg and imagemagick

    It is possible to rotate the frames (picture) directly in ffmpeg

    See
    Code:
    man ffmpeg
    Under the section

    Code:
    VIDEO FILTERS
           When you configure your Libav build, you can disable any of the existing filters using
           --disable-filters.  The configure output will show the video filters included in your
           build.
    
           Below is a description of the currently available video filters.
    you find transpose

    Code:
       transpose
           Transpose rows with columns in the input video and optionally flip it.
    
           It accepts a parameter representing an integer, which can assume the values:
    
           0   Rotate by 90 degrees counterclockwise and vertically flip (default), that is:
    
                       L.R     L.l
                       . . ->  . .
                       l.r     R.r
    
           1   Rotate by 90 degrees clockwise, that is:
    
                       L.R     l.L
                       . . ->  . .
                       l.r     r.R
    
           2   Rotate by 90 degrees counterclockwise, that is:
    
                       L.R     R.r
                       . . ->  . .
                       l.r     L.l
    
           3   Rotate by 90 degrees clockwise and vertically flip, that is:
    
                       L.R     r.R
                       . . ->  . .
                       l.r     l.L
    Last edited by sudodus; January 30th, 2014 at 08:51 PM.

  3. #3
    Join Date
    Jan 2014
    Beans
    16

    Re: Rotating avi file using ffmpeg and imagemagick

    thanks for your response. I've used both ffmpeg and mencoder. The problem is that shifting the images 90 degrees throws off the synchronization with the sound. None of these solutions address that issue.

    I'd like to see if someone can help me with the question i posed. The question I am asking how can I use ffmpeg to create a movie from the images that is compatible with the originals?

    Thanks

  4. #4
    Join Date
    Sep 2006
    Beans
    3,491

    Re: Rotating avi file using ffmpeg and imagemagick

    You can skip imagemagick, and you can even perform the rotation losslessly:

    Get exiftran:
    Code:
    sudo apt-get install exiftran
    Get ffmpeg. Ubuntu doesn't use the real ffmpeg but you can easily download it:
    Code:
    wget http://ffmpeg.gusari.org/static/32bit/ffmpeg.static.32bit.$(date +"%F").tar.gz
    tar xzvf ffmpeg.static.32bit.$(date +"%F").tar.gz
    Output the frames. MJPEG is basically just a bunch of jpg images. This will use stream copy mode and extract the actual jpg images (in addition to using the mjpeg2jpeg bitstream filter) instead of re-encoding them. Notice the "./" before the "ffmpeg":
    Code:
    mkdir frames
    ./ffmpeg -i MVI_0183.AVI -codec:v copy -bsf:v mjpeg2jpeg frames/frame_%d.jpg
    Rotate the images. You can do this losslessly with exiftran:
    Code:
    exiftran -i -9 frames/frame*.jpg
    Mux the images into a video, and mux the audio from MVI_0183.AVI (again, no re-encoding is needed). Default input frame rate is 25, so you may have to change it to match your original input:
    Code:
    ./ffmpeg -framerate 15 -i frames/frame_%d.jpg -i MVI_0183.AVI -codec copy -map 0:v -map 1:a rotated.avi
    Delete the frames if you like:
    Code:
    rm -rf frames
    Last edited by FakeOutdoorsman; January 31st, 2014 at 07:59 PM.

  5. #5
    Join Date
    Jan 2014
    Beans
    16

    Re: Rotating avi file using ffmpeg and imagemagick

    Thanks FakeOutdoorsman that's an amazing response. I noticed that the thread I used to start my project included your work as well.


    I just added a step after rotating the .jpgs. The rotated.jpgs are oriented 240 wide X 320 high. Because I'm trying to splice this onto an standard 320 wide x 240 high avi, i chop the bottom off (temporarily making the image 240 x 240) and then add side borders to provide the standard 320 wide x 240 high avi (an inelegant solution). However, you copy codec made the frames match up perfectly. I'm testing now to see if I can convert the output into a standard .vob DVD output.


    Code:
    mogrify -format jpg -gravity south -chop 0x80 -background black -extent 320x240 *.jpg
    Last edited by rioguia2; February 1st, 2014 at 04:59 AM. Reason: [CODE]

  6. #6
    Join Date
    Sep 2006
    Beans
    3,491

    Re: Rotating avi file using ffmpeg and imagemagick

    You can also crop losslessly with jpegtran, but I'm not sure how to add the borders without re-encoding. Since re-encoding is required (because of the padding and because you want DVD video) you can just use ffmpeg to do everything:

    Code:
    ffmpeg -i MVI_0183.AVI -vf "transpose=1,crop=iw:240:0:0,pad=320:240:(ow-iw)/2:0" -target ntsc-dvd output.mpg


    I forgot to mention "-framerate" in my last post but I updated it. Don't forgot the code tag for your commands and console outputs.
    Last edited by FakeOutdoorsman; January 31st, 2014 at 08:27 PM.

  7. #7
    Join Date
    Jan 2014
    Beans
    16

    Re: Rotating avi file using ffmpeg and imagemagick

    I can't wait to try it. i love going directly to mpeg.

    I was working on my own solution but its in no way as elegant as yours.

    Here is what i did to assemble several clips into a single avi file. The problem was that some clips were shot in portrait (i.e. at a 90 degree angle). In needed to rotate them, making them 240 wide x 320 high). In order for them to match the other clips, i had resize the rotated clips to the standard format (320 wide x 240 high), and conconcatinate them into a single avi file. The basic problem I ran into was that despite all my best efforts, I was unable to match the rotated file to the original specifications for the files and, as a result, the sound was always wildly out of sequence. As a compromise, I ran all the files through the same basic process and got a single avi with the audio in sync with the video.

    Here are my notes on a successful attempt.

    for files that were shot in portrait (90 degree angle)
    . i used the -ovc lavc because i could not make mencoder simply copy and needed to pick some codec. There may be better options but this one worked for me. the -vf invokes the scale video filter which actually rotates and crops the clip)
    Code:
    mencoder MVI_0184.AVI -ovc lavc -oac copy -vf rotate=1,scale=320:-2,crop=320:240 -o mvi_0184.avi
    I used nearly the same processing for clips that were shot in standard format. Note the: "-vf scale=320:-2,crop=320:240" below. Unlike above, we shouldn't need to use this bit since these files are already the correct size. I suspect that this should be unnecssary (but it was the only way this amateur got this to work).
    Code:
    mencoder MVI_0462.AVI -ovc lavc -oac copy -vf scale=320:-2,crop=320:240 -o mvi_0462.avi
    after we have processed all the clips, I need to make a list so that I can conveniently concatenate the clips into a single file without all the typing. Note that the file is created in the directory above the working directory.
    Code:
    ls *.avi > ../list_avi.txt
    now I print the list of the processed clips to the screen in a single line that I can cut and paste into the terminal.
    Code:
    tr '\n' ' ' < ../list_avi.txt
    now concatinatinate the processed clips.
    Code:
    cat mvi_0007.avi mvi_0008.avi mvi_0014.avi mvi_0015.avi mvi_0016.avi mvi_0017.avi mvi_0147.avi mvi_0175.avi mvi_0182.avi mvi_0183.avi mvi_0184.avi mvi_0209.avi mvi_0210.avi mvi_0211.avi mvi_0212.avi mvi_0218.avi mvi_0219.avi mvi_0462.avi > movie_collection_05.avi
    copy the video and audio streams and generate an index for our final product.
    Code:
    mencoder -forceidx -oac copy -ovc copy movie_collection_05.avi -o movie_collection_indexed_05.avi
    Last edited by rioguia2; February 1st, 2014 at 05:04 AM. Reason: typos / code tag

  8. #8
    Join Date
    Jan 2014
    Beans
    16

    Re: Rotating avi file using ffmpeg and imagemagick

    Thanks for sharing your answers with me. It's great when people who obviously know what they are doing shine a light for us amateurs.

    I've found that there might be some issues in how ffmpeg calculates the padding. I looked at the examples at the FFMPEG site and the code you provided should work. I am looking at some options to work around it but need to get some sleep first. I think this approach makes so much sense I think i'm just missing something because I'm tired.

    Code:
    ~/Videos/oldmovies4$ ffmpeg -i MVI_0469.AVI -vf "transpose=1,crop=iw:240:0:0,pad=320:240:(ow-iw)/2:0" -target ntsc-dvd output.mpg
    ffmpeg version 0.8.9-4:0.8.9-0ubuntu0.12.04.1, Copyright (c) 2000-2013 the Libav developers
      built on Nov  9 2013 19:12:07 with gcc 4.6.3
    *** THIS PROGRAM IS DEPRECATED ***
    This program is only provided for compatibility and will be removed in a future release. Please use avconv instead.
    Input #0, avi, from 'MVI_0469.AVI':
      Metadata:
        creation_time   : 2003-09-14 18:25:07
        encoder         : CanonMVI01
      Duration: 00:00:28.99, start: 0.000000, bitrate: 1561 kb/s
        Stream #0.0: Video: mjpeg, yuvj422p, 320x240, 15 tbr, 15 tbn, 15 tbc
        Stream #0.1: Audio: pcm_u8, 11024 Hz, 1 channels, u8, 88 kb/s
    File 'output.mpg' already exists. Overwrite ? [y/N] y
    Incompatible pixel format 'yuvj422p' for codec 'mpeg2video', auto-selecting format 'yuv420p'
    [buffer @ 0x844f000] w:320 h:240 pixfmt:yuvj422p
    [scale @ 0x844e9e0] w:320 h:240 fmt:yuvj422p -> w:720 h:480 fmt:yuv420p flags:0x4
    [transpose @ 0x845ed40] w:720 h:480 dir:1 -> w:480 h:720 rotation:clockwise vflip:0
    [crop @ 0x845f1c0] w:480 h:720 -> w:480 h:240
    [pad @ 0x844ef00] Negative values are not acceptable.
    Error opening filters!
    Last edited by rioguia2; February 3rd, 2014 at 02:37 AM. Reason: highlighting unexpected result

  9. #9
    Join Date
    Jan 2014
    Beans
    16

    Re: Rotating avi file using ffmpeg and imagemagick

    I'm making some progress through reading and experimentation. I don't understand all the parameters and how they work. Here is an example for an .avi clip shot in portrait (i.e. at a 90 degree angle, 240 wide x 320 high). Transpose rotates the clip 90 degrees. Crop cuts 240 pixels off the new top of the clip and outputs to 720 x 480 mpg.

    Code:
    ffmpeg -i MVI_0469.AVI -vf "transpose=1,crop=in_w:in_h-240:0:240:in_w*9/16:(ow-iw)/2:0" -target ntsc-dvd output8.mpg
    Last edited by rioguia2; February 1st, 2014 at 09:01 PM.

  10. #10
    Join Date
    Sep 2006
    Beans
    3,491

    Re: Rotating avi file using ffmpeg and imagemagick

    Quote Originally Posted by rioguia2 View Post
    Code:
    ~/Videos/oldmovies4$ ffmpeg -i MVI_0469.AVI -vf "transpose=1,crop=iw:240:0:0,pad=320:240:(ow-iw)/2:0" -target ntsc-dvd output.mpg
    ffmpeg version 0.8.9-4:0.8.9-0ubuntu0.12.04.1, Copyright (c) 2000-2013 the Libav developers
      built on Nov  9 2013 19:12:07 with gcc 4.6.3
    *** THIS PROGRAM IS DEPRECATED ***
    This program is only provided for compatibility and will be removed in a future release. Please use avconv instead.
    ...
    [pad @ 0x844ef00] Negative values are not acceptable.
    Error opening filters!
    I can't duplicate this issue, but I'm not using the fake "ffmpeg" from Libav. Try a recent build of ffmpeg from FFmpeg. You can simply download a Linux build of ffmpeg or follow a step-by-step guide to compile ffmpeg.

Page 1 of 2 12 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •