无法在Mac上的python中使用opencv读取xvid视频

时间:2015-03-31 18:25:16

标签: python macos opencv ffmpeg

我一直在尝试使用python 2.7和opencv在我的mac上读取xvid .avi文件。我已经使用brew安装了所有最新版本,而opencv正在python中工作,我可以打开图像和其他电影文件但是当我尝试用

读取xvid avi文件时
cap.cv2.VideoCapture('video.AVI')
ret, frame = cap.read()

因框架为空而失败。我已经用brew和x264安装了ffmpeg并运行了brew医生。我完全迷失了现在要做的事情。很高兴在需要时提供更多信息。我希望有人可以帮助我,谢谢。

2 个答案:

答案 0 :(得分:4)

几周后我终于找到了解决方案。我首先必须安装Perian然后通过brew重新安装opencv并使用python和ffmpeg支持:

public class PagerContainer extends FrameLayout implements ViewPager.OnPageChangeListener {

    private ViewPager mPager;
    boolean mNeedsRedraw = false;
    public PagerContainer(Context context) {
        super(context);
        init();
    }

    public PagerContainer(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PagerContainer(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    private void init() {
//Disable clipping of children so non-selected pages are visible
        setClipChildren(false);

//Child clipping doesn't work with hardware acceleration in Android 3.x/4.x
//You need to set this value here if using hardware acceleration in an
// application targeted at these releases.
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }

    @Override
    protected void onFinishInflate() {
        try {
            mPager = (ViewPager) getChildAt(0);
            mPager.setOnPageChangeListener(this);
        } catch (Exception e) {
            throw new IllegalStateException("The root child of PagerContainer must be a ViewPager");
        }
    }

    public ViewPager getViewPager() {
        return mPager;
    }

    private Point mCenter = new Point();
    private Point mInitialTouch = new Point();

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        mCenter.x = w / 2;
        mCenter.y = h / 2;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
//We capture any touches not already handled by the ViewPager
// to implement scrolling from a touch outside the pager bounds.
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mInitialTouch.x = (int)ev.getX();
                mInitialTouch.y = (int)ev.getY();
            default:
                ev.offsetLocation(mCenter.x - mInitialTouch.x, mCenter.y - mInitialTouch.y);
                break;
        }

        return mPager.dispatchTouchEvent(ev);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//Force the container to redraw on scrolling.
//Without this the outer pages render initially and then stay static

        final int scrollX = mPager.getScrollX();
        final int childCount = mPager.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = mPager.getChildAt(i);
            final ViewPager.LayoutParams lp = (ViewPager.LayoutParams) child.getLayoutParams();
            if (lp.isDecor) continue;
            final float transformPos = (float) (child.getLeft() - scrollX) / child.getWidth();
            transformPage(child, transformPos);
        }
        if (mNeedsRedraw) invalidate();

    }

    @Override
    public void onPageSelected(int position) {
//         mPager. getChildAt(3).setAlpha(05f);

    }
    @Override
    public void onPageScrollStateChanged(int state) {
        mNeedsRedraw = (state != ViewPager.SCROLL_STATE_IDLE);

    }


    private static final float MIN_SCALE = 0.85f;
    private static final float MIN_ALPHA = 0.5f;

    public void transformPage(View view, float position) {
        int pageWidth = view.getWidth();
        int pageHeight = view.getHeight();

        if (position < -1) { // [-Infinity,-1)
            // This page is way off-screen to the left.
//            view.setAlpha(MIN_ALPHA);
            view.setScaleX(MIN_SCALE);
            view.setScaleY(MIN_SCALE);


        } else if (position <= 1) { // [-1,1]
            // Modify the default slide transition to shrink the page as well
            float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
            float vertMargin = pageHeight * (1 - scaleFactor) / 2;
            float horzMargin = pageWidth * (1 - scaleFactor) / 2;
            if (position < 0) {
                view.setTranslationX(horzMargin - vertMargin / 2);
            } else {
                view.setTranslationX(-horzMargin + vertMargin / 2);
            }

            // Scale the page down (between MIN_SCALE and 1)
            view.setScaleX(scaleFactor);
            view.setScaleY(scaleFactor);

            // Fade the page relative to its size.
//            view.setAlpha(MIN_ALPHA +  (scaleFactor - MIN_SCALE) / (1 - MIN_SCALE) * (1 - MIN_ALPHA));
            view.clearAnimation();

        } else { // (1,+Infinity]
            // This page is way off-screen to the right.
//            view.setAlpha(MIN_ALPHA);
            view.setScaleX(MIN_SCALE);
            view.setScaleY(MIN_SCALE);

        }
    }

}

我希望这对其他人有用。

答案 1 :(得分:1)

对于opencv3(在pyenv中),我将补充crazjo非常有用和准确的问答。在找到答案之前,我也徒劳地努力阅读avi文件。最后我能够阅读xvid avi文件!

我做了以下(我不能也没有安装perian !!):

CREATE TABLE { table-name } AS (
       SELECT { column-name }, ..., { column-name }
       FROM { table-name }, ..., { table-name } 
       -- {WHERE expression} 
       ) DATA INITIALLY DEFERRED REFRESH DEFERRED
       MAINTAINED BY USER
       ORGANIZE BY COLUMN
       ;
SET INTEGRITY FOR { table-name }
       ALL IMMEDIATE UNCHECKED
       ;

之后我在virtualenv和opencv3的网站包之间做了一个符号链接。

并且它有效!!我可以阅读avi文件。

再次感谢,我希望这对其他人也有用!!